dot-object 2.1.4 → 2.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,176 +0,0 @@
1
- 'use strict'
2
-
3
- /* jshint -W030 */
4
-
5
- require('should')
6
- var Dot = require('../index')
7
-
8
- describe('Dotted Array notation', function () {
9
- var src
10
-
11
- beforeEach(function () {
12
- src = {
13
- path: [{
14
- longitude: 5.512482166290283,
15
- latitude: 52.5006217956543
16
- }, {
17
- longitude: 5.512370586395264,
18
- latitude: 52.50059509277344
19
- }, {
20
- longitude: 5.512370586395264,
21
- latitude: 52.50059509277344
22
- }]
23
- }
24
- })
25
-
26
- function runVariant (type) {
27
- var v = function (v) {
28
- if (type === 'bracket') {
29
- // rewrite some.prop.1 to some.prop[1]
30
- return v.replace(/\.(-?\d+)/g, '[$1]')
31
- } else {
32
- return v
33
- }
34
- }
35
-
36
- describe('can pick', function () {
37
- it('index', function () {
38
- Dot.pick(v('path.0'), src).should.eql(src.path[0])
39
- Dot.pick(v('path.2'), src).should.eql(src.path[2])
40
- ;(typeof Dot.pick(v('path.9'), src)).should.eql('undefined')
41
- })
42
-
43
- it('negative index', function () {
44
- Dot.pick(v('path.-1'), src).should.eql(src.path[2])
45
- Dot.pick(v('path.-2'), src).should.eql(src.path[1])
46
- Dot.pick(v('path.-3'), src).should.eql(src.path[0])
47
- ;(typeof Dot.pick(v('path.-9'), src)).should.eql('undefined')
48
- })
49
-
50
- it('non-array `-` prefixed properties', function () {
51
- var src = {
52
- path: {
53
- '-1': 'test1',
54
- '-2': 'test2',
55
- '-3': 'test3',
56
- '----key': 'test4'
57
- }
58
- }
59
- Dot.pick(v('path.-1'), src).should.eql('test1')
60
- Dot.pick(v('path.-2'), src).should.eql('test2')
61
- Dot.pick(v('path.-3'), src).should.eql('test3')
62
- Dot.pick(v('path.----key'), src).should.eql('test4')
63
- ;(typeof Dot.pick(v('path.-9'), src)).should.eql('undefined')
64
- })
65
-
66
- it('multiple indexes', function () {
67
- var src = {
68
- I: [
69
- { am: [{ nes: ['ted'] }] },
70
- { me: 'too' }
71
- ]
72
- }
73
-
74
- Dot.pick(v('I.0'), src).should.eql(src.I[0])
75
- Dot.pick(v('I.0.am'), src).should.eql(src.I[0].am)
76
- Dot.pick(v('I.0.am.0'), src).should.eql(src.I[0].am[0])
77
- Dot.pick(v('I.0.am.0.nes'), src).should.eql(src.I[0].am[0].nes)
78
- Dot.pick(v('I.0.am.0.nes.0'), src).should.eql('ted')
79
- Dot.pick(v('I.1.me'), src).should.eql('too')
80
- })
81
- })
82
-
83
- describe('can set', function () {
84
- it('index at target', function () {
85
- var obj = { path: [] }
86
-
87
- Dot.set(v('path.0'), 'test', obj)
88
- Dot.set(v('path.1'), 'test2', obj)
89
-
90
- obj.path.should.be.instanceOf(Array)
91
- obj.should.eql({ path: ['test', 'test2'] })
92
- })
93
-
94
- it('index and set undefined for empty indices', function () {
95
- var obj = { path: [] }
96
-
97
- Dot.set(v('path.0'), 'test', obj)
98
- Dot.set(v('path.2'), 'test2', obj)
99
-
100
- obj.path.should.be.instanceOf(Array)
101
-
102
- // array will have an undefined index.
103
- JSON.stringify(obj)
104
- .should.eql(
105
- JSON.stringify({ path: ['test', undefined, 'test2'] })
106
- )
107
-
108
- // to json will converted it to null
109
- JSON.stringify(obj).should.eql('{"path":["test",null,"test2"]}')
110
- })
111
-
112
- it('index and overwrite existing values', function () {
113
- var obj = { path: ['still', 'shall', 'be', 'gone', 'here'] }
114
-
115
- Dot.set(v('path.1'), 'x', obj)
116
- Dot.set(v('path.2'), 'xx', obj)
117
- Dot.set(v('path.3'), 'xxx', obj)
118
-
119
- obj.should.eql({ path: ['still', 'x', 'xx', 'xxx', 'here'] })
120
- })
121
- })
122
-
123
- describe('can remove', function () {
124
- it('indexes one by one leaving traces', function () {
125
- var obj = { path: ['still', 'shall', 'really', 'be', 'gone', 'here'] }
126
-
127
- Dot.remove(v('path.1'), obj)
128
- Dot.remove(v('path.2'), obj)
129
- Dot.del(v('path.3'), obj) // use alias
130
- Dot.del(v('path.4'), obj)
131
-
132
- // array will have an undefined index.
133
- JSON.stringify(obj)
134
- .should.eql(
135
- JSON.stringify({
136
- path: [
137
- 'still', undefined, undefined, undefined, undefined, 'here'
138
- ]
139
- })
140
- )
141
-
142
- // to json will converted it to null
143
- JSON.stringify(obj).should.eql(
144
- '{"path":["still",null,null,null,null,"here"]}'
145
- )
146
- })
147
-
148
- it('array of indexes leaving no traces', function () {
149
- var obj = { path: ['still', 'shall', 'really', 'be', 'gone', 'here'] }
150
-
151
- Dot.remove([
152
- v('path.1'),
153
- v('path.2'),
154
- v('path.3'),
155
- v('path.4')], obj)
156
-
157
- JSON.stringify(obj).should.eql('{"path":["still","here"]}')
158
- })
159
- })
160
- }
161
-
162
- describe('with dot notation', function () {
163
- runVariant()
164
- })
165
-
166
- // extra logic no real benefit.
167
- describe('with bracket notation', function () {
168
- runVariant('bracket')
169
- })
170
-
171
- describe('Refuse to update __proto__', function () {
172
- var obj = { path: [] }
173
-
174
- ;(() => Dot.set('path[0].__proto__.toString', 'test', obj)).should.throw(/Refusing to update/)
175
- })
176
- })
package/test/copy.js DELETED
@@ -1,86 +0,0 @@
1
- 'use strict'
2
-
3
- require('should')
4
- var Dot = require('../index')
5
-
6
- describe('Copy:', function () {
7
- it('Should be able to copy properties', function () {
8
- var src = {
9
- name: 'John',
10
- stuff: {
11
- phone: {
12
- brand: 'iphone',
13
- version: 6
14
- }
15
- }
16
- }
17
-
18
- var tgt = {
19
- name: 'Brandon'
20
- }
21
-
22
- var srcExpected = JSON.parse(JSON.stringify(src))
23
-
24
- var tgtExpected = {
25
- name: 'Brandon',
26
- copied: 'John',
27
- wanna: {
28
- haves: {
29
- phone: {
30
- brand: 'iphone',
31
- version: 6
32
- }
33
- }
34
- }
35
- }
36
-
37
- // copy object
38
- Dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt)
39
-
40
- // copy string
41
- Dot.copy('name', 'copied', src, tgt)
42
-
43
- src.should.eql(srcExpected)
44
- tgt.should.eql(tgtExpected)
45
- })
46
-
47
- it('Should process modifiers', function () {
48
- function up (val) {
49
- val.brand = val.brand.toUpperCase()
50
- return val
51
- }
52
-
53
- var src = {
54
- name: 'John',
55
- stuff: {
56
- phone: {
57
- brand: 'iphone',
58
- version: 6
59
- }
60
- }
61
- }
62
-
63
- var tgt = {
64
- name: 'Brandon'
65
- }
66
-
67
- var srcExpected = JSON.parse(JSON.stringify(src))
68
-
69
- var tgtExpected = {
70
- name: 'Brandon',
71
- wanna: {
72
- haves: {
73
- phone: {
74
- brand: 'IPHONE',
75
- version: 6
76
- }
77
- }
78
- }
79
- }
80
-
81
- Dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt, up)
82
-
83
- src.should.eql(srcExpected)
84
- tgt.should.eql(tgtExpected)
85
- })
86
- })
@@ -1,30 +0,0 @@
1
- {
2
- "name": "array deep bug #10",
3
- "options": {
4
- "useArray": true
5
- },
6
- "input": {
7
- "a[0]": "A0",
8
- "a[1]": "A1",
9
- "a[2]": "A2",
10
- "a[3].b[0].c[0]": "A3B0C0",
11
- "a[3].b[0].c[1]": "A3B0C1"
12
- },
13
- "expected": {
14
- "a": [
15
- "A0",
16
- "A1",
17
- "A2",
18
- {
19
- "b": [
20
- {
21
- "c": [
22
- "A3B0C0",
23
- "A3B0C1"
24
- ]
25
- }
26
- ]
27
- }
28
- ]
29
- }
30
- }
@@ -1,19 +0,0 @@
1
- {
2
- "name": "array deep bug2 #10",
3
- "options": {
4
- "useArray": true
5
- },
6
- "input": {
7
- "b.0.c.1": "b0c1",
8
- "b.0.c.2": "b0c2"
9
- },
10
- "expected": {
11
- "b" : [ {
12
- "c" : [
13
- null,
14
- "b0c1",
15
- "b0c2"
16
- ]
17
- }]
18
- }
19
- }
@@ -1,43 +0,0 @@
1
- {
2
- "name": "empty array",
3
- "input": [
4
- {
5
- "a": []
6
- },
7
- {
8
- "a.0": 1,
9
- "a": []
10
- },
11
- {
12
- "a": [],
13
- "a.0": 2
14
- },
15
- {
16
- "a.0.b": []
17
- },
18
- {
19
- "b.a.0": "b",
20
- "b.a": [],
21
- "b.a.1": "c"
22
- }
23
- ],
24
- "expected": [
25
- {
26
- "a": []
27
- },
28
- {
29
- "a": [1]
30
- },
31
- {
32
- "a": [2]
33
- },
34
- {
35
- "a" : [ {"b" : [] } ]
36
- },
37
- {
38
- "b" : {
39
- "a": [ "b", "c" ]
40
- }
41
- }
42
- ]
43
- }
@@ -1,23 +0,0 @@
1
- {
2
- "name": "empty object",
3
- "input": [
4
- {
5
- "a.b": {}
6
- },
7
- {
8
- "a.b.c": 1,
9
- "a.b": {} ,
10
- "a.b.d": 2
11
- }
12
- ],
13
- "expected": [
14
- {
15
- "a" : {"b" : {} }
16
- },
17
- {
18
- "a" : {
19
- "b": { "c": 1, "d": 2 }
20
- }
21
- }
22
- ]
23
- }
@@ -1,8 +0,0 @@
1
- module.exports = [
2
- require('./array_deep_bug'),
3
- require('./array_deep_bug2'),
4
- require('./empty_array'),
5
- require('./empty_object'),
6
- require('./object_deep_numeric_keys'),
7
- require('./object_deep_numeric_keys2')
8
- ]
@@ -1,30 +0,0 @@
1
- {
2
- "name": "object deep numeric keys",
3
- "options": {
4
- "useArray": false
5
- },
6
- "input": {
7
- "a[0]": "A0",
8
- "a[1]": "A1",
9
- "a[2]": "A2",
10
- "a[3].b[0].c[0]": "A3B0C0",
11
- "a[3].b[0].c[1]": "A3B0C1"
12
- },
13
- "expected": {
14
- "a": {
15
- "0": "A0",
16
- "1": "A1",
17
- "2": "A2",
18
- "3": {
19
- "b": {
20
- "0": {
21
- "c": {
22
- "0": "A3B0C0",
23
- "1": "A3B0C1"
24
- }
25
- }
26
- }
27
- }
28
- }
29
- }
30
- }
@@ -1,20 +0,0 @@
1
- {
2
- "name": "object deep numeric keys 2 #10",
3
- "options": {
4
- "useArray": false
5
- },
6
- "input": {
7
- "b.0.c.1": "b0c1",
8
- "b.0.c.2": "b0c2"
9
- },
10
- "expected": {
11
- "b": {
12
- "0": {
13
- "c": {
14
- "1": "b0c1",
15
- "2": "b0c2"
16
- }
17
- }
18
- }
19
- }
20
- }
package/test/dot-json.js DELETED
@@ -1,237 +0,0 @@
1
- 'use strict'
2
-
3
- require('should')
4
- var _s = require('underscore.string')
5
- var Dot = require('../index')
6
-
7
- describe('Object test:', function () {
8
- it('Should expand dotted keys', function () {
9
- var row = {
10
- id: 2,
11
- 'contact.name.first': 'John',
12
- 'contact.name.last': 'Doe',
13
- 'contact.email': 'example@gmail.com',
14
- 'contact.info.about.me': 'classified'
15
- }
16
-
17
- Dot.object(row)
18
-
19
- row.should.eql({
20
- id: 2,
21
- contact: {
22
- name: {
23
- first: 'John',
24
- last: 'Doe'
25
- },
26
- email: 'example@gmail.com',
27
- info: {
28
- about: {
29
- me: 'classified'
30
- }
31
- }
32
- }
33
- })
34
- })
35
-
36
- it('Should expand dotted keys with array notation', function () {
37
- var row = {
38
- id: 2,
39
- 'my.arr.0': 'one',
40
- 'my.arr.1': 'two',
41
- 'my.arr.2': 'three',
42
- 'my.arr2[0]': 'one',
43
- 'my.arr2[1]': 'two',
44
- 'my.arr2[2]': 'three'
45
- }
46
-
47
- Dot.object(row)
48
-
49
- row.should.eql({
50
- id: 2,
51
- my: {
52
- arr: ['one', 'two', 'three'],
53
- arr2: ['one', 'two', 'three']
54
- }
55
- })
56
- })
57
-
58
- it('Should expand dotted keys with array notation with different separator', function () {
59
- var row = {
60
- id: 2,
61
- my_arr_0: 'one',
62
- my_arr_1: 'two',
63
- my_arr_2: 'three',
64
- 'my_arr2[0]': 'one',
65
- 'my_arr2[1]': 'two',
66
- 'my_arr2[2]': 'three'
67
- }
68
-
69
- new Dot('_').object(row)
70
-
71
- row.should.eql({
72
- id: 2,
73
- my: {
74
- arr: ['one', 'two', 'three'],
75
- arr2: ['one', 'two', 'three']
76
- }
77
- })
78
- })
79
-
80
- it('Should allow keys with numbers', function () {
81
- var row = {
82
- id: 2,
83
- '0A': 'a',
84
- '0A9': 'b',
85
- '0B.1AB.A34C9': 'c'
86
- }
87
-
88
- Dot.object(row)
89
-
90
- row.should.eql({
91
- id: 2,
92
- '0A': 'a',
93
- '0A9': 'b',
94
- '0B': {
95
- '1AB': {
96
- A34C9: 'c'
97
- }
98
- }
99
- })
100
- })
101
-
102
- it('Should expand dotted string', function () {
103
- var tgt = {}
104
-
105
- Dot.str('this.is.my.string', 'value', tgt)
106
-
107
- tgt.should.eql({
108
- this: {
109
- is: {
110
- my: {
111
- string: 'value'
112
- }
113
- }
114
- }
115
- })
116
- })
117
-
118
- it('Dot.str Redefinition should fail', function () {
119
- var tgt = {
120
- already: 'set'
121
- }
122
-
123
- ;(function () {
124
- Dot.str('already.new', 'value', tgt)
125
- }).should.throw('Trying to redefine `already` which is a string')
126
- })
127
-
128
- it('Dot.str should process a modifier', function () {
129
- var tgt = {}
130
-
131
- Dot.str('this.is.my.string', 'value', tgt, _s.capitalize)
132
-
133
- tgt.should.eql({
134
- this: {
135
- is: {
136
- my: {
137
- string: 'Value'
138
- }
139
- }
140
- }
141
- })
142
- })
143
-
144
- it('Dot.str should process multiple modifiers', function () {
145
- var tgt = {}
146
-
147
- Dot.str(
148
- 'this.is.my.string',
149
- ' this is a test ',
150
- tgt, [_s.trim, _s.underscored]
151
- )
152
-
153
- tgt.should.eql({
154
- this: {
155
- is: {
156
- my: {
157
- string: 'this_is_a_test'
158
- }
159
- }
160
- }
161
- })
162
- })
163
-
164
- it('Dot.object should process a modifier', function () {
165
- var row = {
166
- 'page.title': 'my page',
167
- 'page.slug': 'My Page'
168
- }
169
-
170
- var mods = {
171
- 'page.title': _s.titleize,
172
- 'page.slug': _s.slugify
173
- }
174
-
175
- Dot.object(row, mods)
176
-
177
- row.should.eql({ page: { title: 'My Page', slug: 'my-page' } })
178
- })
179
-
180
- it('should process root properties',
181
- function () {
182
- var row = {
183
- nr: 200,
184
- 'nested.nr': 200
185
- }
186
-
187
- var mods = {
188
- nr: [val => val * 2],
189
- 'nested.nr': [val => val * 2]
190
- }
191
-
192
- Dot.object(row, mods)
193
-
194
- row.should.eql({ nr: 400, nested: { nr: 400 } })
195
- }
196
- )
197
-
198
- it('should process non dot value with modifier when override is false',
199
- function () {
200
- var row = { title: 'my page', slug: 'My Page' }
201
-
202
- var mods = { title: _s.titleize, slug: _s.slugify }
203
-
204
- Dot.object(row, mods)
205
-
206
- row.should.eql({ title: 'My Page', slug: 'my-page' })
207
- }
208
- )
209
-
210
- it('Dot.object should process multiple modifiers', function () {
211
- var row = { 'page.name': ' My Page ' }
212
-
213
- var mods = { 'page.name': [_s.trim, _s.underscored] }
214
-
215
- Dot.object(row, mods)
216
-
217
- row.should.eql({ page: { name: 'my_page' } })
218
- })
219
-
220
- it('Dot.object should work with a different separator', function () {
221
- var row = { 'page=>name': ' My Page ' }
222
-
223
- var mods = { 'page=>name': [_s.trim, _s.underscored] }
224
-
225
- var dot = new Dot('=>', false)
226
- dot.object(row, mods)
227
-
228
- row.should.eql({ page: { name: 'my_page' } })
229
- })
230
-
231
- it('Dot.object should disallow to set __proto__', function () {
232
- var row = { '__proto__.toString': 'hi' }
233
-
234
- var dot = new Dot()
235
- ;(() => dot.object(row)).should.throw(/Refusing to update/)
236
- })
237
- })