Skip to content

Commit 6b806cc

Browse files
committed
array: test coverage
1 parent 3e95614 commit 6b806cc

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"ava": {
3838
"babel": false,
3939
"files": [
40-
"test/array.test.js"
40+
"test/*.test.js"
4141
]
4242
},
4343
"author": "kaelzhang",

src/array.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const hasOwnProperty = require('has-own-prop')
2+
const {isObject, isArray} = require('core-util-is')
23

34
const PREFIX_BEFORE = 'before'
45
const PREFIX_AFTER_PROP = 'after-prop'
@@ -60,12 +61,12 @@ const swap_comments = (array, from, to) => {
6061
SYMBOL_PREFIXES.forEach(prefix => {
6162
const target_prop = symbol(prefix, to)
6263
if (!hasOwnProperty(array, target_prop)) {
63-
assign_comments(array, array, from, to, prefix)
64+
assign_comments(array, array, to, from, prefix)
6465
return
6566
}
6667

6768
const comments = array[target_prop]
68-
assign_comments(array, array, from, to, prefix)
69+
assign_comments(array, array, to, from, prefix)
6970
array[symbol(prefix, from)] = comments
7071
})
7172
}
@@ -254,7 +255,23 @@ class CommentArray extends Array {
254255

255256
module.exports = {
256257
CommentArray,
257-
assign,
258+
assign (target, source, keys) {
259+
if (!isObject(target)) {
260+
throw new TypeError('Cannot convert undefined or null to object')
261+
}
262+
263+
if (!isObject(source)) {
264+
return target
265+
}
266+
267+
if (keys === UNDEFINED) {
268+
keys = Object.keys(source)
269+
} else if (!isArray(keys)) {
270+
throw new TypeError('keys must be array or undefined')
271+
}
272+
273+
return assign(target, source, keys)
274+
},
258275

259276
PREFIX_BEFORE,
260277
PREFIX_AFTER_PROP,

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const {parse, tokenize} = require('./parse')
22
const stringify = require('./stringify')
3-
const {CommentArray} = require('./array')
3+
const {CommentArray, assign} = require('./array')
44

55
module.exports = {
66
parse,
77
stringify,
88
tokenize,
99

10-
CommentArray
10+
CommentArray,
11+
assign
1112
}

test/array.test.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const test = require('ava')
22
const {
33
isFunction, isObject, isString, isArray
44
} = require('core-util-is')
5-
const {parse, stringify} = require('../src')
5+
const {parse, stringify, assign} = require('../src')
6+
7+
const st = o => stringify(o, null, 2)
68

79
const a1 = `[
810
// 0
@@ -20,6 +22,11 @@ const a2 = `[
2022
2
2123
]`
2224

25+
const a3 = `[
26+
// 0
27+
0
28+
]`
29+
2330
// ret: return value to expect
2431
const texpect = (t, ret, str, r, rr) => {
2532
if (isObject(ret)) {
@@ -31,7 +38,7 @@ const texpect = (t, ret, str, r, rr) => {
3138
if (isString(rr)) {
3239
t.is(rr, str)
3340
} else {
34-
t.is(stringify(rr, null, 2), str)
41+
t.is(st(rr), str)
3542
}
3643
}
3744

@@ -102,10 +109,13 @@ const CASES = [
102109
'slice(0, - 2)',
103110
a1,
104111
array => array.slice(0, - 2),
105-
slice([0], `[
106-
// 0
107-
0
108-
]`)
112+
slice([0], a3)
113+
],
114+
[
115+
'slice(0, 1)',
116+
a1,
117+
array => array.slice(0, 1),
118+
slice([0], a3)
109119
],
110120
[
111121
'unshift()',
@@ -138,19 +148,30 @@ const CASES = [
138148
2
139149
]`)
140150
],
141-
// [
142-
// 'reverse',
143-
// a1,
144-
// array => array.reverse(),
145-
// unshift([2, 1, 0], `[
146-
// // 2
147-
// 2,
148-
// // 1
149-
// 1,
150-
// // 0
151-
// 0
152-
// ]`)
153-
// ]
151+
[
152+
'reverse',
153+
a1,
154+
array => array.reverse(),
155+
unshift([2, 1, 0], `[
156+
// 2
157+
2,
158+
// 1
159+
1,
160+
// 0
161+
0
162+
]`)
163+
],
164+
[
165+
'pop',
166+
a1,
167+
array => array.pop(),
168+
unshift(2, `[
169+
// 0
170+
0,
171+
// 1
172+
1
173+
]`)
174+
]
154175
]
155176

156177
CASES.forEach(([d, a, run, e, s]) => {
@@ -171,6 +192,28 @@ CASES.forEach(([d, a, run, e, s]) => {
171192
// clean ret
172193
? [...ret]
173194
: ret,
174-
stringify(parsed, null, 2), parsed, ret)
195+
st(parsed), parsed, ret)
175196
})
176197
})
198+
199+
test('assign', t => {
200+
const str = `{
201+
// a
202+
"a": 1,
203+
// b
204+
"b": 2
205+
}`
206+
207+
const parsed = parse(str)
208+
209+
t.is(st(assign({}, parsed)), str)
210+
t.is(st(assign({})), '{}')
211+
212+
t.is(st(assign({}, parsed, ['a', 'c'])), `{
213+
// a
214+
"a": 1
215+
}`)
216+
217+
t.throws(() => assign({}, parsed, false), /keys/)
218+
t.throws(() => assign(), /convert/)
219+
})

0 commit comments

Comments
 (0)