rudash 2.17.1 → 2.17.2
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 +8 -8
- data/lib/rudash/capitalize.rb +4 -4
- data/lib/rudash/chain.rb +4 -4
- data/lib/rudash/compact.rb +5 -5
- data/lib/rudash/concat.rb +9 -9
- data/lib/rudash/curry.rb +4 -4
- data/lib/rudash/difference.rb +11 -11
- data/lib/rudash/drop_right.rb +9 -9
- data/lib/rudash/each.rb +8 -8
- data/lib/rudash/each_right.rb +10 -10
- data/lib/rudash/eq.rb +4 -4
- data/lib/rudash/every.rb +5 -5
- data/lib/rudash/filter.rb +23 -23
- data/lib/rudash/find.rb +7 -7
- data/lib/rudash/find_last.rb +7 -7
- data/lib/rudash/flip.rb +10 -10
- data/lib/rudash/flow.rb +11 -11
- data/lib/rudash/flow_right.rb +6 -6
- data/lib/rudash/get.rb +22 -22
- data/lib/rudash/group_by.rb +17 -17
- data/lib/rudash/head.rb +8 -8
- data/lib/rudash/identity.rb +4 -4
- data/lib/rudash/initial.rb +6 -6
- data/lib/rudash/intersection.rb +10 -10
- data/lib/rudash/is_array.rb +4 -4
- data/lib/rudash/is_empty.rb +9 -9
- data/lib/rudash/is_equal.rb +4 -4
- data/lib/rudash/is_hash.rb +4 -4
- data/lib/rudash/is_nil.rb +4 -4
- data/lib/rudash/is_number.rb +4 -4
- data/lib/rudash/is_string.rb +4 -4
- data/lib/rudash/join.rb +6 -6
- data/lib/rudash/keys.rb +11 -11
- data/lib/rudash/last.rb +5 -5
- data/lib/rudash/map.rb +19 -19
- data/lib/rudash/negate.rb +8 -8
- data/lib/rudash/pick.rb +16 -16
- data/lib/rudash/range.rb +43 -43
- data/lib/rudash/reduce.rb +29 -29
- data/lib/rudash/reduce_right.rb +5 -5
- data/lib/rudash/reject.rb +13 -13
- data/lib/rudash/remove.rb +9 -9
- data/lib/rudash/reverse.rb +9 -9
- data/lib/rudash/set.rb +20 -20
- data/lib/rudash/size.rb +5 -5
- data/lib/rudash/slice.rb +12 -12
- data/lib/rudash/some.rb +5 -5
- data/lib/rudash/tail.rb +5 -5
- data/lib/rudash/take.rb +11 -11
- data/lib/rudash/union.rb +15 -15
- data/lib/rudash/uniq.rb +11 -11
- data/lib/rudash/unset.rb +30 -30
- data/lib/rudash/update.rb +9 -9
- data/lib/rudash/without.rb +5 -5
- data/lib/rudash.rb +54 -54
- data/lib/utils/chain_wrapper.rb +18 -18
- data/lib/utils/dynamic_args_count.rb +10 -10
- data/lib/utils/index.rb +17 -17
- data/lib/utils/nested_path_creator.rb +24 -24
- data/lib/utils/path_resolver.rb +17 -17
- data/lib/utils/subset_deep_match.rb +41 -41
- data/lib/version.rb +1 -1
- metadata +1 -1
data/lib/rudash/unset.rb
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
module Rudash
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
when Hash
|
22
|
-
last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym
|
23
|
-
if last_parent.key?(last_key)
|
24
|
-
last_parent.delete(last_key)
|
25
|
-
true
|
26
|
-
else
|
27
|
-
false
|
28
|
-
end
|
29
|
-
else
|
30
|
-
false
|
31
|
-
end
|
2
|
+
module Unset
|
3
|
+
def unset(object, path)
|
4
|
+
return object if !object.is_a?(Hash) && !object.is_a?(Array)
|
5
|
+
|
6
|
+
*initial_path, last = Rudash::PathResolver.resolve(path)
|
7
|
+
|
8
|
+
last_parent = self.get(object, initial_path)
|
9
|
+
|
10
|
+
case last_parent
|
11
|
+
when Array
|
12
|
+
return false if !Rudash::Utils.match_number?(last)
|
13
|
+
|
14
|
+
last_key = last.to_i
|
15
|
+
if last_key > 0 && last_key < last_parent.length
|
16
|
+
last_parent.delete_at(last_key)
|
17
|
+
true
|
18
|
+
else
|
19
|
+
false
|
32
20
|
end
|
21
|
+
when Hash
|
22
|
+
last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym
|
23
|
+
if last_parent.key?(last_key)
|
24
|
+
last_parent.delete(last_key)
|
25
|
+
true
|
26
|
+
else
|
27
|
+
false
|
28
|
+
end
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
33
32
|
end
|
33
|
+
end
|
34
34
|
end
|
data/lib/rudash/update.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module Rudash
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
2
|
+
module Update
|
3
|
+
def update(object, path, *rest_args)
|
4
|
+
updater_fn = self.head(rest_args) || self.method(:identity)
|
5
|
+
return object if !Rudash::Utils.is_function?(updater_fn)
|
6
|
+
|
7
|
+
current_value = self.get(object, path)
|
8
|
+
self.set(object, path, Rudash::DynamicArgsCount.call(updater_fn, current_value))
|
9
|
+
object
|
11
10
|
end
|
11
|
+
end
|
12
12
|
end
|
data/lib/rudash/without.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Rudash
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
2
|
+
module Without
|
3
|
+
def without(array, *values)
|
4
|
+
return [] if !array.is_a?(Array)
|
5
|
+
self.difference(array, values)
|
7
6
|
end
|
7
|
+
end
|
8
8
|
end
|
data/lib/rudash.rb
CHANGED
@@ -65,58 +65,58 @@ require_relative './utils/chain_wrapper.rb'
|
|
65
65
|
# New methods can use already implemented methods in the library 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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
68
|
+
extend Rudash::Map
|
69
|
+
extend Rudash::IsNil
|
70
|
+
extend Rudash::Filter
|
71
|
+
extend Rudash::Some
|
72
|
+
extend Rudash::Every
|
73
|
+
extend Rudash::Find
|
74
|
+
extend Rudash::Concat
|
75
|
+
extend Rudash::FindLast
|
76
|
+
extend Rudash::Reduce
|
77
|
+
extend Rudash::ReduceRight
|
78
|
+
extend Rudash::Compact
|
79
|
+
extend Rudash::Head
|
80
|
+
extend Rudash::Last
|
81
|
+
extend Rudash::Tail
|
82
|
+
extend Rudash::Size
|
83
|
+
extend Rudash::Flip
|
84
|
+
extend Rudash::Identity
|
85
|
+
extend Rudash::Get
|
86
|
+
extend Rudash::IsArray
|
87
|
+
extend Rudash::IsEqual
|
88
|
+
extend Rudash::IsHash
|
89
|
+
extend Rudash::IsEmpty
|
90
|
+
extend Rudash::Reverse
|
91
|
+
extend Rudash::Curry
|
92
|
+
extend Rudash::IsNumber
|
93
|
+
extend Rudash::Keys
|
94
|
+
extend Rudash::Each
|
95
|
+
extend Rudash::Uniq
|
96
|
+
extend Rudash::Difference
|
97
|
+
extend Rudash::IsString
|
98
|
+
extend Rudash::Eq
|
99
|
+
extend Rudash::EachRight
|
100
|
+
extend Rudash::At
|
101
|
+
extend Rudash::Negate
|
102
|
+
extend Rudash::Capitalize
|
103
|
+
extend Rudash::Without
|
104
|
+
extend Rudash::Intersection
|
105
|
+
extend Rudash::Join
|
106
|
+
extend Rudash::Initial
|
107
|
+
extend Rudash::Set
|
108
|
+
extend Rudash::Pick
|
109
|
+
extend Rudash::Update
|
110
|
+
extend Rudash::Slice
|
111
|
+
extend Rudash::Remove
|
112
|
+
extend Rudash::Union
|
113
|
+
extend Rudash::Reject
|
114
|
+
extend Rudash::Range
|
115
|
+
extend Rudash::GroupBy
|
116
|
+
extend Rudash::Take
|
117
|
+
extend Rudash::DropRight
|
118
|
+
extend Rudash::Chain
|
119
|
+
extend Rudash::Flow
|
120
|
+
extend Rudash::FlowRight
|
121
|
+
extend Rudash::Unset
|
122
122
|
end
|
data/lib/utils/chain_wrapper.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
module Rudash
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
2
|
+
module ChainUtils
|
3
|
+
class ChainWrapper
|
4
|
+
def initialize(value, r_)
|
5
|
+
@value = value
|
6
|
+
@r_ = r_
|
7
|
+
end
|
8
|
+
|
9
|
+
def value
|
10
|
+
@value
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(method_name, *args, &block)
|
14
|
+
result = @r_.public_send(method_name, @value, *args)
|
15
|
+
self.class.new(result, @r_)
|
16
|
+
rescue NameError
|
17
|
+
raise NameError.new("\"#{method_name}\" doesn't exist in Rudash")
|
18
|
+
end
|
20
19
|
end
|
20
|
+
end
|
21
21
|
end
|
@@ -5,15 +5,15 @@
|
|
5
5
|
# to the developer defined Proc and if it's failed because of ArgumentError we call it recursively with less argument until success.
|
6
6
|
|
7
7
|
module Rudash
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
8
|
+
module DynamicArgsCount
|
9
|
+
def self.call(func, *args)
|
10
|
+
begin
|
11
|
+
return func.(*args)
|
12
|
+
rescue ArgumentError => e
|
13
|
+
raise ArgumentError.new('Argument Error') if args.size == 0
|
14
|
+
*initial, last = args
|
15
|
+
return self.call(func, *initial)
|
16
|
+
end
|
18
17
|
end
|
18
|
+
end
|
19
19
|
end
|
data/lib/utils/index.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
# This module will contain tiny general utilities
|
2
2
|
|
3
3
|
module Rudash
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
module Utils
|
5
|
+
def self.match_number?(str)
|
6
|
+
return false if !str.is_a?(String)
|
7
|
+
str.match(/^(\d)+$/)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.is_function?(func)
|
11
|
+
func.is_a?(Proc) || func.is_a?(Method)
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
14
|
+
def self.force_reverse(collection)
|
15
|
+
case collection
|
16
|
+
when Array then collection.reverse
|
17
|
+
when Hash then collection.reverse_each.to_h
|
18
|
+
when String then collection.reverse
|
19
|
+
else []
|
20
|
+
end
|
22
21
|
end
|
22
|
+
end
|
23
23
|
end
|
@@ -6,30 +6,30 @@ require_relative './index.rb'
|
|
6
6
|
# path ['a', 'b', 'c', 'd'] as embedded hashes. If we have some string that matches a number then we create an array.
|
7
7
|
|
8
8
|
module Rudash
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# Do the same recursively for next path until getting to the last path item
|
29
|
-
self.create_path_if_not_exist(
|
30
|
-
R_.get(object, path),
|
31
|
-
rest_paths
|
32
|
-
)
|
9
|
+
module NestedPathCreator
|
10
|
+
def self.create_path_if_not_exist(object, resolved_path)
|
11
|
+
path = R_.head(resolved_path)
|
12
|
+
return nil if !resolved_path.is_a?(Array) || R_.is_nil?(path)
|
13
|
+
|
14
|
+
path_key = Utils.match_number?(path) ? path.to_i : path.to_sym
|
15
|
+
rest_paths = R_.tail(resolved_path)
|
16
|
+
next_path = R_.head(rest_paths)
|
17
|
+
value = R_.get(object, path)
|
18
|
+
|
19
|
+
if R_.is_nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
|
20
|
+
# If the next path item is numeric (index) then we want to create an array otherwise we create a hash
|
21
|
+
if next_path && Utils.match_number?(next_path)
|
22
|
+
object[path_key] = []
|
23
|
+
else
|
24
|
+
object[path_key] = {}
|
33
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Do the same recursively for next path until getting to the last path item
|
29
|
+
self.create_path_if_not_exist(
|
30
|
+
R_.get(object, path),
|
31
|
+
rest_paths
|
32
|
+
)
|
34
33
|
end
|
34
|
+
end
|
35
35
|
end
|
data/lib/utils/path_resolver.rb
CHANGED
@@ -6,22 +6,22 @@ require_relative '../rudash'
|
|
6
6
|
# to the array shape in order to make the logic related only to one data structure.
|
7
7
|
|
8
8
|
module Rudash
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
9
|
+
module PathResolver
|
10
|
+
def self.resolve(path)
|
11
|
+
normalized_path = path
|
12
|
+
|
13
|
+
if normalized_path.is_a?(Array)
|
14
|
+
normalized_path = normalized_path.join('.')
|
15
|
+
end
|
16
|
+
|
17
|
+
filter_not_empty = -> (value) {
|
18
|
+
value != ''
|
19
|
+
}
|
20
|
+
|
21
|
+
splitted_hash = R_.filter(
|
22
|
+
normalized_path.split(/[.\[\]]/),
|
23
|
+
filter_not_empty
|
24
|
+
)
|
26
25
|
end
|
26
|
+
end
|
27
27
|
end
|
@@ -5,48 +5,48 @@ require_relative '../rudash'
|
|
5
5
|
# See test_filter_hashes_by_deep_hash (test/filter.rb)
|
6
6
|
|
7
7
|
module Rudash
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
+
})
|
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?.(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
|
41
|
+
})
|
42
42
|
rescue
|
43
|
-
|
43
|
+
return false
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
match
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
}
|
48
|
+
|
49
|
+
subset_matcher.curry
|
50
|
+
end
|
51
|
+
end
|
50
52
|
end
|
51
|
-
end
|
52
|
-
end
|
data/lib/version.rb
CHANGED