rudash 2.17.4 → 3.0.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/{is_array.rb → array.rb} +1 -1
- data/lib/rudash/drop_right.rb +1 -1
- data/lib/rudash/{is_empty.rb → empty.rb} +1 -1
- data/lib/rudash/{is_equal.rb → equal.rb} +1 -1
- data/lib/rudash/filter.rb +1 -1
- data/lib/rudash/flip.rb +2 -2
- data/lib/rudash/get.rb +1 -1
- data/lib/rudash/{is_hash.rb → hash.rb} +1 -1
- data/lib/rudash/map.rb +3 -3
- data/lib/rudash/negate.rb +1 -1
- data/lib/rudash/{is_nil.rb → nil.rb} +1 -1
- data/lib/rudash/{is_number.rb → number.rb} +1 -1
- data/lib/rudash/reduce.rb +3 -3
- data/lib/rudash/reject.rb +1 -1
- data/lib/rudash/size.rb +1 -1
- data/lib/rudash/{is_string.rb → string.rb} +1 -1
- data/lib/rudash/take.rb +1 -1
- data/lib/rudash/union.rb +0 -1
- data/lib/rudash/update.rb +6 -2
- data/lib/rudash.rb +11 -7
- data/lib/utils/chain_wrapper.rb +5 -5
- data/lib/utils/dynamic_args_count.rb +8 -7
- data/lib/utils/index.rb +1 -1
- data/lib/utils/nested_path_creator.rb +5 -8
- data/lib/utils/path_resolver.rb +4 -5
- data/lib/utils/subset_deep_match.rb +5 -5
- data/lib/version.rb +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 010e99090c5a7822aed08f891f2c1619f0fa7a4375236138cc1964085e56e4c3
|
4
|
+
data.tar.gz: 285bb203659eff8822da94e78e881f0288a5fb64545c8b549208391e085eae33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eb6463f8b89b738c486aa04f4be34388765b2a17a0732a520eb49698d1575380e0484d7886772d5ea229348fef03bfe614ea1de1749db646c4ac4bb7b478460
|
7
|
+
data.tar.gz: cb38153995314e82750e09245286e5c8fdc38b4b2d16fb28f63677f6730160db2a1c908985895683824cc850fb8aa6b3389a8544d7b65295cd186fb112d23ea1
|
data/lib/rudash/drop_right.rb
CHANGED
data/lib/rudash/filter.rb
CHANGED
@@ -8,7 +8,7 @@ module Rudash
|
|
8
8
|
return self.filter(collection, slice_matcher)
|
9
9
|
end
|
10
10
|
|
11
|
-
return [] unless Rudash::Utils.
|
11
|
+
return [] unless Rudash::Utils.function?(predicate_fn)
|
12
12
|
|
13
13
|
if collection.is_a?(Array)
|
14
14
|
return collection.select.with_index do |x, idx|
|
data/lib/rudash/flip.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module Rudash
|
2
2
|
module Flip
|
3
3
|
def flip(a_proc)
|
4
|
-
raise 'Expected a Proc/Method' unless Rudash::Utils.
|
4
|
+
raise 'Expected a Proc/Method' unless Rudash::Utils.function?(a_proc)
|
5
5
|
|
6
6
|
->(*args) {
|
7
7
|
reveresed_args = args.reverse
|
8
8
|
|
9
|
-
a_proc.(*reveresed_args)
|
9
|
+
a_proc.call(*reveresed_args)
|
10
10
|
}
|
11
11
|
end
|
12
12
|
end
|
data/lib/rudash/get.rb
CHANGED
data/lib/rudash/map.rb
CHANGED
@@ -4,12 +4,12 @@ module Rudash
|
|
4
4
|
iteratee_fn = self.head(rest_args) || self.method(:identity)
|
5
5
|
col = collection.is_a?(String) ? collection.split('') : collection
|
6
6
|
|
7
|
-
return self.map(collection, ->
|
7
|
+
return self.map(collection, -> { nil }) unless Rudash::Utils.function?(iteratee_fn)
|
8
8
|
|
9
9
|
if col.is_a?(Array)
|
10
|
-
return col.map.with_index
|
10
|
+
return col.map.with_index do |value, index|
|
11
11
|
Rudash::DynamicArgsCount.call(iteratee_fn, value, index)
|
12
|
-
|
12
|
+
end
|
13
13
|
elsif col.is_a?(Hash)
|
14
14
|
return col.map do |k, v|
|
15
15
|
Rudash::DynamicArgsCount.call(iteratee_fn, v, k)
|
data/lib/rudash/negate.rb
CHANGED
data/lib/rudash/reduce.rb
CHANGED
@@ -5,7 +5,7 @@ module Rudash
|
|
5
5
|
initial_state = rest_args[1]
|
6
6
|
col = collection.is_a?(String) ? collection.split('') : collection
|
7
7
|
|
8
|
-
return self.reduce(collection, ->
|
8
|
+
return self.reduce(collection, -> { nil }) unless Rudash::Utils.function?(reducer)
|
9
9
|
|
10
10
|
case rest_args.size
|
11
11
|
when 1
|
@@ -17,13 +17,13 @@ module Rudash
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
when 2
|
20
|
-
return col.reduce(initial_state)
|
20
|
+
return col.reduce(initial_state) do |acc, current|
|
21
21
|
if col.is_a?(Hash)
|
22
22
|
Rudash::DynamicArgsCount.call(reducer, acc, current[1], current[0])
|
23
23
|
else
|
24
24
|
Rudash::DynamicArgsCount.call(reducer, acc, current)
|
25
25
|
end
|
26
|
-
|
26
|
+
end
|
27
27
|
else
|
28
28
|
return nil
|
29
29
|
end
|
data/lib/rudash/reject.rb
CHANGED
@@ -6,7 +6,7 @@ module Rudash
|
|
6
6
|
if filter.is_a?(Hash)
|
7
7
|
slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.call(filter)
|
8
8
|
return self.filter(collection, self.negate(slice_matcher))
|
9
|
-
elsif Rudash::Utils.
|
9
|
+
elsif Rudash::Utils.function?(filter)
|
10
10
|
return self.filter(collection, self.negate(filter))
|
11
11
|
else
|
12
12
|
return []
|
data/lib/rudash/size.rb
CHANGED
data/lib/rudash/take.rb
CHANGED
data/lib/rudash/union.rb
CHANGED
data/lib/rudash/update.rb
CHANGED
@@ -2,10 +2,14 @@ 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 unless Rudash::Utils.
|
5
|
+
return object unless Rudash::Utils.function?(updater_fn)
|
6
6
|
|
7
7
|
current_value = self.get(object, path)
|
8
|
-
self.set(
|
8
|
+
self.set(
|
9
|
+
object,
|
10
|
+
path,
|
11
|
+
Rudash::DynamicArgsCount.call(updater_fn, current_value)
|
12
|
+
)
|
9
13
|
object
|
10
14
|
end
|
11
15
|
end
|
data/lib/rudash.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require_relative './rudash/map.rb'
|
2
|
-
require_relative './rudash/
|
2
|
+
require_relative './rudash/nil.rb'
|
3
3
|
require_relative './rudash/filter.rb'
|
4
4
|
require_relative './rudash/some.rb'
|
5
5
|
require_relative './rudash/every.rb'
|
@@ -16,18 +16,18 @@ require_relative './rudash/size.rb'
|
|
16
16
|
require_relative './rudash/flip.rb'
|
17
17
|
require_relative './rudash/identity.rb'
|
18
18
|
require_relative './rudash/get.rb'
|
19
|
-
require_relative './rudash/
|
20
|
-
require_relative './rudash/
|
21
|
-
require_relative './rudash/
|
22
|
-
require_relative './rudash/
|
19
|
+
require_relative './rudash/array.rb'
|
20
|
+
require_relative './rudash/equal.rb'
|
21
|
+
require_relative './rudash/hash.rb'
|
22
|
+
require_relative './rudash/empty.rb'
|
23
23
|
require_relative './rudash/reverse.rb'
|
24
24
|
require_relative './rudash/curry.rb'
|
25
|
-
require_relative './rudash/
|
25
|
+
require_relative './rudash/number.rb'
|
26
26
|
require_relative './rudash/keys.rb'
|
27
27
|
require_relative './rudash/each.rb'
|
28
28
|
require_relative './rudash/uniq.rb'
|
29
29
|
require_relative './rudash/difference.rb'
|
30
|
-
require_relative './rudash/
|
30
|
+
require_relative './rudash/string.rb'
|
31
31
|
require_relative './rudash/eq.rb'
|
32
32
|
require_relative './rudash/each_right.rb'
|
33
33
|
require_relative './rudash/at.rb'
|
@@ -64,6 +64,8 @@ require_relative './utils/chain_wrapper.rb'
|
|
64
64
|
# This is the exposed Gem class that contains all Rudash methods.
|
65
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
|
+
|
68
|
+
# rubocop:disable Naming/ClassAndModuleCamelCase
|
67
69
|
class R_
|
68
70
|
extend Rudash::Map
|
69
71
|
extend Rudash::IsNil
|
@@ -120,3 +122,5 @@ class R_
|
|
120
122
|
extend Rudash::FlowRight
|
121
123
|
extend Rudash::Unset
|
122
124
|
end
|
125
|
+
|
126
|
+
# rubocop:enable Naming/ClassAndModuleCamelCase
|
data/lib/utils/chain_wrapper.rb
CHANGED
@@ -3,16 +3,16 @@ module Rudash
|
|
3
3
|
class ChainWrapper
|
4
4
|
attr_reader :value
|
5
5
|
|
6
|
-
def initialize(value,
|
6
|
+
def initialize(value, ru_)
|
7
7
|
@value = value
|
8
|
-
@
|
8
|
+
@ru_ = ru_
|
9
9
|
end
|
10
10
|
|
11
11
|
def method_missing(method_name, *args, &_block)
|
12
|
-
result = @
|
13
|
-
self.class.new(result, @
|
12
|
+
result = @ru_.public_send(method_name, @value, *args)
|
13
|
+
self.class.new(result, @ru_)
|
14
14
|
rescue NameError
|
15
|
-
raise NameError
|
15
|
+
raise NameError, "\"#{method_name}\" doesn't exist in Rudash"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -1,16 +1,17 @@
|
|
1
|
-
# This module was written in order to give the predicate functions for R_.filter, R_.map, R_.each
|
2
|
-
# to be called with any dynamic arguments as it can. What differante JavaScript from Ruby is that
|
3
|
-
# with a specific set of arguments then you must call it with that
|
4
|
-
#
|
5
|
-
#
|
1
|
+
# This module was written in order to give the predicate functions for R_.filter, R_.map, R_.each.
|
2
|
+
# to be called with any dynamic arguments as it can. What differante JavaScript from Ruby is that
|
3
|
+
# once you configured a function with a specific set of arguments then you must call it with that
|
4
|
+
# set or else you will get an exception.
|
5
|
+
# That is so useful for predicate functions that developer define out of the library scope.
|
6
|
+
# We send all the arguments to the developer defined Proc and if it's failed
|
7
|
+
# because of ArgumentError we call it recursively with less argument until success.
|
6
8
|
|
7
9
|
module Rudash
|
8
10
|
module DynamicArgsCount
|
9
11
|
def self.call(func, *args)
|
10
12
|
func.call(*args)
|
11
|
-
|
12
13
|
rescue ArgumentError
|
13
|
-
raise ArgumentError
|
14
|
+
raise ArgumentError, 'Argument Error' if args.size.zero?
|
14
15
|
|
15
16
|
*initial, _last = args
|
16
17
|
self.call(func, *initial)
|
data/lib/utils/index.rb
CHANGED
@@ -3,27 +3,24 @@ require_relative './index.rb'
|
|
3
3
|
|
4
4
|
# This module will create the nested path when using R_.set on not existing paths
|
5
5
|
# If we've a hash as { a: 1 } and we run R_.set(hash, 'a.b.c.d', 5) then we want to create the whole
|
6
|
-
# path ['a', 'b', 'c', 'd'] as embedded hashes.
|
6
|
+
# path ['a', 'b', 'c', 'd'] as embedded hashes.
|
7
|
+
# If we have some string that matches a number then we create an array.
|
7
8
|
|
8
9
|
module Rudash
|
9
10
|
module NestedPathCreator
|
10
11
|
def self.create_path_if_not_exist(object, resolved_path)
|
11
12
|
path = R_.head(resolved_path)
|
12
|
-
return nil if !resolved_path.is_a?(Array) || R_.
|
13
|
+
return nil if !resolved_path.is_a?(Array) || R_.nil?(path)
|
13
14
|
|
14
15
|
path_key = Utils.match_number?(path) ? path.to_i : path.to_sym
|
15
16
|
rest_paths = R_.tail(resolved_path)
|
16
17
|
next_path = R_.head(rest_paths)
|
17
18
|
value = R_.get(object, path)
|
18
19
|
|
19
|
-
if R_.
|
20
|
+
if R_.nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
|
20
21
|
# If the next path item is numeric (index)
|
21
22
|
# then we want to create an array otherwise we create a hash
|
22
|
-
|
23
|
-
object[path_key] = []
|
24
|
-
else
|
25
|
-
object[path_key] = {}
|
26
|
-
end
|
23
|
+
object[path_key] = next_path && Utils.match_number?(next_path) ? [] : {}
|
27
24
|
end
|
28
25
|
|
29
26
|
# Do the same recursively for next path
|
data/lib/utils/path_resolver.rb
CHANGED
@@ -2,17 +2,16 @@ require_relative '../rudash'
|
|
2
2
|
|
3
3
|
# This module have the logic of resolving the paths for R_.get, R_.set and R_.update.
|
4
4
|
# The resolve method will transform the path 'a.b.c.d[0]' to ['a', 'b', 'c', 'd', '0'].
|
5
|
-
# The mentioned functions above can get the path in either way
|
6
|
-
# to the
|
5
|
+
# The mentioned functions above can get the path in either way
|
6
|
+
# but we want to normilize the string path to the array shape in order to
|
7
|
+
# make the logic related only to one data structure.
|
7
8
|
|
8
9
|
module Rudash
|
9
10
|
module PathResolver
|
10
11
|
def self.resolve(path)
|
11
12
|
normalized_path = path
|
12
13
|
|
13
|
-
if normalized_path.is_a?(Array)
|
14
|
-
normalized_path = normalized_path.join('.')
|
15
|
-
end
|
14
|
+
normalized_path = normalized_path.join('.') if normalized_path.is_a?(Array)
|
16
15
|
|
17
16
|
filter_not_empty = ->(value) {
|
18
17
|
value != ''
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative '../rudash'
|
2
2
|
|
3
3
|
# This module was written to supply complex subset deep hash and array matching
|
4
|
-
# in order to give
|
4
|
+
# in order to give filter, some?, every? and find the ability to deep match with complex hash query.
|
5
5
|
# See test_filter_hashes_by_deep_hash (test/filter.rb)
|
6
6
|
|
7
7
|
module Rudash
|
@@ -14,9 +14,9 @@ module Rudash
|
|
14
14
|
# check if every "slice" items exist somehow in the collection
|
15
15
|
# without any order consideration.
|
16
16
|
if slice.is_a?(Array) && collection.is_a?(Array)
|
17
|
-
return R_.every?(slice, ->(
|
18
|
-
R_.some?(collection, ->(
|
19
|
-
self.subset_deep_match?.call(
|
17
|
+
return R_.every?(slice, ->(slice_val) {
|
18
|
+
R_.some?(collection, ->(collection_val) {
|
19
|
+
self.subset_deep_match?.call(slice_val, collection_val)
|
20
20
|
})
|
21
21
|
})
|
22
22
|
end
|
@@ -39,7 +39,7 @@ module Rudash
|
|
39
39
|
# It's the same hack for JavaScript forEach function.
|
40
40
|
raise if match == false
|
41
41
|
})
|
42
|
-
rescue
|
42
|
+
rescue StandardError
|
43
43
|
return false
|
44
44
|
end
|
45
45
|
|
data/lib/version.rb
CHANGED
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:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Islam Attrash
|
@@ -17,6 +17,7 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/rudash.rb
|
20
|
+
- lib/rudash/array.rb
|
20
21
|
- lib/rudash/at.rb
|
21
22
|
- lib/rudash/capitalize.rb
|
22
23
|
- lib/rudash/chain.rb
|
@@ -27,7 +28,9 @@ files:
|
|
27
28
|
- lib/rudash/drop_right.rb
|
28
29
|
- lib/rudash/each.rb
|
29
30
|
- lib/rudash/each_right.rb
|
31
|
+
- lib/rudash/empty.rb
|
30
32
|
- lib/rudash/eq.rb
|
33
|
+
- lib/rudash/equal.rb
|
31
34
|
- lib/rudash/every.rb
|
32
35
|
- lib/rudash/filter.rb
|
33
36
|
- lib/rudash/find.rb
|
@@ -37,22 +40,18 @@ files:
|
|
37
40
|
- lib/rudash/flow_right.rb
|
38
41
|
- lib/rudash/get.rb
|
39
42
|
- lib/rudash/group_by.rb
|
43
|
+
- lib/rudash/hash.rb
|
40
44
|
- lib/rudash/head.rb
|
41
45
|
- lib/rudash/identity.rb
|
42
46
|
- lib/rudash/initial.rb
|
43
47
|
- lib/rudash/intersection.rb
|
44
|
-
- lib/rudash/is_array.rb
|
45
|
-
- lib/rudash/is_empty.rb
|
46
|
-
- lib/rudash/is_equal.rb
|
47
|
-
- lib/rudash/is_hash.rb
|
48
|
-
- lib/rudash/is_nil.rb
|
49
|
-
- lib/rudash/is_number.rb
|
50
|
-
- lib/rudash/is_string.rb
|
51
48
|
- lib/rudash/join.rb
|
52
49
|
- lib/rudash/keys.rb
|
53
50
|
- lib/rudash/last.rb
|
54
51
|
- lib/rudash/map.rb
|
55
52
|
- lib/rudash/negate.rb
|
53
|
+
- lib/rudash/nil.rb
|
54
|
+
- lib/rudash/number.rb
|
56
55
|
- lib/rudash/pick.rb
|
57
56
|
- lib/rudash/range.rb
|
58
57
|
- lib/rudash/reduce.rb
|
@@ -64,6 +63,7 @@ files:
|
|
64
63
|
- lib/rudash/size.rb
|
65
64
|
- lib/rudash/slice.rb
|
66
65
|
- lib/rudash/some.rb
|
66
|
+
- lib/rudash/string.rb
|
67
67
|
- lib/rudash/tail.rb
|
68
68
|
- lib/rudash/take.rb
|
69
69
|
- lib/rudash/union.rb
|