rudash 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79b36b623652453b46f43dee730eb8772e914bf1649587cc15e75c276c4318a8
4
- data.tar.gz: 0bd55da85472936d9105973ed252b33b46c593a52a8900f536e1eef0ca3ac301
3
+ metadata.gz: dbc4c64e0ac4afd88c14987bf0a9bf423e83f4f16a3692cfd36f755d06cd1674
4
+ data.tar.gz: 2b95666688193596456700bd89d4336332fc2c26a23c311967b33df62fc5af2c
5
5
  SHA512:
6
- metadata.gz: bf85bc6887ca76721aa02889d5cecf7bdeab99ffcc287edfcd4dfe40c49c2a94f0a529d379c6cc34f547894ee743d38c43464ddfa3fa36eedc29fbece5a7748c
7
- data.tar.gz: 26c269cba38c25cc009c25f240d573dce072250341dccfe0f236313bff35057e1bdd6ff0e12854b248c20639eaf2a9ec4ac51b73dd9780856b24b8e7f8cf5bc1
6
+ metadata.gz: 160db19f317177af4d73f6c32556c5b7bf2cf56c81690f0543cfab3dd69b0d300a35a049d9f78add41202eed75afea0c533e1f5db9fbf6face69f4083ba51a4e
7
+ data.tar.gz: 4fddf2dd811af7c28f2aa4d9cca5ae697ce1161dd7b007c1715d14b1b5a18aea6b0f1fbfb5d2e1bc54f5d713134ae851b85eeb1d96918109eabf6678ee7a4fdd
data/lib/rudash/get.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require_relative 'reduce.rb'
2
2
  require_relative 'filter.rb'
3
3
  require_relative '../utils/path_resolver.rb'
4
+ require_relative '../utils/index.rb'
4
5
 
5
6
  module Get
6
7
  extend Reduce
@@ -15,9 +16,9 @@ module Get
15
16
  get_reducer = -> (acc, current) {
16
17
  return nil if acc.nil?
17
18
 
18
- if acc.is_a?(Array) && current.match(/^(\d)+$/)
19
+ if acc.is_a?(Array) && Utils.match_number?(current)
19
20
  acc[current.to_i]
20
- elsif acc.is_a?(Array) && !current.match(/^(\d)+$/)
21
+ elsif acc.is_a?(Array) && !Utils.match_number?(current)
21
22
  nil
22
23
  elsif acc.is_a?(Hash)
23
24
  acc[current.to_sym] || acc[current]
@@ -0,0 +1,26 @@
1
+ require_relative 'each.rb'
2
+ require_relative 'get.rb'
3
+ require_relative 'set.rb'
4
+
5
+ module Pick
6
+ extend Each
7
+ extend Get
8
+ extend Set
9
+
10
+ def pick(hash, paths)
11
+ return self.pick(hash, [paths]) if !paths.is_a?(Array)
12
+ return {} if !hash.is_a?(Hash)
13
+
14
+ picked_hash = {}
15
+
16
+ eacher = -> (path) {
17
+ value = self.get(hash, path)
18
+ if !value.nil?
19
+ self.set(picked_hash, path, value)
20
+ end
21
+ }
22
+
23
+ self.each(paths, eacher)
24
+ picked_hash
25
+ end
26
+ end
data/lib/rudash/set.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative '../utils/path_resolver.rb'
2
2
  require_relative '../utils/nested_path_creator.rb'
3
+ require_relative '../utils/index.rb'
3
4
  require_relative 'get.rb'
4
5
 
5
6
  module Set
@@ -13,7 +14,7 @@ module Set
13
14
 
14
15
  *initial_path, last = resolved_path
15
16
 
16
- last_key = last.match(/^(\d)+$/) ? last.to_i : last.to_sym
17
+ last_key = Utils.match_number?(last) ? last.to_i : last.to_sym
17
18
 
18
19
  if initial_path.size == 0
19
20
  object[last_key] = value
data/lib/rudash.rb CHANGED
@@ -39,6 +39,7 @@ require_relative './rudash/intersection.rb'
39
39
  require_relative './rudash/join.rb'
40
40
  require_relative './rudash/initial.rb'
41
41
  require_relative './rudash/set.rb'
42
+ require_relative './rudash/pick.rb'
42
43
 
43
44
  class R_
44
45
  extend Map
@@ -82,4 +83,5 @@ class R_
82
83
  extend Join
83
84
  extend Initial
84
85
  extend Set
86
+ extend Pick
85
87
  end
@@ -0,0 +1,6 @@
1
+ module Utils
2
+ def self.match_number?(str)
3
+ return false if !str.is_a?(String)
4
+ str.match(/^(\d)+$/)
5
+ end
6
+ end
@@ -1,17 +1,18 @@
1
1
  require_relative '../rudash'
2
+ require_relative './index.rb'
2
3
 
3
4
  module NestedPathCreator
4
5
  def self.create_path_if_not_exist(object, resolved_path)
5
6
  path = R_.head(resolved_path)
6
7
  return nil if !resolved_path.is_a?(Array) || R_.is_nil?(path)
7
8
 
8
- path_key = path.match(/^(\d)+$/) ? path.to_i : path.to_sym
9
+ path_key = Utils.match_number?(path) ? path.to_i : path.to_sym
9
10
  rest_paths = R_.tail(resolved_path)
10
11
  next_path = R_.head(rest_paths)
11
12
  value = R_.get(object, path)
12
13
 
13
14
  if R_.is_nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
14
- if next_path && next_path.match(/^(\d)+$/)
15
+ if next_path && Utils.match_number?(next_path)
15
16
  object[path_key] = []
16
17
  else
17
18
  object[path_key] = {}
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.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Islam Attrash
@@ -49,6 +49,7 @@ files:
49
49
  - lib/rudash/last.rb
50
50
  - lib/rudash/map.rb
51
51
  - lib/rudash/negate.rb
52
+ - lib/rudash/pick.rb
52
53
  - lib/rudash/reduce.rb
53
54
  - lib/rudash/reduce_right.rb
54
55
  - lib/rudash/reverse.rb
@@ -58,6 +59,7 @@ files:
58
59
  - lib/rudash/tail.rb
59
60
  - lib/rudash/uniq.rb
60
61
  - lib/rudash/without.rb
62
+ - lib/utils/index.rb
61
63
  - lib/utils/nested_path_creator.rb
62
64
  - lib/utils/path_resolver.rb
63
65
  homepage: https://github.com/Attrash-Islam/rudash