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/rudash/keys.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module Rudash
|
2
|
+
module Keys
|
3
|
+
def keys(value)
|
4
|
+
case value
|
5
|
+
when Hash
|
6
|
+
value.map { |key, value| "#{key}" }
|
7
|
+
when Array
|
8
|
+
value.map.with_index { |value, index| "#{index}" }
|
9
|
+
else
|
10
|
+
[]
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
data/lib/rudash/last.rb
CHANGED
data/lib/rudash/map.rb
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
require_relative 'head.rb'
|
2
2
|
require_relative 'identity.rb'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
4
|
+
module Rudash
|
5
|
+
module Map
|
6
|
+
extend Rudash
|
7
|
+
|
8
|
+
def map(collection, *rest_args)
|
9
|
+
mapper_proc = self.head(rest_args) || self.method(:identity)
|
10
|
+
|
11
|
+
col = collection.is_a?(String) ? collection.split('') : collection
|
12
|
+
|
13
|
+
if col.is_a?(Array)
|
14
|
+
begin
|
15
|
+
return col.map.with_index { |value, index| mapper_proc[value, index] }
|
16
|
+
rescue ArgumentError => e
|
17
|
+
return col.map { |value| mapper_proc[value] }
|
18
|
+
end
|
19
|
+
elsif col.is_a?(Hash)
|
20
|
+
begin
|
21
|
+
return col.map { |k,v| mapper_proc[v, k] }
|
22
|
+
rescue ArgumentError => e
|
23
|
+
return col.map { |k,v| mapper_proc[v] }
|
24
|
+
end
|
25
|
+
else
|
26
|
+
return []
|
24
27
|
end
|
25
|
-
else
|
26
|
-
return []
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
data/lib/rudash/negate.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module Rudash
|
2
|
+
module Negate
|
3
|
+
def negate(a_proc)
|
4
|
+
case a_proc
|
5
|
+
when Proc
|
6
|
+
negate_proc = -> (*args) {
|
7
|
+
!a_proc[*args]
|
8
|
+
}
|
9
|
+
else
|
10
|
+
raise 'Expected a Proc'
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
data/lib/rudash/pick.rb
CHANGED
@@ -2,25 +2,25 @@ require_relative 'each.rb'
|
|
2
2
|
require_relative 'get.rb'
|
3
3
|
require_relative 'set.rb'
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
5
|
+
module Rudash
|
6
|
+
module Pick
|
7
|
+
extend Rudash
|
8
|
+
|
9
|
+
def pick(hash, paths)
|
10
|
+
return self.pick(hash, [paths]) if !paths.is_a?(Array)
|
11
|
+
return {} if !hash.is_a?(Hash)
|
12
|
+
|
13
|
+
picked_hash = {}
|
14
|
+
|
15
|
+
eacher = -> (path) {
|
16
|
+
value = self.get(hash, path)
|
17
|
+
if !value.nil?
|
18
|
+
self.set(picked_hash, path, value)
|
19
|
+
end
|
20
|
+
}
|
21
|
+
|
22
|
+
self.each(paths, eacher)
|
23
|
+
picked_hash
|
24
|
+
end
|
25
25
|
end
|
26
26
|
end
|
data/lib/rudash/reduce.rb
CHANGED
@@ -1,37 +1,39 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
1
|
+
module Rudash
|
2
|
+
module Reduce
|
3
|
+
def reduce(collection, *rest_args)
|
4
|
+
reducer = rest_args[0]
|
5
|
+
initial_state = rest_args[1]
|
6
|
+
|
7
|
+
col = collection.is_a?(String) ? collection.split('') : collection
|
8
|
+
|
9
|
+
case rest_args.size
|
10
|
+
when 1
|
11
|
+
return col.reduce { |acc, current|
|
12
|
+
if col.is_a?(Hash)
|
13
|
+
begin
|
14
|
+
reducer[acc, current[1], current[0]]
|
15
|
+
rescue ArgumentError => e
|
16
|
+
reducer[acc, current[1]]
|
17
|
+
end
|
18
|
+
else
|
19
|
+
reducer[acc, current]
|
16
20
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
reducer[acc, current
|
21
|
+
}
|
22
|
+
when 2
|
23
|
+
return col.reduce(initial_state) { |acc, current|
|
24
|
+
if col.is_a?(Hash)
|
25
|
+
begin
|
26
|
+
reducer[acc, current[1], current[0]]
|
27
|
+
rescue ArgumentError => e
|
28
|
+
reducer[acc, current[1]]
|
29
|
+
end
|
30
|
+
else
|
31
|
+
reducer[acc, current]
|
28
32
|
end
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
else
|
34
|
-
return nil
|
33
|
+
}
|
34
|
+
else
|
35
|
+
return nil
|
36
|
+
end
|
35
37
|
end
|
36
38
|
end
|
37
39
|
end
|
data/lib/rudash/reduce_right.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require_relative 'reduce.rb'
|
2
2
|
require_relative '../utils/index.rb'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module Rudash
|
5
|
+
module ReduceRight
|
6
|
+
extend Rudash
|
7
|
+
|
8
|
+
def reduce_right(collection, *rest_args)
|
9
|
+
reversed_collection = Rudash::Utils.force_reverse(collection)
|
10
|
+
self.reduce(reversed_collection, *rest_args)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
data/lib/rudash/remove.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require_relative 'identity.rb'
|
2
2
|
require_relative 'filter.rb'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4
|
+
module Rudash
|
5
|
+
module Remove
|
6
|
+
extend Rudash
|
7
|
+
|
8
|
+
def remove(array, *rest_args)
|
9
|
+
predicate = self.head(rest_args) || self.method(:identity)
|
10
|
+
return [] if !array.is_a?(Array)
|
11
|
+
removed_items = self.filter(array, predicate)
|
12
|
+
|
13
|
+
array.replace(array - removed_items)
|
14
|
+
removed_items
|
15
|
+
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/rudash/reverse.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module Rudash
|
2
|
+
module Reverse
|
3
|
+
def reverse(value)
|
4
|
+
case value
|
5
|
+
when Array, String
|
6
|
+
value.reverse
|
7
|
+
else
|
8
|
+
value
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/rudash/set.rb
CHANGED
@@ -3,26 +3,28 @@ require_relative '../utils/nested_path_creator.rb'
|
|
3
3
|
require_relative '../utils/index.rb'
|
4
4
|
require_relative 'get.rb'
|
5
5
|
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
6
|
+
module Rudash
|
7
|
+
module Set
|
8
|
+
extend Rudash
|
9
|
+
|
10
|
+
def set(object, path, value)
|
11
|
+
return object if !object.is_a?(Hash) && !object.is_a?(Array)
|
12
|
+
|
13
|
+
resolved_path = Rudash::PathResolver.resolve(path)
|
14
|
+
Rudash::NestedPathCreator.create_path_if_not_exist(object, resolved_path)
|
15
|
+
|
16
|
+
*initial_path, last = resolved_path
|
17
|
+
|
18
|
+
last_key = Rudash::Utils.match_number?(last) ? last.to_i : last.to_sym
|
19
|
+
|
20
|
+
if initial_path.size == 0
|
21
|
+
object[last_key] = value
|
22
|
+
return object
|
23
|
+
end
|
24
|
+
|
25
|
+
last_parent = self.get(object, initial_path)
|
26
|
+
last_parent[last_key] = value
|
27
|
+
object
|
22
28
|
end
|
23
|
-
|
24
|
-
last_parent = self.get(object, initial_path)
|
25
|
-
last_parent[last_key] = value
|
26
|
-
object
|
27
29
|
end
|
28
30
|
end
|
data/lib/rudash/size.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require_relative 'is_nil.rb'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Rudash
|
4
|
+
module Size
|
5
|
+
extend Rudash
|
6
|
+
|
7
|
+
def size(something)
|
8
|
+
return 0 if self.is_nil?(something)
|
9
|
+
something.size
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/rudash/slice.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
module Rudash
|
2
|
+
module Slice
|
3
|
+
def slice(array, *rest_args)
|
4
|
+
return self.slice(array.split(''), *rest_args) if array.is_a?(String)
|
5
|
+
return [] if !array.is_a?(Array)
|
6
|
+
|
7
|
+
start_point = rest_args[0] || 0
|
8
|
+
end_point = rest_args[1] || array.size
|
9
|
+
|
10
|
+
return [] if !end_point.is_a?(Numeric)
|
11
|
+
|
12
|
+
array.slice(start_point, end_point - start_point) || []
|
13
|
+
end
|
12
14
|
end
|
13
15
|
end
|
data/lib/rudash/some.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require_relative 'filter.rb'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Rudash
|
4
|
+
module Some
|
5
|
+
extend Rudash
|
6
|
+
|
7
|
+
def some?(array, filter)
|
8
|
+
filtered_arr = self.filter(array, filter)
|
9
|
+
filtered_arr.length != 0
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/rudash/tail.rb
CHANGED
data/lib/rudash/union.rb
CHANGED
@@ -2,23 +2,23 @@ require_relative 'reduce.rb'
|
|
2
2
|
require_relative 'head.rb'
|
3
3
|
require_relative 'filter.rb'
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
5
|
+
module Rudash
|
6
|
+
module Union
|
7
|
+
extend Rudash
|
8
|
+
|
9
|
+
def union(*values)
|
10
|
+
|
11
|
+
union_reducer = -> (acc, current) {
|
12
|
+
return acc if !current.is_a?(Array) || !acc.is_a?(Array)
|
13
|
+
acc | current
|
14
|
+
}
|
15
|
+
|
16
|
+
is_array = -> (value) { value.is_a?(Array) }
|
17
|
+
|
18
|
+
arr_values = self.filter(values, is_array)
|
19
|
+
head = self.head(arr_values)
|
20
|
+
|
21
|
+
self.reduce(arr_values, union_reducer, head) || []
|
22
|
+
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/rudash/uniq.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module Rudash
|
2
|
+
module Uniq
|
3
|
+
def uniq(value)
|
4
|
+
case value
|
5
|
+
when String
|
6
|
+
self.uniq(value.split(''))
|
7
|
+
when Array
|
8
|
+
value.uniq
|
9
|
+
else
|
10
|
+
[]
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
data/lib/rudash/update.rb
CHANGED
@@ -3,16 +3,15 @@ require_relative 'get.rb'
|
|
3
3
|
require_relative 'set.rb'
|
4
4
|
require_relative 'head.rb'
|
5
5
|
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
object
|
6
|
+
module Rudash
|
7
|
+
module Update
|
8
|
+
extend Rudash
|
9
|
+
|
10
|
+
def update(object, path, *rest_args)
|
11
|
+
updater = self.head(rest_args) || self.method(:identity)
|
12
|
+
current_value = self.get(object, path)
|
13
|
+
self.set(object, path, updater.(current_value))
|
14
|
+
object
|
15
|
+
end
|
17
16
|
end
|
18
17
|
end
|
data/lib/rudash/without.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require_relative 'difference.rb'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Rudash
|
4
|
+
module Without
|
5
|
+
extend Rudash
|
6
|
+
|
7
|
+
def without(array, *values)
|
8
|
+
return [] if !array.is_a?(Array)
|
9
|
+
self.difference(array, values)
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/rudash.rb
CHANGED
@@ -46,50 +46,50 @@ require_relative './rudash/remove.rb'
|
|
46
46
|
require_relative './rudash/union.rb'
|
47
47
|
|
48
48
|
class R_
|
49
|
-
extend Map
|
50
|
-
extend IsNil
|
51
|
-
extend Filter
|
52
|
-
extend Some
|
53
|
-
extend Every
|
54
|
-
extend Find
|
55
|
-
extend Concat
|
56
|
-
extend FindLast
|
57
|
-
extend Reduce
|
58
|
-
extend ReduceRight
|
59
|
-
extend Compact
|
60
|
-
extend Head
|
61
|
-
extend Last
|
62
|
-
extend Tail
|
63
|
-
extend Size
|
64
|
-
extend Flip
|
65
|
-
extend Identity
|
66
|
-
extend Get
|
67
|
-
extend IsArray
|
68
|
-
extend IsEqual
|
69
|
-
extend IsHash
|
70
|
-
extend IsEmpty
|
71
|
-
extend IsProc
|
72
|
-
extend Reverse
|
73
|
-
extend Curry
|
74
|
-
extend IsNumber
|
75
|
-
extend Keys
|
76
|
-
extend Each
|
77
|
-
extend Uniq
|
78
|
-
extend Difference
|
79
|
-
extend IsString
|
80
|
-
extend Eq
|
81
|
-
extend EachRight
|
82
|
-
extend At
|
83
|
-
extend Negate
|
84
|
-
extend Capitalize
|
85
|
-
extend Without
|
86
|
-
extend Intersection
|
87
|
-
extend Join
|
88
|
-
extend Initial
|
89
|
-
extend Set
|
90
|
-
extend Pick
|
91
|
-
extend Update
|
92
|
-
extend Slice
|
93
|
-
extend Remove
|
94
|
-
extend Union
|
49
|
+
extend Rudash::Map
|
50
|
+
extend Rudash::IsNil
|
51
|
+
extend Rudash::Filter
|
52
|
+
extend Rudash::Some
|
53
|
+
extend Rudash::Every
|
54
|
+
extend Rudash::Find
|
55
|
+
extend Rudash::Concat
|
56
|
+
extend Rudash::FindLast
|
57
|
+
extend Rudash::Reduce
|
58
|
+
extend Rudash::ReduceRight
|
59
|
+
extend Rudash::Compact
|
60
|
+
extend Rudash::Head
|
61
|
+
extend Rudash::Last
|
62
|
+
extend Rudash::Tail
|
63
|
+
extend Rudash::Size
|
64
|
+
extend Rudash::Flip
|
65
|
+
extend Rudash::Identity
|
66
|
+
extend Rudash::Get
|
67
|
+
extend Rudash::IsArray
|
68
|
+
extend Rudash::IsEqual
|
69
|
+
extend Rudash::IsHash
|
70
|
+
extend Rudash::IsEmpty
|
71
|
+
extend Rudash::IsProc
|
72
|
+
extend Rudash::Reverse
|
73
|
+
extend Rudash::Curry
|
74
|
+
extend Rudash::IsNumber
|
75
|
+
extend Rudash::Keys
|
76
|
+
extend Rudash::Each
|
77
|
+
extend Rudash::Uniq
|
78
|
+
extend Rudash::Difference
|
79
|
+
extend Rudash::IsString
|
80
|
+
extend Rudash::Eq
|
81
|
+
extend Rudash::EachRight
|
82
|
+
extend Rudash::At
|
83
|
+
extend Rudash::Negate
|
84
|
+
extend Rudash::Capitalize
|
85
|
+
extend Rudash::Without
|
86
|
+
extend Rudash::Intersection
|
87
|
+
extend Rudash::Join
|
88
|
+
extend Rudash::Initial
|
89
|
+
extend Rudash::Set
|
90
|
+
extend Rudash::Pick
|
91
|
+
extend Rudash::Update
|
92
|
+
extend Rudash::Slice
|
93
|
+
extend Rudash::Remove
|
94
|
+
extend Rudash::Union
|
95
95
|
end
|