forked from dawidd6/action-download-artifact
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_notation.js
More file actions
176 lines (144 loc) · 4.91 KB
/
Copy patharray_notation.js
File metadata and controls
176 lines (144 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict'
/* jshint -W030 */
require('should')
var Dot = require('../index')
describe('Dotted Array notation', function () {
var src
beforeEach(function () {
src = {
path: [{
longitude: 5.512482166290283,
latitude: 52.5006217956543
}, {
longitude: 5.512370586395264,
latitude: 52.50059509277344
}, {
longitude: 5.512370586395264,
latitude: 52.50059509277344
}]
}
})
function runVariant (type) {
var v = function (v) {
if (type === 'bracket') {
// rewrite some.prop.1 to some.prop[1]
return v.replace(/\.(-?\d+)/g, '[$1]')
} else {
return v
}
}
describe('can pick', function () {
it('index', function () {
Dot.pick(v('path.0'), src).should.eql(src.path[0])
Dot.pick(v('path.2'), src).should.eql(src.path[2])
;(typeof Dot.pick(v('path.9'), src)).should.eql('undefined')
})
it('negative index', function () {
Dot.pick(v('path.-1'), src).should.eql(src.path[2])
Dot.pick(v('path.-2'), src).should.eql(src.path[1])
Dot.pick(v('path.-3'), src).should.eql(src.path[0])
;(typeof Dot.pick(v('path.-9'), src)).should.eql('undefined')
})
it('non-array `-` prefixed properties', function () {
var src = {
path: {
'-1': 'test1',
'-2': 'test2',
'-3': 'test3',
'----key': 'test4'
}
}
Dot.pick(v('path.-1'), src).should.eql('test1')
Dot.pick(v('path.-2'), src).should.eql('test2')
Dot.pick(v('path.-3'), src).should.eql('test3')
Dot.pick(v('path.----key'), src).should.eql('test4')
;(typeof Dot.pick(v('path.-9'), src)).should.eql('undefined')
})
it('multiple indexes', function () {
var src = {
I: [
{ am: [{ nes: ['ted'] }] },
{ me: 'too' }
]
}
Dot.pick(v('I.0'), src).should.eql(src.I[0])
Dot.pick(v('I.0.am'), src).should.eql(src.I[0].am)
Dot.pick(v('I.0.am.0'), src).should.eql(src.I[0].am[0])
Dot.pick(v('I.0.am.0.nes'), src).should.eql(src.I[0].am[0].nes)
Dot.pick(v('I.0.am.0.nes.0'), src).should.eql('ted')
Dot.pick(v('I.1.me'), src).should.eql('too')
})
})
describe('can set', function () {
it('index at target', function () {
var obj = { path: [] }
Dot.set(v('path.0'), 'test', obj)
Dot.set(v('path.1'), 'test2', obj)
obj.path.should.be.instanceOf(Array)
obj.should.eql({ path: ['test', 'test2'] })
})
it('index and set undefined for empty indices', function () {
var obj = { path: [] }
Dot.set(v('path.0'), 'test', obj)
Dot.set(v('path.2'), 'test2', obj)
obj.path.should.be.instanceOf(Array)
// array will have an undefined index.
JSON.stringify(obj)
.should.eql(
JSON.stringify({ path: ['test', undefined, 'test2'] })
)
// to json will converted it to null
JSON.stringify(obj).should.eql('{"path":["test",null,"test2"]}')
})
it('index and overwrite existing values', function () {
var obj = { path: ['still', 'shall', 'be', 'gone', 'here'] }
Dot.set(v('path.1'), 'x', obj)
Dot.set(v('path.2'), 'xx', obj)
Dot.set(v('path.3'), 'xxx', obj)
obj.should.eql({ path: ['still', 'x', 'xx', 'xxx', 'here'] })
})
})
describe('can remove', function () {
it('indexes one by one leaving traces', function () {
var obj = { path: ['still', 'shall', 'really', 'be', 'gone', 'here'] }
Dot.remove(v('path.1'), obj)
Dot.remove(v('path.2'), obj)
Dot.del(v('path.3'), obj) // use alias
Dot.del(v('path.4'), obj)
// array will have an undefined index.
JSON.stringify(obj)
.should.eql(
JSON.stringify({
path: [
'still', undefined, undefined, undefined, undefined, 'here'
]
})
)
// to json will converted it to null
JSON.stringify(obj).should.eql(
'{"path":["still",null,null,null,null,"here"]}'
)
})
it('array of indexes leaving no traces', function () {
var obj = { path: ['still', 'shall', 'really', 'be', 'gone', 'here'] }
Dot.remove([
v('path.1'),
v('path.2'),
v('path.3'),
v('path.4')], obj)
JSON.stringify(obj).should.eql('{"path":["still","here"]}')
})
})
}
describe('with dot notation', function () {
runVariant()
})
// extra logic no real benefit.
describe('with bracket notation', function () {
runVariant('bracket')
})
describe('Refuse to update __proto__', function () {
var obj = { path: [] }
;(() => Dot.set('path[0].__proto__.toString', 'test', obj)).should.throw(/Refusing to update/)
})
})