rodash 3.0.0 → 4.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.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/rodash.rb +197 -194
  3. metadata +6 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5e98a9cc411fb744966413cf552a6b01f306afb9
4
- data.tar.gz: 0b3be2881260775de2765a2cea28bbeb8d5dda95
2
+ SHA256:
3
+ metadata.gz: bbb69e1ae3e796ebf133983a5bad41414f4e2178e4e7e364480aefd8849f21fe
4
+ data.tar.gz: 62df9d3a934288dbe0b5e9fefcbcd3db94dadf503a63be7206ba9180739abef1
5
5
  SHA512:
6
- metadata.gz: bdd7b60b961c28081e536d715057722a772de45b6feac89955df65520bdbb538a9eb7abe5dd05bc4d8cfd2d3f5138a3d253c6b7fb0cc3f79714b8a909859b5c4
7
- data.tar.gz: 39e07f005de74248c80415d5290666046df9b8c1e85765bffbc508e40645070f0a748d5495f837fc67f190389b09f1f0bd8569a7779967ac338620d230f3491c
6
+ metadata.gz: 1b73b37cb3fe80006f9a0ac5c263a3f506f73879de09f7ae4e0f02044153487324e4ecc51ad49fdf1f32167058409aa3eee4e5439f0db46602fbbfa363efba5a
7
+ data.tar.gz: b26b0a476f722cb6a321df96fbd318dec71d3525e0e5d966a2ec2fca66e16ce07acd4ba72735e6a1c84d62f8e6b62ad3aeea9ff1ed3440b9502e8650de2ffaf3
data/lib/rodash.rb CHANGED
@@ -1,194 +1,197 @@
1
- # Rodash 1.0.0
2
- # Rodash.get and Rodash.set
3
- # MIT License
4
- # Based on Lodash.js 4.3.0 <https://lodash.com/>
5
-
6
- class Rodash
7
-
8
- # Sets the value at `path` of `object`. If a portion of `path` doesn't exist
9
- # it's created. Arrays are created for missing index properties while objects
10
- # are created for all other missing properties.
11
- #
12
- # **Note:** This method mutates `object`.
13
- #
14
- # @param {Hash} object The object to modify.
15
- # @param {Array|string} path The path of the property to set.
16
- # @param {*} value The value to set.
17
- # @returns {Hash} Returns `object`.
18
- # @example
19
- #
20
- # object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
21
- #
22
- # Rodash.set(object, 'a[0].b.c', 4)
23
- # object['a'][0]['b']['c']
24
- # // => 4
25
- #
26
- # Rodash.set(object, 'x[0].y.z', 5)
27
- # object['x'][0]['y']['z'])
28
- # // => 5
29
- #
30
- def self.set(object, path, value)
31
- object.nil? ? object : baseSet(object, path, value)
32
- end
33
-
34
- # Gets the value at `path` of `object`. If the resolved value is
35
- # `nil` the `defaultValue` is used in its place.
36
- #
37
- # @param {Hash|Array} object The object to query.
38
- # @param {Array|string} path The path of the property to get.
39
- # @param {*} [defaultValue] The value returned if the resolved value is `nil`.
40
- # @returns {*} Returns the resolved value.
41
- # @example
42
- #
43
- # object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
44
- #
45
- # Rodash.get(object, 'a[0].b.c')
46
- # // => 3
47
- #
48
- # Rodash.get(object, ['a', '0', 'b', 'c'])
49
- # // => 3
50
- #
51
- # Rodash.get(object, 'a.b.c', 'default')
52
- # // => 'default'
53
- def self.get(object, path, defaultValue = nil)
54
- result = object.nil? ? nil : baseGet(object, path)
55
- result.nil? ? defaultValue : result
56
- end
57
-
58
- # Removes the property at `path` of `object`.
59
- #
60
- # **Note:** This method mutates `object`.
61
- #
62
- # @param {Hash} object The object to modify.
63
- # @param {Array|string} path The path of the property to unset.
64
- # @returns {boolean} Returns `true` if the property is deleted, else `false`.
65
- # @example
66
- #
67
- # object = { 'a' => [{ 'b' => { 'c' => 7 } }] }
68
- # Rodash.unset(object, 'a[0].b.c')
69
- # // => true
70
- #
71
- # object
72
- # // => { 'a' => [{ 'b' => {} }] }
73
- #
74
- # Rodash.unset(object, 'a[0].b.c')
75
- # // => true
76
- #
77
- # object
78
- # // => { 'a' => [{ 'b' => {} }] }
79
- #
80
- def self.unset(object, path)
81
- object.nil? ? true : baseUnset(object, path)
82
- end
83
-
84
- protected
85
-
86
- @@reIsDeepProp = /\.|\[(?:[^\[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/
87
- @@reIsPlainProp = /^\w*$/
88
- @@rePropName = /[^.\[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]/
89
- @@reEscapeChar = /\\(\\)?/
90
-
91
- def self.baseSet(object, path, value, customizer = false)
92
- throw "object must be a hash" if not object.is_a? Hash
93
-
94
- path = isKey(path, object) ? [path + ''] : baseToPath(path)
95
-
96
- nested = object
97
- newNested = nil
98
-
99
- [*path, nil].each_cons(2) do |key, nextkey|
100
- if isIndex(key) && nested.is_a?(Array)
101
- key = key.to_i
102
- end
103
- if nextkey.nil?
104
- newNested = nested[key] = value
105
- else
106
- newNested = nested[key]
107
- if isIndex(nextkey) && (!newNested.is_a?(Hash) || !newNested.is_a(Array))
108
- nested[key] = []
109
- newNested = [] if newNested == nil
110
- nested[key] = newNested if newNested != nil
111
- elsif not newNested.is_a? Hash
112
- nested[key] = {}
113
- newNested = {} if newNested == nil
114
- nested[key] = newNested if newNested != nil
115
- end
116
- end
117
- nested = newNested
118
- end
119
- return object
120
- end
121
-
122
- def self.baseGet(object, path)
123
- path = isKey(path, object) ? [path + ''] : baseToPath(path)
124
-
125
- index = 0
126
- length = path.count
127
-
128
- while !object.nil? && index < length
129
- key = path[index]
130
- if object.is_a?(Array)
131
- if isIndex(key)
132
- object = object[key.to_i]
133
- else
134
- return nil
135
- end
136
- elsif object.is_a?(Hash)
137
- object = object[path[index]]
138
- else
139
- return nil
140
- end
141
- index += 1
142
- end
143
-
144
- (index > 0 && index == length) ? object : nil
145
- end
146
-
147
- def self.baseUnset(object, path)
148
- path = isKey(path, object) ? [path + ''] : baseToPath(path)
149
- object = parent(object, path)
150
- key = path.last
151
- if object.is_a?(Array) && isIndex(key)
152
- object[key.to_i] = nil
153
- elsif not object.nil?
154
- object.delete(key)
155
- end
156
- return true
157
- end
158
-
159
- def self.parent(object, path)
160
- path.count == 1 ? object : get(object, path[0 ... -1])
161
- end
162
-
163
- def self.isKey(value, object)
164
- return true if value.is_a? Numeric
165
- return !value.is_a?(Array) &&
166
- (@@reIsPlainProp =~ value || !@@reIsDeepProp =~ value ||
167
- (!object.nil? && object.has_key?(value)))
168
- end
169
-
170
- def self.baseToPath(value)
171
- value.kind_of?(Array) ? value : stringToPath(value)
172
- end
173
-
174
- def self.stringToPath(string)
175
- result = []
176
- string.to_s.gsub(@@rePropName) do |match|
177
- number = $1
178
- quote = $2
179
- string = $3
180
- result.push((quote && !quote.empty?) ? string.gsub(@@reEscapeChar, '\1') : (number || match))
181
- end
182
-
183
- return result
184
- end
185
-
186
- @@MAX_SAFE_INTEGER = 9007199254740991
187
- @@reIsUint = /^(?:0|[1-9]\d*)$/
188
-
189
- def self.isIndex(value, length = nil)
190
- value = (value.is_a?(Numeric) || @@reIsUint =~ value) ? value.to_i : -1
191
- length = length.nil? ? @@MAX_SAFE_INTEGER : length
192
- return value > -1 && value % 1 == 0 && value < length
193
- end
194
- end
1
+ # Rodash 1.0.0
2
+ # Rodash.get and Rodash.set
3
+ # MIT License
4
+ # Based on Lodash.js 4.3.0 <https://lodash.com/>
5
+
6
+ class Rodash
7
+
8
+ # Sets the value at `path` of `object`. If a portion of `path` doesn't exist
9
+ # it's created. Arrays are created for missing index properties while objects
10
+ # are created for all other missing properties.
11
+ #
12
+ # **Note:** This method mutates `object`.
13
+ #
14
+ # @param {Hash} object The object to modify.
15
+ # @param {Array|string} path The path of the property to set.
16
+ # @param {*} value The value to set.
17
+ # @returns {Hash} Returns `object`.
18
+ # @example
19
+ #
20
+ # object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
21
+ #
22
+ # Rodash.set(object, 'a[0].b.c', 4)
23
+ # object['a'][0]['b']['c']
24
+ # // => 4
25
+ #
26
+ # Rodash.set(object, 'x[0].y.z', 5)
27
+ # object['x'][0]['y']['z'])
28
+ # // => 5
29
+ #
30
+ def self.set(object, path, value)
31
+ object.nil? ? object : baseSet(object, path, value)
32
+ end
33
+
34
+ # Gets the value at `path` of `object`. If the resolved value is
35
+ # `nil` the `defaultValue` is used in its place.
36
+ #
37
+ # @param {Hash|Array} object The object to query.
38
+ # @param {Array|string} path The path of the property to get.
39
+ # @param {*} [defaultValue] The value returned if the resolved value is `nil`.
40
+ # @returns {*} Returns the resolved value.
41
+ # @example
42
+ #
43
+ # object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
44
+ #
45
+ # Rodash.get(object, 'a[0].b.c')
46
+ # // => 3
47
+ #
48
+ # Rodash.get(object, ['a', '0', 'b', 'c'])
49
+ # // => 3
50
+ #
51
+ # Rodash.get(object, 'a.b.c', 'default')
52
+ # // => 'default'
53
+ def self.get(object, path, defaultValue = nil)
54
+ result = object.nil? ? nil : baseGet(object, path)
55
+ result.nil? ? defaultValue : result
56
+ end
57
+
58
+ # Removes the property at `path` of `object`.
59
+ #
60
+ # **Note:** This method mutates `object`.
61
+ #
62
+ # @param {Hash} object The object to modify.
63
+ # @param {Array|string} path The path of the property to unset.
64
+ # @returns {boolean} Returns `true` if the property is deleted, else `false`.
65
+ # @example
66
+ #
67
+ # object = { 'a' => [{ 'b' => { 'c' => 7 } }] }
68
+ # Rodash.unset(object, 'a[0].b.c')
69
+ # // => true
70
+ #
71
+ # object
72
+ # // => { 'a' => [{ 'b' => {} }] }
73
+ #
74
+ # Rodash.unset(object, 'a[0].b.c')
75
+ # // => true
76
+ #
77
+ # object
78
+ # // => { 'a' => [{ 'b' => {} }] }
79
+ #
80
+ def self.unset(object, path)
81
+ object.nil? ? true : baseUnset(object, path)
82
+ end
83
+
84
+ protected
85
+
86
+ @@reIsDeepProp = /\.|\[(?:[^\[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/
87
+ @@reIsPlainProp = /^\w*$/
88
+ @@rePropName = /[^.\[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]/
89
+ @@reEscapeChar = /\\(\\)?/
90
+
91
+ def self.baseSet(object, path, value, customizer = false)
92
+ throw "object must be a hash" if not object.is_a? Hash
93
+
94
+ path = isKey(path, object) ? [path + ''] : baseToPath(path)
95
+
96
+ nested = object
97
+ newNested = nil
98
+
99
+ [*path, nil].each_cons(2) do |key, nextkey|
100
+ if isIndex(key) && nested.is_a?(Array)
101
+ key = key.to_i
102
+ end
103
+ if nextkey.nil?
104
+ newNested = nested[key] = value
105
+ else
106
+ newNested = nested[key]
107
+ if isIndex(nextkey) && (!newNested.is_a?(Hash) || !newNested.is_a(Array))
108
+ nested[key] = []
109
+ newNested = [] if newNested == nil
110
+ nested[key] = newNested if newNested != nil
111
+ elsif not newNested.is_a? Hash
112
+ nested[key] = {}
113
+ newNested = {} if newNested == nil
114
+ nested[key] = newNested if newNested != nil
115
+ end
116
+ end
117
+ nested = newNested
118
+ end
119
+ return object
120
+ end
121
+
122
+ def self.baseGet(object, path)
123
+ path = isKey(path, object) ? [path + ''] : baseToPath(path)
124
+
125
+ index = 0
126
+ length = path.count
127
+
128
+ while !object.nil? && index < length
129
+ key = path[index]
130
+ if object.is_a?(Array)
131
+ if isIndex(key)
132
+ object = object[key.to_i]
133
+ else
134
+ return nil
135
+ end
136
+ elsif object.is_a?(Hash)
137
+ object = object[path[index]]
138
+ else
139
+ return nil
140
+ end
141
+ index += 1
142
+ end
143
+
144
+ (index > 0 && index == length) ? object : nil
145
+ end
146
+
147
+ def self.baseUnset(object, path)
148
+ path = isKey(path, object) ? [path + ''] : baseToPath(path)
149
+ object = parent(object, path)
150
+ key = path.last
151
+ if object.is_a?(Array) && isIndex(key)
152
+ object[key.to_i] = nil
153
+ elsif not object.nil?
154
+ object.delete(key)
155
+ end
156
+ return true
157
+ end
158
+
159
+ def self.parent(object, path)
160
+ path.count == 1 ? object : get(object, path[0 ... -1])
161
+ end
162
+
163
+ def self.isKey(value, object)
164
+ return true if value.is_a? Numeric
165
+
166
+ return false if value.is_a?(Array)
167
+
168
+ @@reIsPlainProp.match?(value) ||
169
+ !@@reIsDeepProp.match?(value) ||
170
+ (object && !object.is_a?(Array) && object.has_key?(value))
171
+ end
172
+
173
+ def self.baseToPath(value)
174
+ value.kind_of?(Array) ? value : stringToPath(value)
175
+ end
176
+
177
+ def self.stringToPath(string)
178
+ result = []
179
+ string.to_s.gsub(@@rePropName) do |match|
180
+ number = $1
181
+ quote = $2
182
+ string = $3
183
+ result.push((quote && !quote.empty?) ? string.gsub(@@reEscapeChar, '\1') : (number || match))
184
+ end
185
+
186
+ return result
187
+ end
188
+
189
+ @@MAX_SAFE_INTEGER = 9007199254740991
190
+ @@reIsUint = /^(?:0|[1-9]\d*)$/
191
+
192
+ def self.isIndex(value, length = nil)
193
+ value = (value.is_a?(Numeric) || @@reIsUint =~ value) ? value.to_i : -1
194
+ length = length.nil? ? @@MAX_SAFE_INTEGER : length
195
+ return value > -1 && value % 1 == 0 && value < length
196
+ end
197
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodash
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Pultier
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-10 00:00:00.000000000 Z
11
+ date: 2024-01-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: _.set, _.get, and _.unset for Ruby
14
14
  email: antoine.pultier@sintef.no
@@ -21,7 +21,7 @@ homepage: https://github.com/SINTEF-9012/Rodash
21
21
  licenses:
22
22
  - MIT
23
23
  metadata: {}
24
- post_install_message:
24
+ post_install_message:
25
25
  rdoc_options: []
26
26
  require_paths:
27
27
  - lib
@@ -36,9 +36,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
38
  requirements: []
39
- rubyforge_project:
40
- rubygems_version: 2.4.8
41
- signing_key:
39
+ rubygems_version: 3.5.3
40
+ signing_key:
42
41
  specification_version: 4
43
42
  summary: Rodash
44
43
  test_files: []