rodash 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rodash.rb +194 -192
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e98a9cc411fb744966413cf552a6b01f306afb9
|
4
|
+
data.tar.gz: 0b3be2881260775de2765a2cea28bbeb8d5dda95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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] =
|
109
|
-
|
110
|
-
nested[key] = newNested
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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:
|
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:
|
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
|