rodash 1.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 +7 -0
  2. data/lib/rodash.rb +150 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab72b44b135255ef2eebb842b453a12bded7ada2
4
+ data.tar.gz: 84ead6c3d972c943f5a0d191467067233f0e6a6d
5
+ SHA512:
6
+ metadata.gz: 7406aa92397c4eedcdac93b752d2733d8c9460e405849b20293c952727f07d6061771d534f757bc54ec9fcb6e7f95f015a2de33174359d6a81881f2dfd23281e
7
+ data.tar.gz: 063179d08dbeb162b8476919952ac4621a9824ec1f3f39fdab242b53f60726a774d3fcf17e4ecaa70e9f4186ee3f473c748492d0d282de55d955fb33b88b7fc0
data/lib/rodash.rb ADDED
@@ -0,0 +1,150 @@
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
+ protected
59
+
60
+ def self.baseSet(object, path, value, customizer = false)
61
+ throw "object must be a hash" if not object.is_a? Hash
62
+
63
+ path = isKey(path, object) ? [path + ''] : baseToPath(path);
64
+
65
+ nested = object
66
+ newNested = nil
67
+
68
+ [*path, nil].each_cons(2) do |key,nextkey|
69
+ if isIndex(key) && nested.is_a?(Array)
70
+ key = key.to_i
71
+ end
72
+ if nextkey.nil?
73
+ newNested = nested[key] = value
74
+ else
75
+ newNested = nested[key]
76
+ if isIndex(nextkey) && (!newNested.is_a?(Hash) || !newNested.is_a(Array))
77
+ nested[key] = newNested = []
78
+ elsif not newNested.is_a? Hash
79
+ nested[key] = newNested = {}
80
+ end
81
+ end
82
+ nested = newNested
83
+ end
84
+
85
+ return object
86
+ end
87
+
88
+ def self.baseGet(object, path)
89
+ path = isKey(path, object) ? [path + ''] : baseToPath(path);
90
+
91
+ index = 0
92
+ length = path.count
93
+
94
+ while !object.nil? && index < length
95
+ key = path[index]
96
+ if object.is_a?(Array)
97
+ if isIndex(key)
98
+ object = object[key.to_i]
99
+ else
100
+ return nil
101
+ end
102
+ elsif object.is_a?(Hash)
103
+ object = object[path[index]]
104
+ else
105
+ return nil
106
+ end
107
+ index += 1
108
+ end
109
+
110
+ (index > 0 && index == length) ? object : nil
111
+ end
112
+
113
+ @@reIsDeepProp = /\.|\[(?:[^\[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/
114
+ @@reIsPlainProp = /^\w*$/
115
+ @@rePropName = /[^.\[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]/
116
+ @@reEscapeChar = /\\(\\)?/
117
+
118
+ def self.isKey(value, object)
119
+ return true if value.is_a? Numeric
120
+
121
+ return !value.is_a?(Array) &&
122
+ (@@reIsPlainProp =~ value || !@@reIsDeepProp =~ value ||
123
+ (!object.nil? && object.has_key?(value)))
124
+ end
125
+
126
+ def self.baseToPath(value)
127
+ value.kind_of?(Array) ? value : stringToPath(value)
128
+ end
129
+
130
+ def self.stringToPath(string)
131
+ result = []
132
+ string.to_s.gsub(@@rePropName) do |match|
133
+ number = $1
134
+ quote = $2
135
+ string = $3
136
+ result.push((quote && !quote.empty?) ? string.gsub(@@reEscapeChar, '\1') : (number || match))
137
+ end
138
+
139
+ return result
140
+ end
141
+
142
+ @@MAX_SAFE_INTEGER = 9007199254740991
143
+ @@reIsUint = /^(?:0|[1-9]\d*)$/
144
+
145
+ def self.isIndex(value, length = nil)
146
+ value = (value.is_a?(Numeric) || @@reIsUint =~ value) ? value.to_i : -1
147
+ length = length.nil? ? @@MAX_SAFE_INTEGER : length
148
+ return value > -1 && value % 1 == 0 && value < length
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rodash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Antoine Pultier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: _.set and _.get for Ruby
14
+ email: antoine.pultier@sintef.no
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rodash.rb
20
+ homepage: https://github.com/SINTEF-9012/Rodash
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Rodash
44
+ test_files: []