rodash-bi 3.0.1

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 +7 -0
  2. data/lib/rodash-bi.rb +197 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2af0b23175ccc8395ee3db7f8130b16bdda6bf06849febd63e4d22b112a49549
4
+ data.tar.gz: '0105796949a1d1dcfffa2584d6973d15e8ca9d7f3bd6219816e741956b23039d'
5
+ SHA512:
6
+ metadata.gz: bf528b713f72fc81a1fc169dac41a05814d2ea13a4488a76b95d8e4f1f1ccb700b05f5318e818824fac0315755b3058d166c76b3c35b03cfbdedc737d4cf1f32
7
+ data.tar.gz: 2a535d9b0541b498f56a64e5f4f55359bc2524dc91685b1ea60de14fb0cb6601c22c6cb9b369fb834c7fc16ded203ca79bfd2f26a7b91619aa612aed2e748f0e
data/lib/rodash-bi.rb ADDED
@@ -0,0 +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 RodashBi
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 ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rodash-bi
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Victor Fernandez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: _.set, _.get, and _.unset for Ruby
14
+ email: victor@joinbuildit.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rodash-bi.rb
20
+ homepage: https://github.com/victor-fdez/Rodash
21
+ licenses:
22
+ - MIT
23
+ metadata:
24
+ source_code_uri: https://github.com/SINTEF-9012/Rodash
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.4.10
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Rodash
44
+ test_files: []