rodash 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rodash.rb +194 -192
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 065794978a50b2ed6a1d951f656756c9f0b63727
4
- data.tar.gz: dc481f1efadbab5e1a780caf25418e5b83603495
3
+ metadata.gz: 5e98a9cc411fb744966413cf552a6b01f306afb9
4
+ data.tar.gz: 0b3be2881260775de2765a2cea28bbeb8d5dda95
5
5
  SHA512:
6
- metadata.gz: ac4f63f272d6d5ce5040e1090207cc71dfe6941979e7f28a98659a2638a039f354e1f264177e797e715b302adda2e46289ec6a8b8f3df38eb0d2fa4e2e9b3d52
7
- data.tar.gz: c506010baea93b8a3499eaa687ff293fb36fcc7138a3a9c58bfebe4c57a30ca9c14482f02315610bc8a073418eded03d7126aad7f947d24f017c612a56298148
6
+ metadata.gz: bdd7b60b961c28081e536d715057722a772de45b6feac89955df65520bdbb538a9eb7abe5dd05bc4d8cfd2d3f5138a3d253c6b7fb0cc3f79714b8a909859b5c4
7
+ data.tar.gz: 39e07f005de74248c80415d5290666046df9b8c1e85765bffbc508e40645070f0a748d5495f837fc67f190389b09f1f0bd8569a7779967ac338620d230f3491c
data/lib/rodash.rb CHANGED
@@ -1,192 +1,194 @@
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] = newNested = []
109
- elsif not newNested.is_a? Hash
110
- nested[key] = newNested = {}
111
- end
112
- end
113
- nested = newNested
114
- end
115
-
116
- return object
117
- end
118
-
119
- def self.baseGet(object, path)
120
- path = isKey(path, object) ? [path + ''] : baseToPath(path)
121
-
122
- index = 0
123
- length = path.count
124
-
125
- while !object.nil? && index < length
126
- key = path[index]
127
- if object.is_a?(Array)
128
- if isIndex(key)
129
- object = object[key.to_i]
130
- else
131
- return nil
132
- end
133
- elsif object.is_a?(Hash)
134
- object = object[path[index]]
135
- else
136
- return nil
137
- end
138
- index += 1
139
- end
140
-
141
- (index > 0 && index == length) ? object : nil
142
- end
143
-
144
- def self.baseUnset(object, path)
145
- path = isKey(path, object) ? [path + ''] : baseToPath(path)
146
- object = parent(object, path)
147
- key = path.last
148
- if object.is_a?(Array) && isIndex(key)
149
- object[key.to_i] = nil
150
- elsif not object.nil?
151
- object.delete(key)
152
- end
153
- return true
154
- end
155
-
156
- def self.parent(object, path)
157
- path.count == 1 ? object : get(object, path[0 ... -1])
158
- end
159
-
160
- def self.isKey(value, object)
161
- return true if value.is_a? Numeric
162
-
163
- return !value.is_a?(Array) &&
164
- (@@reIsPlainProp =~ value || !@@reIsDeepProp =~ value ||
165
- (!object.nil? && object.has_key?(value)))
166
- end
167
-
168
- def self.baseToPath(value)
169
- value.kind_of?(Array) ? value : stringToPath(value)
170
- end
171
-
172
- def self.stringToPath(string)
173
- result = []
174
- string.to_s.gsub(@@rePropName) do |match|
175
- number = $1
176
- quote = $2
177
- string = $3
178
- result.push((quote && !quote.empty?) ? string.gsub(@@reEscapeChar, '\1') : (number || match))
179
- end
180
-
181
- return result
182
- end
183
-
184
- @@MAX_SAFE_INTEGER = 9007199254740991
185
- @@reIsUint = /^(?:0|[1-9]\d*)$/
186
-
187
- def self.isIndex(value, length = nil)
188
- value = (value.is_a?(Numeric) || @@reIsUint =~ value) ? value.to_i : -1
189
- length = length.nil? ? @@MAX_SAFE_INTEGER : length
190
- return value > -1 && value % 1 == 0 && value < length
191
- end
192
- 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
+ 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
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: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Pultier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-24 00:00:00.000000000 Z
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: _.set, _.get, and _.unset for Ruby
14
14
  email: antoine.pultier@sintef.no