rodash 1.0.0 → 2.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 +4 -4
  2. data/lib/rodash.rb +56 -14
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab72b44b135255ef2eebb842b453a12bded7ada2
4
- data.tar.gz: 84ead6c3d972c943f5a0d191467067233f0e6a6d
3
+ metadata.gz: 065794978a50b2ed6a1d951f656756c9f0b63727
4
+ data.tar.gz: dc481f1efadbab5e1a780caf25418e5b83603495
5
5
  SHA512:
6
- metadata.gz: 7406aa92397c4eedcdac93b752d2733d8c9460e405849b20293c952727f07d6061771d534f757bc54ec9fcb6e7f95f015a2de33174359d6a81881f2dfd23281e
7
- data.tar.gz: 063179d08dbeb162b8476919952ac4621a9824ec1f3f39fdab242b53f60726a774d3fcf17e4ecaa70e9f4186ee3f473c748492d0d282de55d955fb33b88b7fc0
6
+ metadata.gz: ac4f63f272d6d5ce5040e1090207cc71dfe6941979e7f28a98659a2638a039f354e1f264177e797e715b302adda2e46289ec6a8b8f3df38eb0d2fa4e2e9b3d52
7
+ data.tar.gz: c506010baea93b8a3499eaa687ff293fb36fcc7138a3a9c58bfebe4c57a30ca9c14482f02315610bc8a073418eded03d7126aad7f947d24f017c612a56298148
data/lib/rodash.rb CHANGED
@@ -19,12 +19,12 @@ class Rodash
19
19
  #
20
20
  # object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
21
21
  #
22
- # Rodash.set(object, 'a[0].b.c', 4);
23
- # object['a'][0]['b']['c'];
22
+ # Rodash.set(object, 'a[0].b.c', 4)
23
+ # object['a'][0]['b']['c']
24
24
  # // => 4
25
25
  #
26
- # Rodash.set(object, 'x[0].y.z', 5);
27
- # object['x'][0]['y']['z']);
26
+ # Rodash.set(object, 'x[0].y.z', 5)
27
+ # object['x'][0]['y']['z'])
28
28
  # // => 5
29
29
  #
30
30
  def self.set(object, path, value)
@@ -40,27 +40,58 @@ class Rodash
40
40
  # @returns {*} Returns the resolved value.
41
41
  # @example
42
42
  #
43
- # object = { 'a' => [{ 'b' => { 'c' => 3 } }] };
43
+ # object = { 'a' => [{ 'b' => { 'c' => 3 } }] }
44
44
  #
45
- # Rodash.get(object, 'a[0].b.c');
45
+ # Rodash.get(object, 'a[0].b.c')
46
46
  # // => 3
47
47
  #
48
- # Rodash.get(object, ['a', '0', 'b', 'c']);
48
+ # Rodash.get(object, ['a', '0', 'b', 'c'])
49
49
  # // => 3
50
50
  #
51
- # Rodash.get(object, 'a.b.c', 'default');
51
+ # Rodash.get(object, 'a.b.c', 'default')
52
52
  # // => 'default'
53
53
  def self.get(object, path, defaultValue = nil)
54
54
  result = object.nil? ? nil : baseGet(object, path)
55
55
  result.nil? ? defaultValue : result
56
56
  end
57
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
+
58
84
  protected
59
85
 
86
+ @@reIsDeepProp = /\.|\[(?:[^\[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/
87
+ @@reIsPlainProp = /^\w*$/
88
+ @@rePropName = /[^.\[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]/
89
+ @@reEscapeChar = /\\(\\)?/
90
+
60
91
  def self.baseSet(object, path, value, customizer = false)
61
92
  throw "object must be a hash" if not object.is_a? Hash
62
93
 
63
- path = isKey(path, object) ? [path + ''] : baseToPath(path);
94
+ path = isKey(path, object) ? [path + ''] : baseToPath(path)
64
95
 
65
96
  nested = object
66
97
  newNested = nil
@@ -86,7 +117,7 @@ class Rodash
86
117
  end
87
118
 
88
119
  def self.baseGet(object, path)
89
- path = isKey(path, object) ? [path + ''] : baseToPath(path);
120
+ path = isKey(path, object) ? [path + ''] : baseToPath(path)
90
121
 
91
122
  index = 0
92
123
  length = path.count
@@ -110,10 +141,21 @@ class Rodash
110
141
  (index > 0 && index == length) ? object : nil
111
142
  end
112
143
 
113
- @@reIsDeepProp = /\.|\[(?:[^\[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/
114
- @@reIsPlainProp = /^\w*$/
115
- @@rePropName = /[^.\[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]/
116
- @@reEscapeChar = /\\(\\)?/
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
117
159
 
118
160
  def self.isKey(value, object)
119
161
  return true if value.is_a? Numeric
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.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-23 00:00:00.000000000 Z
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: _.set and _.get for Ruby
13
+ description: _.set, _.get, and _.unset for Ruby
14
14
  email: antoine.pultier@sintef.no
15
15
  executables: []
16
16
  extensions: []