rudash 2.7.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1be883b432dd317e12aa88cf12390e067292d866cb30ce89f0fd43826f75de74
4
- data.tar.gz: fca7e87890b36e9e1423f5f5d1661d23db04834d08cb5b247f72e26dd9747ee4
3
+ metadata.gz: bd83c39622ad30f4a0c1831c7ccb7cd7a92c5bad4f46e39b854d2e7bf9f6f176
4
+ data.tar.gz: 3dbd1c65c36b27dfa5b1b3a8bcefbc84b600d9c8f20c9f43a9861cafa96af37a
5
5
  SHA512:
6
- metadata.gz: 80dd01987d0301f1f5311d1b3e2278d71d2662d0eacd2864abb196224621fd878c276e11edc10288f7269340658685f760f8c5a1bd0e123fc3b283a56c998402
7
- data.tar.gz: 2c4e8f9a515c7877518d30376a9a4b5692c3a517cd2be2f45bf0c1dff483526a3ab8cab46aa9b57b8564cc129edb48352a0db925b05aeeacd793ba1e23481236
6
+ metadata.gz: a85e4581e865eab2ec4c3fdc43fe3c442e4d4b953e531942278d8b37e186c3af466bf3e35e14a83d2147176ca640bd425dd2ce5f1d2a17b9e1faf624bdccf7c4
7
+ data.tar.gz: aba56b13cbd1ca07343cd2eec0914dcc64f2a6e61e817afab123ac6f57766e737cdd2c3a0cc42ceb53047fdcd163c58a4ce6b2f31e51d581e0128bcc4352dfb7
@@ -1,16 +1,11 @@
1
1
  require_relative 'each.rb'
2
+ require_relative '../utils/index.rb'
2
3
 
3
4
  module EachRight
4
5
  extend Each
5
6
 
6
7
  def each_right(collection, *rest_args)
7
- reversed_collection =
8
- case collection
9
- when Array then collection.reverse
10
- when Hash then collection.reverse_each.to_h
11
- when String then collection.reverse
12
- else []
13
- end
8
+ reversed_collection = Utils.force_reverse(collection)
14
9
 
15
10
  self.each(reversed_collection, *rest_args)
16
11
  collection
@@ -1,6 +1,7 @@
1
1
  require_relative 'is_nil.rb'
2
2
  require_relative 'identity.rb'
3
3
  require_relative 'head.rb'
4
+ require_relative '../utils/subset_deep_match.rb'
4
5
 
5
6
  module Filter
6
7
  extend IsNil
@@ -10,24 +11,12 @@ module Filter
10
11
  def filter(collection, *rest_args)
11
12
  filter = self.head(rest_args) || self.method(:identity)
12
13
 
13
- if collection.is_a?(Array) && filter.is_a?(Hash)
14
- filtered_arr = []
15
- collection.each do |v|
16
- match = true
17
- filter.each do |key, value|
18
- if (value != v[key])
19
- match = false
20
- break
21
- end
22
- end
23
-
24
- if (match == true)
25
- filtered_arr << v
26
- end
27
- end
14
+ if filter.is_a?(Hash)
15
+ slice_matcher = SubsetDeepMatch.subset_deep_match?.(filter)
16
+ return self.filter(collection, slice_matcher)
17
+ end
28
18
 
29
- return filtered_arr
30
- elsif collection.is_a?(Array)
19
+ if collection.is_a?(Array)
31
20
  begin
32
21
  return collection.select.with_index { |x, idx| filter[x, idx] }
33
22
  rescue ArgumentError => e
@@ -1,17 +1,11 @@
1
1
  require_relative 'reduce.rb'
2
+ require_relative '../utils/index.rb'
2
3
 
3
4
  module ReduceRight
4
5
  extend Reduce
5
6
 
6
7
  def reduce_right(collection, *rest_args)
7
- reversed_collection =
8
- case collection
9
- when Array then collection.reverse
10
- when Hash then collection.reverse_each.to_h
11
- when String then collection.reverse
12
- else []
13
- end
14
-
8
+ reversed_collection = Utils.force_reverse(collection)
15
9
  self.reduce(reversed_collection, *rest_args)
16
10
  end
17
11
  end
@@ -18,8 +18,7 @@ module Union
18
18
 
19
19
  arr_values = self.filter(values, is_array)
20
20
  head = self.head(arr_values)
21
- return [] if !head.is_a?(Array)
22
21
 
23
- self.reduce(arr_values, union_reducer, head)
22
+ self.reduce(arr_values, union_reducer, head) || []
24
23
  end
25
24
  end
@@ -3,4 +3,13 @@ module Utils
3
3
  return false if !str.is_a?(String)
4
4
  str.match(/^(\d)+$/)
5
5
  end
6
+
7
+ def self.force_reverse(collection)
8
+ case collection
9
+ when Array then collection.reverse
10
+ when Hash then collection.reverse_each.to_h
11
+ when String then collection.reverse
12
+ else []
13
+ end
14
+ end
6
15
  end
@@ -0,0 +1,50 @@
1
+ require_relative '../rudash'
2
+
3
+ # This module was written to supply complex subset deep hash and array matching
4
+ # in order to give filter, some?, every? and find the ability to deep match with complex hash queries.
5
+ # See test_filter_hashes_by_deep_hash (test/filter.rb)
6
+
7
+ module SubsetDeepMatch
8
+ def self.subset_deep_match?
9
+ subset_matcher = -> (slice, collection) {
10
+ match = true
11
+
12
+ # If was called with two arrays then the logic will be to
13
+ # check if every "slice" items exist somehow in the collection
14
+ # without any order consideration.
15
+ if (slice.is_a?(Array) && collection.is_a?(Array))
16
+ return R_.every?(slice, -> (sliceVal) {
17
+ R_.some?(collection, -> (collectionVal) {
18
+ self.subset_deep_match?.(sliceVal, collectionVal)
19
+ })
20
+ })
21
+ end
22
+
23
+ begin
24
+ R_.each(collection, -> (v) {
25
+ R_.each(slice, -> (value, key) {
26
+ if (value.is_a?(Hash) && collection[key].is_a?(Hash))
27
+ match &= self.subset_deep_match?.(value, collection[key])
28
+ elsif (value.is_a?(Array) && collection[key].is_a?(Array))
29
+ match &= self.subset_deep_match?.(value, collection[key])
30
+ elsif (value != collection[key])
31
+ match = false
32
+ end
33
+ })
34
+
35
+ # That was done for performance manners since
36
+ # R_.each don't stop when returning false from the proc
37
+ # so we force it to stop by throwing an exception that we catch later
38
+ # It's the same hack for JavaScript forEach function.
39
+ raise if match == false
40
+ })
41
+ rescue
42
+ return false
43
+ end
44
+
45
+ match
46
+ }
47
+
48
+ subset_matcher.curry
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rudash
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Islam Attrash
@@ -66,6 +66,7 @@ files:
66
66
  - lib/utils/index.rb
67
67
  - lib/utils/nested_path_creator.rb
68
68
  - lib/utils/path_resolver.rb
69
+ - lib/utils/subset_deep_match.rb
69
70
  homepage: https://github.com/Attrash-Islam/rudash
70
71
  licenses:
71
72
  - MIT