forked from dawidd6/action-download-artifact
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot-json.js
More file actions
237 lines (191 loc) · 4.59 KB
/
Copy pathdot-json.js
File metadata and controls
237 lines (191 loc) · 4.59 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'use strict'
require('should')
var _s = require('underscore.string')
var Dot = require('../index')
describe('Object test:', function () {
it('Should expand dotted keys', function () {
var row = {
id: 2,
'contact.name.first': 'John',
'contact.name.last': 'Doe',
'contact.email': 'example@gmail.com',
'contact.info.about.me': 'classified'
}
Dot.object(row)
row.should.eql({
id: 2,
contact: {
name: {
first: 'John',
last: 'Doe'
},
email: 'example@gmail.com',
info: {
about: {
me: 'classified'
}
}
}
})
})
it('Should expand dotted keys with array notation', function () {
var row = {
id: 2,
'my.arr.0': 'one',
'my.arr.1': 'two',
'my.arr.2': 'three',
'my.arr2[0]': 'one',
'my.arr2[1]': 'two',
'my.arr2[2]': 'three'
}
Dot.object(row)
row.should.eql({
id: 2,
my: {
arr: ['one', 'two', 'three'],
arr2: ['one', 'two', 'three']
}
})
})
it('Should expand dotted keys with array notation with different separator', function () {
var row = {
id: 2,
my_arr_0: 'one',
my_arr_1: 'two',
my_arr_2: 'three',
'my_arr2[0]': 'one',
'my_arr2[1]': 'two',
'my_arr2[2]': 'three'
}
new Dot('_').object(row)
row.should.eql({
id: 2,
my: {
arr: ['one', 'two', 'three'],
arr2: ['one', 'two', 'three']
}
})
})
it('Should allow keys with numbers', function () {
var row = {
id: 2,
'0A': 'a',
'0A9': 'b',
'0B.1AB.A34C9': 'c'
}
Dot.object(row)
row.should.eql({
id: 2,
'0A': 'a',
'0A9': 'b',
'0B': {
'1AB': {
A34C9: 'c'
}
}
})
})
it('Should expand dotted string', function () {
var tgt = {}
Dot.str('this.is.my.string', 'value', tgt)
tgt.should.eql({
this: {
is: {
my: {
string: 'value'
}
}
}
})
})
it('Dot.str Redefinition should fail', function () {
var tgt = {
already: 'set'
}
;(function () {
Dot.str('already.new', 'value', tgt)
}).should.throw('Trying to redefine `already` which is a string')
})
it('Dot.str should process a modifier', function () {
var tgt = {}
Dot.str('this.is.my.string', 'value', tgt, _s.capitalize)
tgt.should.eql({
this: {
is: {
my: {
string: 'Value'
}
}
}
})
})
it('Dot.str should process multiple modifiers', function () {
var tgt = {}
Dot.str(
'this.is.my.string',
' this is a test ',
tgt, [_s.trim, _s.underscored]
)
tgt.should.eql({
this: {
is: {
my: {
string: 'this_is_a_test'
}
}
}
})
})
it('Dot.object should process a modifier', function () {
var row = {
'page.title': 'my page',
'page.slug': 'My Page'
}
var mods = {
'page.title': _s.titleize,
'page.slug': _s.slugify
}
Dot.object(row, mods)
row.should.eql({ page: { title: 'My Page', slug: 'my-page' } })
})
it('should process root properties',
function () {
var row = {
nr: 200,
'nested.nr': 200
}
var mods = {
nr: [val => val * 2],
'nested.nr': [val => val * 2]
}
Dot.object(row, mods)
row.should.eql({ nr: 400, nested: { nr: 400 } })
}
)
it('should process non dot value with modifier when override is false',
function () {
var row = { title: 'my page', slug: 'My Page' }
var mods = { title: _s.titleize, slug: _s.slugify }
Dot.object(row, mods)
row.should.eql({ title: 'My Page', slug: 'my-page' })
}
)
it('Dot.object should process multiple modifiers', function () {
var row = { 'page.name': ' My Page ' }
var mods = { 'page.name': [_s.trim, _s.underscored] }
Dot.object(row, mods)
row.should.eql({ page: { name: 'my_page' } })
})
it('Dot.object should work with a different separator', function () {
var row = { 'page=>name': ' My Page ' }
var mods = { 'page=>name': [_s.trim, _s.underscored] }
var dot = new Dot('=>', false)
dot.object(row, mods)
row.should.eql({ page: { name: 'my_page' } })
})
it('Dot.object should disallow to set __proto__', function () {
var row = { '__proto__.toString': 'hi' }
var dot = new Dot()
;(() => dot.object(row)).should.throw(/Refusing to update/)
})
})