dot-object 2.1.3 → 2.1.5

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.
@@ -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,215 +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 allow keys with numbers', function () {
59
- var row = {
60
- id: 2,
61
- '0A': 'a',
62
- '0A9': 'b',
63
- '0B.1AB.A34C9': 'c'
64
- }
65
-
66
- Dot.object(row)
67
-
68
- row.should.eql({
69
- id: 2,
70
- '0A': 'a',
71
- '0A9': 'b',
72
- '0B': {
73
- '1AB': {
74
- A34C9: 'c'
75
- }
76
- }
77
- })
78
- })
79
-
80
- it('Should expand dotted string', function () {
81
- var tgt = {}
82
-
83
- Dot.str('this.is.my.string', 'value', tgt)
84
-
85
- tgt.should.eql({
86
- this: {
87
- is: {
88
- my: {
89
- string: 'value'
90
- }
91
- }
92
- }
93
- })
94
- })
95
-
96
- it('Dot.str Redefinition should fail', function () {
97
- var tgt = {
98
- already: 'set'
99
- }
100
-
101
- ;(function () {
102
- Dot.str('already.new', 'value', tgt)
103
- }).should.throw('Trying to redefine `already` which is a string')
104
- })
105
-
106
- it('Dot.str should process a modifier', function () {
107
- var tgt = {}
108
-
109
- Dot.str('this.is.my.string', 'value', tgt, _s.capitalize)
110
-
111
- tgt.should.eql({
112
- this: {
113
- is: {
114
- my: {
115
- string: 'Value'
116
- }
117
- }
118
- }
119
- })
120
- })
121
-
122
- it('Dot.str should process multiple modifiers', function () {
123
- var tgt = {}
124
-
125
- Dot.str(
126
- 'this.is.my.string',
127
- ' this is a test ',
128
- tgt, [_s.trim, _s.underscored]
129
- )
130
-
131
- tgt.should.eql({
132
- this: {
133
- is: {
134
- my: {
135
- string: 'this_is_a_test'
136
- }
137
- }
138
- }
139
- })
140
- })
141
-
142
- it('Dot.object should process a modifier', function () {
143
- var row = {
144
- 'page.title': 'my page',
145
- 'page.slug': 'My Page'
146
- }
147
-
148
- var mods = {
149
- 'page.title': _s.titleize,
150
- 'page.slug': _s.slugify
151
- }
152
-
153
- Dot.object(row, mods)
154
-
155
- row.should.eql({ page: { title: 'My Page', slug: 'my-page' } })
156
- })
157
-
158
- it('should process root properties',
159
- function () {
160
- var row = {
161
- nr: 200,
162
- 'nested.nr': 200
163
- }
164
-
165
- var mods = {
166
- nr: [val => val * 2],
167
- 'nested.nr': [val => val * 2]
168
- }
169
-
170
- Dot.object(row, mods)
171
-
172
- row.should.eql({ nr: 400, nested: { nr: 400 } })
173
- }
174
- )
175
-
176
- it('should process non dot value with modifier when override is false',
177
- function () {
178
- var row = { title: 'my page', slug: 'My Page' }
179
-
180
- var mods = { title: _s.titleize, slug: _s.slugify }
181
-
182
- Dot.object(row, mods)
183
-
184
- row.should.eql({ title: 'My Page', slug: 'my-page' })
185
- }
186
- )
187
-
188
- it('Dot.object should process multiple modifiers', function () {
189
- var row = { 'page.name': ' My Page ' }
190
-
191
- var mods = { 'page.name': [_s.trim, _s.underscored] }
192
-
193
- Dot.object(row, mods)
194
-
195
- row.should.eql({ page: { name: 'my_page' } })
196
- })
197
-
198
- it('Dot.object should work with a different separator', function () {
199
- var row = { 'page=>name': ' My Page ' }
200
-
201
- var mods = { 'page=>name': [_s.trim, _s.underscored] }
202
-
203
- var dot = new Dot('=>', false)
204
- dot.object(row, mods)
205
-
206
- row.should.eql({ page: { name: 'my_page' } })
207
- })
208
-
209
- it('Dot.object should disallow to set __proto__', function () {
210
- var row = { '__proto__.toString': 'hi' }
211
-
212
- var dot = new Dot()
213
- ;(() => dot.object(row)).should.throw(/Refusing to update/)
214
- })
215
- })