rudash 2.8.0 → 2.8.1
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/at.rb +11 -10
- data/lib/rudash/capitalize.rb +9 -7
- data/lib/rudash/compact.rb +7 -5
- data/lib/rudash/concat.rb +10 -8
- data/lib/rudash/curry.rb +9 -7
- data/lib/rudash/difference.rb +14 -12
- data/lib/rudash/each.rb +12 -10
- data/lib/rudash/each_right.rb +14 -12
- data/lib/rudash/eq.rb +5 -3
- data/lib/rudash/every.rb +8 -6
- data/lib/rudash/filter.rb +25 -25
- data/lib/rudash/find.rb +10 -10
- data/lib/rudash/find_last.rb +10 -9
- data/lib/rudash/flip.rb +9 -7
- data/lib/rudash/get.rb +26 -25
- data/lib/rudash/head.rb +10 -8
- data/lib/rudash/identity.rb +5 -3
- data/lib/rudash/initial.rb +7 -5
- data/lib/rudash/intersection.rb +13 -11
- data/lib/rudash/is_array.rb +5 -3
- data/lib/rudash/is_empty.rb +9 -7
- data/lib/rudash/is_equal.rb +5 -3
- data/lib/rudash/is_hash.rb +5 -3
- data/lib/rudash/is_nil.rb +5 -3
- data/lib/rudash/is_number.rb +5 -3
- data/lib/rudash/is_proc.rb +5 -3
- data/lib/rudash/is_string.rb +9 -7
- data/lib/rudash/join.rb +7 -5
- data/lib/rudash/keys.rb +11 -9
- data/lib/rudash/last.rb +6 -4
- data/lib/rudash/map.rb +23 -22
- data/lib/rudash/negate.rb +11 -9
- data/lib/rudash/pick.rb +20 -20
- data/lib/rudash/reduce.rb +34 -32
- data/lib/rudash/reduce_right.rb +8 -6
- data/lib/rudash/remove.rb +12 -12
- data/lib/rudash/reverse.rb +9 -7
- data/lib/rudash/set.rb +22 -20
- data/lib/rudash/size.rb +8 -6
- data/lib/rudash/slice.rb +13 -11
- data/lib/rudash/some.rb +8 -6
- data/lib/rudash/tail.rb +6 -4
- data/lib/rudash/union.rb +18 -18
- data/lib/rudash/uniq.rb +11 -9
- data/lib/rudash/update.rb +10 -11
- data/lib/rudash/without.rb +8 -6
- data/lib/rudash.rb +46 -46
- data/lib/utils/index.rb +14 -12
- data/lib/utils/nested_path_creator.rb +22 -20
- data/lib/utils/path_resolver.rb +17 -15
- data/lib/utils/subset_deep_match.rb +42 -40
- metadata +1 -1
data/lib/utils/index.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Rudash
|
2
|
+
module Utils
|
3
|
+
def self.match_number?(str)
|
4
|
+
return false if !str.is_a?(String)
|
5
|
+
str.match(/^(\d)+$/)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.force_reverse(collection)
|
9
|
+
case collection
|
10
|
+
when Array then collection.reverse
|
11
|
+
when Hash then collection.reverse_each.to_h
|
12
|
+
when String then collection.reverse
|
13
|
+
else []
|
14
|
+
end
|
13
15
|
end
|
14
16
|
end
|
15
17
|
end
|
@@ -1,27 +1,29 @@
|
|
1
1
|
require_relative '../rudash'
|
2
2
|
require_relative './index.rb'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
if
|
16
|
-
|
17
|
-
|
18
|
-
|
4
|
+
module Rudash
|
5
|
+
module NestedPathCreator
|
6
|
+
def self.create_path_if_not_exist(object, resolved_path)
|
7
|
+
path = R_.head(resolved_path)
|
8
|
+
return nil if !resolved_path.is_a?(Array) || R_.is_nil?(path)
|
9
|
+
|
10
|
+
path_key = Utils.match_number?(path) ? path.to_i : path.to_sym
|
11
|
+
rest_paths = R_.tail(resolved_path)
|
12
|
+
next_path = R_.head(rest_paths)
|
13
|
+
value = R_.get(object, path)
|
14
|
+
|
15
|
+
if R_.is_nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
|
16
|
+
if next_path && Utils.match_number?(next_path)
|
17
|
+
object[path_key] = []
|
18
|
+
else
|
19
|
+
object[path_key] = {}
|
20
|
+
end
|
19
21
|
end
|
22
|
+
|
23
|
+
self.create_path_if_not_exist(
|
24
|
+
R_.get(object, path),
|
25
|
+
rest_paths
|
26
|
+
)
|
20
27
|
end
|
21
|
-
|
22
|
-
self.create_path_if_not_exist(
|
23
|
-
R_.get(object, path),
|
24
|
-
rest_paths
|
25
|
-
)
|
26
28
|
end
|
27
29
|
end
|
data/lib/utils/path_resolver.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
1
|
require_relative '../rudash'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Rudash
|
4
|
+
module PathResolver
|
5
|
+
def self.resolve(path)
|
6
|
+
normalized_path = path
|
7
|
+
|
8
|
+
if normalized_path.is_a?(Array)
|
9
|
+
normalized_path = normalized_path.join('.')
|
10
|
+
end
|
11
|
+
|
12
|
+
filter_not_empty = -> (value) {
|
13
|
+
value != ''
|
14
|
+
}
|
15
|
+
|
16
|
+
splitted_hash = R_.filter(
|
17
|
+
normalized_path.split(/[.\[\]]/),
|
18
|
+
filter_not_empty
|
19
|
+
)
|
9
20
|
end
|
10
|
-
|
11
|
-
filter_not_empty = -> (value) {
|
12
|
-
value != ''
|
13
|
-
}
|
14
|
-
|
15
|
-
splitted_hash = R_.filter(
|
16
|
-
normalized_path.split(/[.\[\]]/),
|
17
|
-
filter_not_empty
|
18
|
-
)
|
19
21
|
end
|
20
22
|
end
|
@@ -4,47 +4,49 @@ require_relative '../rudash'
|
|
4
4
|
# in order to give filter, some?, every? and find the ability to deep match with complex hash queries.
|
5
5
|
# See test_filter_hashes_by_deep_hash (test/filter.rb)
|
6
6
|
|
7
|
-
module
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
R_.
|
18
|
-
|
7
|
+
module Rudash
|
8
|
+
module SubsetDeepMatch
|
9
|
+
def self.subset_deep_match?
|
10
|
+
subset_matcher = -> (slice, collection) {
|
11
|
+
match = true
|
12
|
+
|
13
|
+
# If was called with two arrays then the logic will be to
|
14
|
+
# check if every "slice" items exist somehow in the collection
|
15
|
+
# without any order consideration.
|
16
|
+
if (slice.is_a?(Array) && collection.is_a?(Array))
|
17
|
+
return R_.every?(slice, -> (sliceVal) {
|
18
|
+
R_.some?(collection, -> (collectionVal) {
|
19
|
+
self.subset_deep_match?.(sliceVal, collectionVal)
|
20
|
+
})
|
19
21
|
})
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
R_.each(collection, -> (v) {
|
26
|
+
R_.each(slice, -> (value, key) {
|
27
|
+
if (value.is_a?(Hash) && collection[key].is_a?(Hash))
|
28
|
+
match &= self.subset_deep_match?.(value, collection[key])
|
29
|
+
elsif (value.is_a?(Array) && collection[key].is_a?(Array))
|
30
|
+
match &= self.subset_deep_match?.(value, collection[key])
|
31
|
+
elsif (value != collection[key])
|
32
|
+
match = false
|
33
|
+
end
|
34
|
+
})
|
35
|
+
|
36
|
+
# That was done for performance manners since
|
37
|
+
# R_.each don't stop when returning false from the proc
|
38
|
+
# so we force it to stop by throwing an exception that we catch later
|
39
|
+
# It's the same hack for JavaScript forEach function.
|
40
|
+
raise if match == false
|
33
41
|
})
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
match
|
46
|
-
}
|
47
|
-
|
48
|
-
subset_matcher.curry
|
42
|
+
rescue
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
|
46
|
+
match
|
47
|
+
}
|
48
|
+
|
49
|
+
subset_matcher.curry
|
50
|
+
end
|
49
51
|
end
|
50
52
|
end
|