rudash 2.1.0 → 2.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1185187b5c978193c2c50118c55fb0dd5e09dc149cfddd3dc859cc130afd84df
4
- data.tar.gz: 0368d04fb8feab44c597596efd6739e021a4853e4c464188cba7cfdad8829873
3
+ metadata.gz: 79b36b623652453b46f43dee730eb8772e914bf1649587cc15e75c276c4318a8
4
+ data.tar.gz: 0bd55da85472936d9105973ed252b33b46c593a52a8900f536e1eef0ca3ac301
5
5
  SHA512:
6
- metadata.gz: ad84f156d45283ea1cac6d3f7d24a069b25d8468da8d465d8d156d232771df1d77a72b4fdb3fbdc8fb597e33e2e7ab3f42078e367b572d9c2d27ba5e1df4bb91
7
- data.tar.gz: 750eefc1c8f0bc1f5510036c9f9a29a2033c3aed16f021b0ee1d67dd4149db9af8256b1ed6e32e11954c01a11c4d11b057496546daf64679c34dc4cd385ce4b4
6
+ metadata.gz: bf85bc6887ca76721aa02889d5cecf7bdeab99ffcc287edfcd4dfe40c49c2a94f0a529d379c6cc34f547894ee743d38c43464ddfa3fa36eedc29fbece5a7748c
7
+ data.tar.gz: 26c269cba38c25cc009c25f240d573dce072250341dccfe0f236313bff35057e1bdd6ff0e12854b248c20639eaf2a9ec4ac51b73dd9780856b24b8e7f8cf5bc1
data/lib/rudash/filter.rb CHANGED
@@ -10,7 +10,7 @@ module Filter
10
10
  def filter(collection, *rest_args)
11
11
  filter = self.head(rest_args) || self.method(:identity)
12
12
 
13
- if collection.is_a?(Array) and filter.is_a?(Hash)
13
+ if collection.is_a?(Array) && filter.is_a?(Hash)
14
14
  filtered_arr = []
15
15
  collection.each do |v|
16
16
  match = true
data/lib/rudash/get.rb CHANGED
@@ -1,39 +1,23 @@
1
1
  require_relative 'reduce.rb'
2
2
  require_relative 'filter.rb'
3
+ require_relative '../utils/path_resolver.rb'
3
4
 
4
5
  module Get
5
6
  extend Reduce
6
7
  extend Filter
7
8
 
8
9
  def get(hash, path, *rest_args)
9
- return nil if !path.is_a?(String) and !path.is_a?(Array)
10
- return nil if !hash.is_a?(Array) and !hash.is_a?(Hash)
10
+ return nil if !path.is_a?(String) && !path.is_a?(Array)
11
+ return nil if !hash.is_a?(Array) && !hash.is_a?(Hash)
11
12
 
12
- transform_path = -> (path) {
13
- normalized_path = path
14
-
15
- if normalized_path.is_a?(Array)
16
- normalized_path = normalized_path.join('.')
17
- end
18
-
19
- filter_not_empty = -> (value) {
20
- value != ''
21
- }
22
-
23
- splitted_hash = self.filter(
24
- normalized_path.split(/[.\[\]]/),
25
- filter_not_empty
26
- )
27
- }
28
-
29
- splitted_hash = transform_path[path]
13
+ resolved_path = PathResolver.resolve(path)
30
14
 
31
15
  get_reducer = -> (acc, current) {
32
16
  return nil if acc.nil?
33
17
 
34
- if acc.is_a?(Array) and current.match(/^(\d)+$/)
18
+ if acc.is_a?(Array) && current.match(/^(\d)+$/)
35
19
  acc[current.to_i]
36
- elsif acc.is_a?(Array) and !current.match(/^(\d)+$/)
20
+ elsif acc.is_a?(Array) && !current.match(/^(\d)+$/)
37
21
  nil
38
22
  elsif acc.is_a?(Hash)
39
23
  acc[current.to_sym] || acc[current]
@@ -42,6 +26,6 @@ module Get
42
26
  end
43
27
  }
44
28
 
45
- self.reduce(splitted_hash, get_reducer, hash)
29
+ self.reduce(resolved_path, get_reducer, hash)
46
30
  end
47
31
  end
@@ -0,0 +1,7 @@
1
+ module Initial
2
+ def initial(array)
3
+ return [] if !array.is_a?(Array)
4
+ *initial, last = array
5
+ initial
6
+ end
7
+ end
@@ -6,7 +6,7 @@ module Intersection
6
6
  def intersection(*values)
7
7
 
8
8
  intersection_reducer = -> (acc, current) {
9
- return [] if !current.is_a?(Array) or !acc.is_a?(Array)
9
+ return [] if !current.is_a?(Array) || !acc.is_a?(Array)
10
10
  acc & current
11
11
  }
12
12
 
data/lib/rudash/set.rb ADDED
@@ -0,0 +1,27 @@
1
+ require_relative '../utils/path_resolver.rb'
2
+ require_relative '../utils/nested_path_creator.rb'
3
+ require_relative 'get.rb'
4
+
5
+ module Set
6
+ extend Get
7
+
8
+ def set(object, path, value)
9
+ return object if !object.is_a?(Hash) && !object.is_a?(Array)
10
+
11
+ resolved_path = PathResolver.resolve(path)
12
+ NestedPathCreator.create_path_if_not_exist(object, resolved_path)
13
+
14
+ *initial_path, last = resolved_path
15
+
16
+ last_key = last.match(/^(\d)+$/) ? last.to_i : last.to_sym
17
+
18
+ if initial_path.size == 0
19
+ object[last_key] = value
20
+ return object
21
+ end
22
+
23
+ last_parent = self.get(object, initial_path)
24
+ last_parent[last_key] = value
25
+ object
26
+ end
27
+ end
data/lib/rudash.rb CHANGED
@@ -37,6 +37,8 @@ require_relative './rudash/capitalize.rb'
37
37
  require_relative './rudash/without.rb'
38
38
  require_relative './rudash/intersection.rb'
39
39
  require_relative './rudash/join.rb'
40
+ require_relative './rudash/initial.rb'
41
+ require_relative './rudash/set.rb'
40
42
 
41
43
  class R_
42
44
  extend Map
@@ -78,4 +80,6 @@ class R_
78
80
  extend Without
79
81
  extend Intersection
80
82
  extend Join
83
+ extend Initial
84
+ extend Set
81
85
  end
@@ -0,0 +1,26 @@
1
+ require_relative '../rudash'
2
+
3
+ module NestedPathCreator
4
+ def self.create_path_if_not_exist(object, resolved_path)
5
+ path = R_.head(resolved_path)
6
+ return nil if !resolved_path.is_a?(Array) || R_.is_nil?(path)
7
+
8
+ path_key = path.match(/^(\d)+$/) ? path.to_i : path.to_sym
9
+ rest_paths = R_.tail(resolved_path)
10
+ next_path = R_.head(rest_paths)
11
+ value = R_.get(object, path)
12
+
13
+ if R_.is_nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
14
+ if next_path && next_path.match(/^(\d)+$/)
15
+ object[path_key] = []
16
+ else
17
+ object[path_key] = {}
18
+ end
19
+ end
20
+
21
+ self.create_path_if_not_exist(
22
+ R_.get(object, path),
23
+ rest_paths
24
+ )
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../rudash'
2
+
3
+ module PathResolver
4
+ def self.resolve(path)
5
+ normalized_path = path
6
+
7
+ if normalized_path.is_a?(Array)
8
+ normalized_path = normalized_path.join('.')
9
+ 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
+ end
20
+ end
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: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Islam Attrash
@@ -34,6 +34,7 @@ files:
34
34
  - lib/rudash/get.rb
35
35
  - lib/rudash/head.rb
36
36
  - lib/rudash/identity.rb
37
+ - lib/rudash/initial.rb
37
38
  - lib/rudash/intersection.rb
38
39
  - lib/rudash/is_array.rb
39
40
  - lib/rudash/is_empty.rb
@@ -51,11 +52,14 @@ files:
51
52
  - lib/rudash/reduce.rb
52
53
  - lib/rudash/reduce_right.rb
53
54
  - lib/rudash/reverse.rb
55
+ - lib/rudash/set.rb
54
56
  - lib/rudash/size.rb
55
57
  - lib/rudash/some.rb
56
58
  - lib/rudash/tail.rb
57
59
  - lib/rudash/uniq.rb
58
60
  - lib/rudash/without.rb
61
+ - lib/utils/nested_path_creator.rb
62
+ - lib/utils/path_resolver.rb
59
63
  homepage: https://github.com/Attrash-Islam/rudash
60
64
  licenses:
61
65
  - MIT