Current section
19 Versions
Jump to
Current section
19 Versions
Compare versions
4
files changed
+23
additions
-15
deletions
| @@ -1,3 +1,9 @@ | |
| 1 | + |
| 2 | + [](https://hex.pm/packages/test_match) |
| 3 | + [](https://travis-ci.org/Apelsinka223/test_match) |
| 4 | + [](https://coveralls.io/github/Apelsinka223/test_match?branch=master) |
| 5 | + [](http://inch-ci.org/github/Apelsinka223/test_match) |
| 6 | + |
| 1 7 | # RecursiveMatch |
| 2 8 | |
| 3 9 | Module for matching |
| @@ -11,4 +11,4 @@ | |
| 11 11 | {<<"maintainers">>,[<<"Anastasiya Dyachenko">>]}. |
| 12 12 | {<<"name">>,<<"test_match">>}. |
| 13 13 | {<<"requirements">>,[]}. |
| 14 | - {<<"version">>,<<"1.2.0">>}. |
| 14 | + {<<"version">>,<<"1.2.1">>}. |
| @@ -46,26 +46,15 @@ defmodule RecursiveMatch do | |
| 46 46 | |
| 47 47 | def match_r(pattern, tested, options) when is_list(tested) and is_list(pattern) do |
| 48 48 | if Enum.count(pattern) == Enum.count(tested) do |
| 49 | - |
| 50 | - pattern = |
| 51 49 | if options[:ignore_order] == true do |
| 52 | - Enum.sort(pattern) |
| 50 | + match_lists_ignore_order(pattern, tested) |
| 53 51 | else |
| 54 52 | pattern |
| 55 | - end |
| 56 | - |
| 57 | - tested = |
| 58 | - if options[:ignore_order] == true do |
| 59 | - Enum.sort(tested) |
| 60 | - else |
| 61 | - tested |
| 62 | - end |
| 63 | - |
| 64 | - pattern |
| 65 53 | |> Enum.zip(tested) |
| 66 54 | |> Enum.all?(fn {pattern_item, tested_item} -> |
| 67 55 | match_r(pattern_item, tested_item, options) |
| 68 56 | end) |
| 57 | + end |
| 69 58 | else |
| 70 59 | false |
| 71 60 | end |
| @@ -96,6 +85,19 @@ defmodule RecursiveMatch do | |
| 96 85 | end |
| 97 86 | end |
| 98 87 | |
| 88 | + defp match_lists_ignore_order([], []), do: true |
| 89 | + |
| 90 | + defp match_lists_ignore_order([pattern | pattern_tail], tested) do |
| 91 | + case Enum.find_index(tested, fn t -> match_r pattern, t end) do |
| 92 | + nil -> |
| 93 | + false |
| 94 | + |
| 95 | + index -> |
| 96 | + tested_rest = List.delete_at(tested, index) |
| 97 | + match_lists_ignore_order(pattern_tail, tested_rest) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 99 101 | @doc """ |
| 100 102 | Matches given value with pattern |
| @@ -4,7 +4,7 @@ defmodule TestMatch.MixProject do | |
| 4 4 | def project do |
| 5 5 | [ |
| 6 6 | app: :test_match, |
| 7 | - version: "1.2.0", |
| 7 | + version: "1.2.1", |
| 8 8 | elixir: "~> 1.5", |
| 9 9 | build_embedded: Mix.env() == :prod, |
| 10 10 | start_permanent: Mix.env() == :prod, |