rudash 2.17.3 → 2.17.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rudash/at.rb +2 -2
- data/lib/rudash/compact.rb +2 -1
- data/lib/rudash/concat.rb +3 -5
- data/lib/rudash/difference.rb +5 -5
- data/lib/rudash/drop_right.rb +3 -3
- data/lib/rudash/each.rb +1 -1
- data/lib/rudash/each_right.rb +2 -2
- data/lib/rudash/filter.rb +10 -10
- data/lib/rudash/find.rb +1 -1
- data/lib/rudash/find_last.rb +1 -1
- data/lib/rudash/flip.rb +4 -4
- data/lib/rudash/flow.rb +6 -6
- data/lib/rudash/flow_right.rb +1 -1
- data/lib/rudash/get.rb +6 -6
- data/lib/rudash/group_by.rb +5 -5
- data/lib/rudash/head.rb +3 -2
- data/lib/rudash/identity.rb +1 -1
- data/lib/rudash/initial.rb +3 -2
- data/lib/rudash/intersection.rb +3 -3
- data/lib/rudash/join.rb +3 -3
- data/lib/rudash/keys.rb +2 -2
- data/lib/rudash/last.rb +2 -1
- data/lib/rudash/map.rb +5 -5
- data/lib/rudash/negate.rb +3 -5
- data/lib/rudash/pick.rb +7 -9
- data/lib/rudash/range.rb +12 -10
- data/lib/rudash/reduce.rb +5 -5
- data/lib/rudash/reject.rb +3 -3
- data/lib/rudash/remove.rb +3 -2
- data/lib/rudash/set.rb +6 -6
- data/lib/rudash/size.rb +1 -0
- data/lib/rudash/slice.rb +5 -5
- data/lib/rudash/some.rb +1 -1
- data/lib/rudash/tail.rb +2 -1
- data/lib/rudash/take.rb +4 -3
- data/lib/rudash/union.rb +7 -6
- data/lib/rudash/unset.rb +5 -5
- data/lib/rudash/update.rb +2 -2
- data/lib/rudash/without.rb +2 -1
- data/lib/rudash.rb +1 -1
- data/lib/utils/chain_wrapper.rb +4 -6
- data/lib/utils/dynamic_args_count.rb +7 -7
- data/lib/utils/index.rb +4 -3
- data/lib/utils/nested_path_creator.rb +7 -5
- data/lib/utils/path_resolver.rb +6 -6
- data/lib/utils/subset_deep_match.rb +38 -38
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d799e40ff80455b2ed477fc110aa72a0cac0ebf1f3523e0bcb9faebabff0e93
|
4
|
+
data.tar.gz: 7e3d4c6be34feb99a7791bce6482a685d9b9944f4b59b0d21f6c8af9ba376d54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b58b76b34470accda8d425d539d1a90414df30fd58bd2f226b152ea73afa92cbd2116eb010b1434d7026a7f57c98f7155e9bfff98ae5046ae1d7e8854751cc7
|
7
|
+
data.tar.gz: b4eb8d6c1577c3ed0243066f16434e35d111aa74c00abbfb22a15ef1a1611dcb587f82f1fca571dfb3581406fc24184017c51aeb7e19d96abf33ea73bc1a00df
|
data/lib/rudash/at.rb
CHANGED
data/lib/rudash/compact.rb
CHANGED
data/lib/rudash/concat.rb
CHANGED
@@ -2,11 +2,9 @@ module Rudash
|
|
2
2
|
module Concat
|
3
3
|
def concat(head, *values)
|
4
4
|
head_arr = head.is_a?(Array) ? head : [head]
|
5
|
-
if values.size
|
6
|
-
|
7
|
-
|
8
|
-
return head_arr + self.concat(*values)
|
9
|
-
end
|
5
|
+
return head_arr if values.size.zero?
|
6
|
+
|
7
|
+
head_arr + self.concat(*values)
|
10
8
|
end
|
11
9
|
end
|
12
10
|
end
|
data/lib/rudash/difference.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Difference
|
3
3
|
def difference(*values)
|
4
|
-
diff_reducer = ->
|
5
|
-
return []
|
6
|
-
return acc
|
7
|
-
|
4
|
+
diff_reducer = ->(acc, current) {
|
5
|
+
return [] unless acc.is_a?(Array)
|
6
|
+
return acc unless current.is_a?(Array)
|
7
|
+
|
8
8
|
acc - current
|
9
9
|
}
|
10
|
-
|
10
|
+
|
11
11
|
self.reduce(values, diff_reducer)
|
12
12
|
end
|
13
13
|
end
|
data/lib/rudash/drop_right.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module Rudash
|
2
2
|
module DropRight
|
3
3
|
def drop_right(array, *rest_args)
|
4
|
-
return []
|
5
|
-
|
4
|
+
return [] unless self.is_array?(array)
|
5
|
+
|
6
6
|
n = self.head(rest_args) || 1
|
7
7
|
return array if n <= 0
|
8
|
-
|
8
|
+
|
9
9
|
self.take(array, self.size(array) - n)
|
10
10
|
end
|
11
11
|
end
|
data/lib/rudash/each.rb
CHANGED
data/lib/rudash/each_right.rb
CHANGED
@@ -2,11 +2,11 @@ module Rudash
|
|
2
2
|
module EachRight
|
3
3
|
def each_right(collection, *rest_args)
|
4
4
|
reversed_collection = Rudash::Utils.force_reverse(collection)
|
5
|
-
|
5
|
+
|
6
6
|
self.each(reversed_collection, *rest_args)
|
7
7
|
collection
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def for_each_right(*args)
|
11
11
|
self.each_right(*args)
|
12
12
|
end
|
data/lib/rudash/filter.rb
CHANGED
@@ -2,22 +2,22 @@ module Rudash
|
|
2
2
|
module Filter
|
3
3
|
def filter(collection, *rest_args)
|
4
4
|
predicate_fn = self.head(rest_args) || self.method(:identity)
|
5
|
-
|
5
|
+
|
6
6
|
if predicate_fn.is_a?(Hash)
|
7
|
-
slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.(predicate_fn)
|
8
|
-
return self.filter(collection, slice_matcher)
|
7
|
+
slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.call(predicate_fn)
|
8
|
+
return self.filter(collection, slice_matcher)
|
9
9
|
end
|
10
|
-
|
11
|
-
return []
|
12
|
-
|
10
|
+
|
11
|
+
return [] unless Rudash::Utils.is_function?(predicate_fn)
|
12
|
+
|
13
13
|
if collection.is_a?(Array)
|
14
|
-
return collection.select.with_index
|
14
|
+
return collection.select.with_index do |x, idx|
|
15
15
|
Rudash::DynamicArgsCount.call(predicate_fn, x, idx)
|
16
|
-
|
16
|
+
end
|
17
17
|
elsif collection.is_a?(Hash)
|
18
|
-
return collection.select
|
18
|
+
return collection.select do |k, v|
|
19
19
|
Rudash::DynamicArgsCount.call(predicate_fn, v, k)
|
20
|
-
|
20
|
+
end.values
|
21
21
|
else
|
22
22
|
return []
|
23
23
|
end
|
data/lib/rudash/find.rb
CHANGED
data/lib/rudash/find_last.rb
CHANGED
data/lib/rudash/flip.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Flip
|
3
3
|
def flip(a_proc)
|
4
|
-
raise 'Expected a Proc/Method'
|
5
|
-
|
6
|
-
|
4
|
+
raise 'Expected a Proc/Method' unless Rudash::Utils.is_function?(a_proc)
|
5
|
+
|
6
|
+
->(*args) {
|
7
7
|
reveresed_args = args.reverse
|
8
|
-
|
8
|
+
|
9
9
|
a_proc.(*reveresed_args)
|
10
10
|
}
|
11
11
|
end
|
data/lib/rudash/flow.rb
CHANGED
@@ -2,12 +2,12 @@ module Rudash
|
|
2
2
|
module Flow
|
3
3
|
def flow(*funs)
|
4
4
|
flatten_funs = funs.flatten
|
5
|
-
|
6
|
-
|
7
|
-
self.reduce(flatten_funs, ->
|
5
|
+
|
6
|
+
->(*args) {
|
7
|
+
self.reduce(flatten_funs, ->(acc, fn) {
|
8
8
|
Rudash::DynamicArgsCount.call(fn, *self.concat(acc))
|
9
|
-
|
10
|
-
|
11
|
-
end
|
9
|
+
}, args)
|
10
|
+
}
|
12
11
|
end
|
13
12
|
end
|
13
|
+
end
|
data/lib/rudash/flow_right.rb
CHANGED
data/lib/rudash/get.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Get
|
3
|
-
def get(hash, path, *
|
3
|
+
def get(hash, path, *_rest_args)
|
4
4
|
return nil if !path.is_a?(String) && !path.is_a?(Array)
|
5
5
|
return nil if !hash.is_a?(Array) && !hash.is_a?(Hash)
|
6
|
-
|
6
|
+
|
7
7
|
resolved_path = Rudash::PathResolver.resolve(path)
|
8
|
-
|
9
|
-
get_reducer = ->
|
8
|
+
|
9
|
+
get_reducer = ->(acc, current) {
|
10
10
|
return nil if acc.nil?
|
11
|
-
|
11
|
+
|
12
12
|
if acc.is_a?(Array) && Rudash::Utils.match_number?(current)
|
13
13
|
acc[current.to_i]
|
14
14
|
elsif acc.is_a?(Array) && !Rudash::Utils.match_number?(current)
|
@@ -19,7 +19,7 @@ module Rudash
|
|
19
19
|
nil
|
20
20
|
end
|
21
21
|
}
|
22
|
-
|
22
|
+
|
23
23
|
self.reduce(resolved_path, get_reducer, hash)
|
24
24
|
end
|
25
25
|
end
|
data/lib/rudash/group_by.rb
CHANGED
@@ -2,19 +2,19 @@ module Rudash
|
|
2
2
|
module GroupBy
|
3
3
|
def group_by(collection, *rest_args)
|
4
4
|
iteratee = self.head(rest_args) || self.method(:identity)
|
5
|
-
|
6
|
-
reducer = ->
|
5
|
+
|
6
|
+
reducer = ->(acc, current) {
|
7
7
|
key = Rudash::DynamicArgsCount.call(iteratee, current)
|
8
|
-
|
8
|
+
|
9
9
|
if acc[key]
|
10
10
|
acc[key] << current
|
11
11
|
else
|
12
12
|
acc[key] = [current]
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
acc
|
16
16
|
}
|
17
|
-
|
17
|
+
|
18
18
|
self.reduce(collection, reducer, {})
|
19
19
|
end
|
20
20
|
end
|
data/lib/rudash/head.rb
CHANGED
data/lib/rudash/identity.rb
CHANGED
data/lib/rudash/initial.rb
CHANGED
data/lib/rudash/intersection.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Intersection
|
3
3
|
def intersection(*values)
|
4
|
-
|
5
|
-
intersection_reducer = -> (acc, current) {
|
4
|
+
intersection_reducer = ->(acc, current) {
|
6
5
|
return [] if !current.is_a?(Array) || !acc.is_a?(Array)
|
6
|
+
|
7
7
|
acc & current
|
8
8
|
}
|
9
|
-
|
9
|
+
|
10
10
|
self.reduce(values, intersection_reducer, values[0])
|
11
11
|
end
|
12
12
|
end
|
data/lib/rudash/join.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Join
|
3
3
|
def join(array, separator = ',')
|
4
|
-
return ''
|
5
|
-
|
6
|
-
array.join(
|
4
|
+
return '' unless array.is_a?(Array)
|
5
|
+
|
6
|
+
array.join(separator.to_s)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
data/lib/rudash/keys.rb
CHANGED
@@ -3,9 +3,9 @@ module Rudash
|
|
3
3
|
def keys(value)
|
4
4
|
case value
|
5
5
|
when Hash
|
6
|
-
value.map { |key,
|
6
|
+
value.map { |key, _value| key.to_s }
|
7
7
|
when Array
|
8
|
-
value.map.with_index { |
|
8
|
+
value.map.with_index { |_value, index| index.to_s }
|
9
9
|
else
|
10
10
|
[]
|
11
11
|
end
|
data/lib/rudash/last.rb
CHANGED
data/lib/rudash/map.rb
CHANGED
@@ -3,17 +3,17 @@ module Rudash
|
|
3
3
|
def map(collection, *rest_args)
|
4
4
|
iteratee_fn = self.head(rest_args) || self.method(:identity)
|
5
5
|
col = collection.is_a?(String) ? collection.split('') : collection
|
6
|
-
|
7
|
-
return self.map(collection, ->
|
8
|
-
|
6
|
+
|
7
|
+
return self.map(collection, ->() { nil }) unless Rudash::Utils.is_function?(iteratee_fn)
|
8
|
+
|
9
9
|
if col.is_a?(Array)
|
10
10
|
return col.map.with_index { |value, index|
|
11
11
|
Rudash::DynamicArgsCount.call(iteratee_fn, value, index)
|
12
12
|
}
|
13
13
|
elsif col.is_a?(Hash)
|
14
|
-
return col.map
|
14
|
+
return col.map do |k, v|
|
15
15
|
Rudash::DynamicArgsCount.call(iteratee_fn, v, k)
|
16
|
-
|
16
|
+
end
|
17
17
|
else
|
18
18
|
return []
|
19
19
|
end
|
data/lib/rudash/negate.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Negate
|
3
3
|
def negate(a_proc)
|
4
|
-
raise 'Expected a Proc/Method'
|
5
|
-
|
6
|
-
|
7
|
-
!a_proc.(*args)
|
8
|
-
}
|
4
|
+
raise 'Expected a Proc/Method' unless Rudash::Utils.is_function?(a_proc)
|
5
|
+
|
6
|
+
->(*args) { !a_proc.call(*args) }
|
9
7
|
end
|
10
8
|
end
|
11
9
|
end
|
data/lib/rudash/pick.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Pick
|
3
3
|
def pick(hash, paths)
|
4
|
-
return self.pick(hash, [paths])
|
5
|
-
return {}
|
6
|
-
|
4
|
+
return self.pick(hash, [paths]) unless paths.is_a?(Array)
|
5
|
+
return {} unless hash.is_a?(Hash)
|
6
|
+
|
7
7
|
picked_hash = {}
|
8
|
-
|
9
|
-
eacher = ->
|
8
|
+
|
9
|
+
eacher = ->(path) {
|
10
10
|
value = self.get(hash, path)
|
11
|
-
|
12
|
-
self.set(picked_hash, path, value)
|
13
|
-
end
|
11
|
+
self.set(picked_hash, path, value) unless value.nil?
|
14
12
|
}
|
15
|
-
|
13
|
+
|
16
14
|
self.each(paths, eacher)
|
17
15
|
picked_hash
|
18
16
|
end
|
data/lib/rudash/range.rb
CHANGED
@@ -4,7 +4,7 @@ module Rudash
|
|
4
4
|
start_step = 0
|
5
5
|
step_jump = 1
|
6
6
|
end_step = 0
|
7
|
-
|
7
|
+
|
8
8
|
case args.size
|
9
9
|
when 0
|
10
10
|
return []
|
@@ -16,30 +16,32 @@ module Rudash
|
|
16
16
|
start_step, end_step, step_jump = args
|
17
17
|
step_jump_configured = true
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
# step_jump direction (+/-) should be defined by start/end values
|
21
21
|
norm_step_jump = (end_step > start_step ? step_jump.abs : -step_jump.abs)
|
22
|
-
|
22
|
+
|
23
23
|
# illegal behaviors
|
24
|
-
|
24
|
+
|
25
|
+
return [] if norm_step_jump != step_jump && step_jump_configured
|
26
|
+
|
25
27
|
# End illegal behavior
|
26
|
-
|
28
|
+
|
27
29
|
iterator = start_step
|
28
30
|
result = []
|
29
|
-
|
31
|
+
|
30
32
|
# calculate loop count
|
31
33
|
boundaries = [start_step, end_step]
|
32
34
|
max = boundaries.max
|
33
35
|
min = boundaries.min
|
34
|
-
i = (norm_step_jump
|
36
|
+
i = (norm_step_jump.zero? ? (max - min) : ((max - min).to_f / norm_step_jump)).abs
|
35
37
|
# end loop calculation
|
36
|
-
|
38
|
+
|
37
39
|
while i > 0
|
38
40
|
result << iterator
|
39
41
|
iterator += norm_step_jump
|
40
|
-
i-=1
|
42
|
+
i -= 1
|
41
43
|
end
|
42
|
-
|
44
|
+
|
43
45
|
result
|
44
46
|
end
|
45
47
|
end
|
data/lib/rudash/reduce.rb
CHANGED
@@ -4,18 +4,18 @@ module Rudash
|
|
4
4
|
reducer = rest_args[0]
|
5
5
|
initial_state = rest_args[1]
|
6
6
|
col = collection.is_a?(String) ? collection.split('') : collection
|
7
|
-
|
8
|
-
return self.reduce(collection, -> () { nil })
|
9
|
-
|
7
|
+
|
8
|
+
return self.reduce(collection, -> () { nil }) unless Rudash::Utils.is_function?(reducer)
|
9
|
+
|
10
10
|
case rest_args.size
|
11
11
|
when 1
|
12
|
-
return col.reduce
|
12
|
+
return col.reduce do |acc, current|
|
13
13
|
if col.is_a?(Hash)
|
14
14
|
Rudash::DynamicArgsCount.call(reducer, acc, current[1], current[0])
|
15
15
|
else
|
16
16
|
Rudash::DynamicArgsCount.call(reducer, acc, current)
|
17
17
|
end
|
18
|
-
|
18
|
+
end
|
19
19
|
when 2
|
20
20
|
return col.reduce(initial_state) { |acc, current|
|
21
21
|
if col.is_a?(Hash)
|
data/lib/rudash/reject.rb
CHANGED
@@ -2,10 +2,10 @@ module Rudash
|
|
2
2
|
module Reject
|
3
3
|
def reject(collection, *rest_args)
|
4
4
|
filter = self.head(rest_args) || self.method(:identity)
|
5
|
-
|
5
|
+
|
6
6
|
if filter.is_a?(Hash)
|
7
|
-
slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.(filter)
|
8
|
-
return self.filter(collection, self.negate(slice_matcher))
|
7
|
+
slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.call(filter)
|
8
|
+
return self.filter(collection, self.negate(slice_matcher))
|
9
9
|
elsif Rudash::Utils.is_function?(filter)
|
10
10
|
return self.filter(collection, self.negate(filter))
|
11
11
|
else
|
data/lib/rudash/remove.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Remove
|
3
3
|
def remove(array, *rest_args)
|
4
|
+
return [] unless array.is_a?(Array)
|
5
|
+
|
4
6
|
predicate_fn = self.head(rest_args)
|
5
|
-
return [] if !array.is_a?(Array)
|
6
7
|
removed_items = self.filter(array, predicate_fn)
|
7
|
-
|
8
|
+
|
8
9
|
array.replace(array - removed_items)
|
9
10
|
removed_items
|
10
11
|
end
|
data/lib/rudash/set.rb
CHANGED
@@ -2,19 +2,19 @@ module Rudash
|
|
2
2
|
module Set
|
3
3
|
def set(object, path, value)
|
4
4
|
return object if !object.is_a?(Hash) && !object.is_a?(Array)
|
5
|
-
|
5
|
+
|
6
6
|
resolved_path = Rudash::PathResolver.resolve(path)
|
7
7
|
Rudash::NestedPathCreator.create_path_if_not_exist(object, resolved_path)
|
8
|
-
|
8
|
+
|
9
9
|
*initial_path, last = resolved_path
|
10
|
-
|
10
|
+
|
11
11
|
last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym
|
12
|
-
|
13
|
-
if initial_path.size
|
12
|
+
|
13
|
+
if initial_path.size.zero?
|
14
14
|
object[last_key] = value
|
15
15
|
return object
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
last_parent = self.get(object, initial_path)
|
19
19
|
last_parent[last_key] = value
|
20
20
|
object
|
data/lib/rudash/size.rb
CHANGED
data/lib/rudash/slice.rb
CHANGED
@@ -2,13 +2,13 @@ module Rudash
|
|
2
2
|
module Slice
|
3
3
|
def slice(array, *rest_args)
|
4
4
|
return self.slice(array.split(''), *rest_args) if array.is_a?(String)
|
5
|
-
return []
|
6
|
-
|
5
|
+
return [] unless array.is_a?(Array)
|
6
|
+
|
7
7
|
start_point = rest_args[0] || 0
|
8
8
|
end_point = rest_args[1] || array.size
|
9
|
-
|
10
|
-
return []
|
11
|
-
|
9
|
+
|
10
|
+
return [] unless end_point.is_a?(Numeric)
|
11
|
+
|
12
12
|
array.slice(start_point, end_point - start_point) || []
|
13
13
|
end
|
14
14
|
end
|
data/lib/rudash/some.rb
CHANGED
data/lib/rudash/tail.rb
CHANGED
data/lib/rudash/take.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Take
|
3
3
|
def take(array, *rest_args)
|
4
|
-
return []
|
4
|
+
return [] unless self.is_array?(array)
|
5
|
+
|
5
6
|
count = self.head(rest_args) || 1
|
6
|
-
|
7
|
+
|
7
8
|
begin
|
8
9
|
return array.take(count)
|
9
|
-
rescue
|
10
|
+
rescue ArgumentError
|
10
11
|
return []
|
11
12
|
end
|
12
13
|
end
|
data/lib/rudash/union.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Union
|
3
3
|
def union(*values)
|
4
|
-
|
5
|
-
union_reducer = ->
|
4
|
+
|
5
|
+
union_reducer = ->(acc, current) {
|
6
6
|
return acc if !current.is_a?(Array) || !acc.is_a?(Array)
|
7
|
+
|
7
8
|
acc | current
|
8
9
|
}
|
9
|
-
|
10
|
-
is_array = ->
|
11
|
-
|
10
|
+
|
11
|
+
is_array = ->(value) { value.is_a?(Array) }
|
12
|
+
|
12
13
|
arr_values = self.filter(values, is_array)
|
13
14
|
head = self.head(arr_values)
|
14
|
-
|
15
|
+
|
15
16
|
self.reduce(arr_values, union_reducer, head) || []
|
16
17
|
end
|
17
18
|
end
|
data/lib/rudash/unset.rb
CHANGED
@@ -2,15 +2,15 @@ module Rudash
|
|
2
2
|
module Unset
|
3
3
|
def unset(object, path)
|
4
4
|
return object if !object.is_a?(Hash) && !object.is_a?(Array)
|
5
|
-
|
5
|
+
|
6
6
|
*initial_path, last = Rudash::PathResolver.resolve(path)
|
7
|
-
|
7
|
+
|
8
8
|
last_parent = self.get(object, initial_path)
|
9
|
-
|
9
|
+
|
10
10
|
case last_parent
|
11
11
|
when Array
|
12
|
-
return false
|
13
|
-
|
12
|
+
return false unless Rudash::Utils.match_number?(last)
|
13
|
+
|
14
14
|
last_key = last.to_i
|
15
15
|
if last_key > 0 && last_key < last_parent.length
|
16
16
|
last_parent.delete_at(last_key)
|
data/lib/rudash/update.rb
CHANGED
@@ -2,8 +2,8 @@ module Rudash
|
|
2
2
|
module Update
|
3
3
|
def update(object, path, *rest_args)
|
4
4
|
updater_fn = self.head(rest_args) || self.method(:identity)
|
5
|
-
return object
|
6
|
-
|
5
|
+
return object unless Rudash::Utils.is_function?(updater_fn)
|
6
|
+
|
7
7
|
current_value = self.get(object, path)
|
8
8
|
self.set(object, path, Rudash::DynamicArgsCount.call(updater_fn, current_value))
|
9
9
|
object
|
data/lib/rudash/without.rb
CHANGED
data/lib/rudash.rb
CHANGED
@@ -62,7 +62,7 @@ require_relative './utils/nested_path_creator.rb'
|
|
62
62
|
require_relative './utils/chain_wrapper.rb'
|
63
63
|
|
64
64
|
# This is the exposed Gem class that contains all Rudash methods.
|
65
|
-
# New methods can use already implemented methods
|
65
|
+
# New methods can use already implemented methods by refering to "self"
|
66
66
|
# in the method if and only if the method get extended into the R_ class.
|
67
67
|
class R_
|
68
68
|
extend Rudash::Map
|
data/lib/utils/chain_wrapper.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
module Rudash
|
2
2
|
module ChainUtils
|
3
3
|
class ChainWrapper
|
4
|
+
attr_reader :value
|
5
|
+
|
4
6
|
def initialize(value, r_)
|
5
7
|
@value = value
|
6
8
|
@r_ = r_
|
7
9
|
end
|
8
|
-
|
9
|
-
def
|
10
|
-
@value
|
11
|
-
end
|
12
|
-
|
13
|
-
def method_missing(method_name, *args, &block)
|
10
|
+
|
11
|
+
def method_missing(method_name, *args, &_block)
|
14
12
|
result = @r_.public_send(method_name, @value, *args)
|
15
13
|
self.class.new(result, @r_)
|
16
14
|
rescue NameError
|
@@ -7,13 +7,13 @@
|
|
7
7
|
module Rudash
|
8
8
|
module DynamicArgsCount
|
9
9
|
def self.call(func, *args)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
func.call(*args)
|
11
|
+
|
12
|
+
rescue ArgumentError
|
13
|
+
raise ArgumentError.new('Argument Error') if args.size.zero?
|
14
|
+
|
15
|
+
*initial, _last = args
|
16
|
+
self.call(func, *initial)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/utils/index.rb
CHANGED
@@ -3,14 +3,15 @@
|
|
3
3
|
module Rudash
|
4
4
|
module Utils
|
5
5
|
def self.match_number?(str)
|
6
|
-
return false
|
6
|
+
return false unless str.is_a?(String)
|
7
|
+
|
7
8
|
str.match(/^(\d)+$/)
|
8
9
|
end
|
9
|
-
|
10
|
+
|
10
11
|
def self.is_function?(func)
|
11
12
|
func.is_a?(Proc) || func.is_a?(Method)
|
12
13
|
end
|
13
|
-
|
14
|
+
|
14
15
|
def self.force_reverse(collection)
|
15
16
|
case collection
|
16
17
|
when Array then collection.reverse
|
@@ -10,22 +10,24 @@ module Rudash
|
|
10
10
|
def self.create_path_if_not_exist(object, resolved_path)
|
11
11
|
path = R_.head(resolved_path)
|
12
12
|
return nil if !resolved_path.is_a?(Array) || R_.is_nil?(path)
|
13
|
-
|
13
|
+
|
14
14
|
path_key = Utils.match_number?(path) ? path.to_i : path.to_sym
|
15
15
|
rest_paths = R_.tail(resolved_path)
|
16
16
|
next_path = R_.head(rest_paths)
|
17
17
|
value = R_.get(object, path)
|
18
|
-
|
18
|
+
|
19
19
|
if R_.is_nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
|
20
|
-
# If the next path item is numeric (index)
|
20
|
+
# If the next path item is numeric (index)
|
21
|
+
# then we want to create an array otherwise we create a hash
|
21
22
|
if next_path && Utils.match_number?(next_path)
|
22
23
|
object[path_key] = []
|
23
24
|
else
|
24
25
|
object[path_key] = {}
|
25
26
|
end
|
26
27
|
end
|
27
|
-
|
28
|
-
# Do the same recursively for next path
|
28
|
+
|
29
|
+
# Do the same recursively for next path
|
30
|
+
# until getting to the last path item
|
29
31
|
self.create_path_if_not_exist(
|
30
32
|
R_.get(object, path),
|
31
33
|
rest_paths
|
data/lib/utils/path_resolver.rb
CHANGED
@@ -9,16 +9,16 @@ module Rudash
|
|
9
9
|
module PathResolver
|
10
10
|
def self.resolve(path)
|
11
11
|
normalized_path = path
|
12
|
-
|
12
|
+
|
13
13
|
if normalized_path.is_a?(Array)
|
14
|
-
normalized_path = normalized_path.join('.')
|
14
|
+
normalized_path = normalized_path.join('.')
|
15
15
|
end
|
16
|
-
|
17
|
-
filter_not_empty = ->
|
16
|
+
|
17
|
+
filter_not_empty = ->(value) {
|
18
18
|
value != ''
|
19
19
|
}
|
20
|
-
|
21
|
-
|
20
|
+
|
21
|
+
R_.filter(
|
22
22
|
normalized_path.split(/[.\[\]]/),
|
23
23
|
filter_not_empty
|
24
24
|
)
|
@@ -7,46 +7,46 @@ require_relative '../rudash'
|
|
7
7
|
module Rudash
|
8
8
|
module SubsetDeepMatch
|
9
9
|
def self.subset_deep_match?
|
10
|
-
subset_matcher = ->
|
10
|
+
subset_matcher = ->(slice, collection) {
|
11
11
|
match = true
|
12
|
-
|
12
|
+
|
13
13
|
# If was called with two arrays then the logic will be to
|
14
14
|
# check if every "slice" items exist somehow in the collection
|
15
15
|
# without any order consideration.
|
16
|
-
if
|
17
|
-
return R_.every?(slice, ->
|
18
|
-
R_.some?(collection, ->
|
19
|
-
self.subset_deep_match?.(sliceVal, collectionVal)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
match
|
47
|
-
}
|
48
|
-
|
49
|
-
subset_matcher.curry
|
50
|
-
end
|
51
|
-
end
|
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?.call(sliceVal, collectionVal)
|
20
|
+
})
|
21
|
+
})
|
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?.call(value, collection[key])
|
29
|
+
elsif value.is_a?(Array) && collection[key].is_a?(Array)
|
30
|
+
match &= self.subset_deep_match?.call(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
|
41
|
+
})
|
42
|
+
rescue
|
43
|
+
return false
|
52
44
|
end
|
45
|
+
|
46
|
+
match
|
47
|
+
}
|
48
|
+
|
49
|
+
subset_matcher.curry
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/version.rb
CHANGED