rudash 2.7.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rudash/each_right.rb +2 -7
- data/lib/rudash/filter.rb +6 -17
- data/lib/rudash/reduce_right.rb +2 -8
- data/lib/rudash/union.rb +1 -2
- data/lib/utils/index.rb +9 -0
- data/lib/utils/subset_deep_match.rb +50 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd83c39622ad30f4a0c1831c7ccb7cd7a92c5bad4f46e39b854d2e7bf9f6f176
|
4
|
+
data.tar.gz: 3dbd1c65c36b27dfa5b1b3a8bcefbc84b600d9c8f20c9f43a9861cafa96af37a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a85e4581e865eab2ec4c3fdc43fe3c442e4d4b953e531942278d8b37e186c3af466bf3e35e14a83d2147176ca640bd425dd2ce5f1d2a17b9e1faf624bdccf7c4
|
7
|
+
data.tar.gz: aba56b13cbd1ca07343cd2eec0914dcc64f2a6e61e817afab123ac6f57766e737cdd2c3a0cc42ceb53047fdcd163c58a4ce6b2f31e51d581e0128bcc4352dfb7
|
data/lib/rudash/each_right.rb
CHANGED
@@ -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
|
data/lib/rudash/filter.rb
CHANGED
@@ -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
|
14
|
-
|
15
|
-
collection
|
16
|
-
|
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
|
-
|
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
|
data/lib/rudash/reduce_right.rb
CHANGED
@@ -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
|
data/lib/rudash/union.rb
CHANGED
data/lib/utils/index.rb
CHANGED
@@ -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.
|
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
|