Skip to content

Commit 3e95614

Browse files
committed
array: fixes slice, unshift, shift
1 parent d1c6465 commit 3e95614

File tree

3 files changed

+142
-17
lines changed

3 files changed

+142
-17
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/*.test.js"
40+
"test/array.test.js"
4141
]
4242
},
4343
"author": "kaelzhang",

src/array.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ class CommentArray extends Array {
133133
// - deleteCount + items.length
134134

135135
// We should avoid `splice(begin, deleteCount, ...items)`,
136-
// because `splice(0, undefined)` is not equivalent to `splice(0)`
136+
// because `splice(0, undefined)` is not equivalent to `splice(0)`,
137+
// as well as:
138+
// - slice
137139
splice (...args) {
138140
const {length} = this
139141
const ret = super.splice(...args)
@@ -177,30 +179,43 @@ class CommentArray extends Array {
177179
return ret
178180
}
179181

180-
slice (begin, before) {
181-
const array = new CommentArray(...super.slice(begin, before))
182-
if (begin < 0 && before === UNDEFINED) {
182+
slice (...args) {
183+
const {length} = this
184+
const array = super.slice(...args)
185+
if (!array.length) {
183186
return new CommentArray()
184187
}
185188

189+
let [begin, before] = args
190+
191+
// Ref:
192+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
186193
if (before === UNDEFINED) {
187-
before = this.length
194+
before = length
195+
} else if (before < 0) {
196+
before += length
197+
}
188198

189-
if (begin < 0) {
190-
begin += before
191-
}
199+
if (begin < 0) {
200+
begin += length
201+
} else if (begin === UNDEFINED) {
202+
begin = 0
192203
}
193204

194-
move_comments(array, this, begin, before - begin, begin)
205+
move_comments(array, this, begin, before - begin, - begin)
206+
195207
return array
196208
}
197209

198210
unshift (...items) {
211+
const {length} = this
199212
const ret = super.unshift(...items)
200-
const {length} = items
213+
const {
214+
length: items_length
215+
} = items
201216

202-
if (length > 0) {
203-
move_comments(this, this, length, this.length, length, true)
217+
if (items_length > 0) {
218+
move_comments(this, this, 0, length, items_length, true)
204219
}
205220

206221
return ret
@@ -210,7 +225,7 @@ class CommentArray extends Array {
210225
const ret = super.shift()
211226
const {length} = this
212227

213-
move_comments(this, this, 1, length - 1, - 1, true)
228+
move_comments(this, this, 1, length, - 1, true)
214229

215230
return ret
216231
}

test/array.test.js

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const test = require('ava')
2-
const {isFunction} = require('core-util-is')
2+
const {
3+
isFunction, isObject, isString, isArray
4+
} = require('core-util-is')
35
const {parse, stringify} = require('../src')
46

57
const a1 = `[
@@ -18,6 +20,27 @@ const a2 = `[
1820
2
1921
]`
2022

23+
// ret: return value to expect
24+
const texpect = (t, ret, str, r, rr) => {
25+
if (isObject(ret)) {
26+
t.deepEqual(r, ret)
27+
} else {
28+
t.is(r, ret)
29+
}
30+
31+
if (isString(rr)) {
32+
t.is(rr, str)
33+
} else {
34+
t.is(stringify(rr, null, 2), str)
35+
}
36+
}
37+
38+
const slice = (ret, str) =>
39+
(t, r, _, __, rr) => texpect(t, ret, str, r, rr)
40+
41+
const unshift = (ret, str) =>
42+
(t, r, s) => texpect(t, ret, str, r, s)
43+
2144
const CASES = [
2245
[
2346
'splice(0, 1)',
@@ -46,7 +69,88 @@ const CASES = [
4669
array => array.splice(0, undefined),
4770
[],
4871
a1
49-
]
72+
],
73+
[
74+
'slice(0)',
75+
a1,
76+
array => array.slice(0),
77+
[0, 1, 2],
78+
a1
79+
],
80+
[
81+
'slice(-1)',
82+
a1,
83+
array => array.slice(- 1),
84+
slice([2], `[
85+
// 2
86+
2
87+
]`)
88+
],
89+
[
90+
'slice(3)',
91+
a1,
92+
array => array.slice(3),
93+
slice([], '[]')
94+
],
95+
[
96+
'slice(undefined, undefined)',
97+
a1,
98+
array => array.slice(),
99+
slice([0, 1, 2], a1)
100+
],
101+
[
102+
'slice(0, - 2)',
103+
a1,
104+
array => array.slice(0, - 2),
105+
slice([0], `[
106+
// 0
107+
0
108+
]`)
109+
],
110+
[
111+
'unshift()',
112+
a1,
113+
array => array.unshift(),
114+
unshift(3, a1)
115+
],
116+
[
117+
'unshift(- 1)',
118+
a1,
119+
array => array.unshift(- 1),
120+
unshift(4, `[
121+
-1,
122+
// 0
123+
0,
124+
// 1
125+
1,
126+
// 2
127+
2
128+
]`)
129+
],
130+
[
131+
'shift',
132+
a1,
133+
array => array.shift(),
134+
unshift(0, `[
135+
// 1
136+
1,
137+
// 2
138+
2
139+
]`)
140+
],
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+
// ]
50154
]
51155

52156
CASES.forEach(([d, a, run, e, s]) => {
@@ -61,6 +165,12 @@ CASES.forEach(([d, a, run, e, s]) => {
61165
tt.is(str, s)
62166
}
63167

64-
expect(t, [...ret], stringify(parsed, null, 2), parsed, ret)
168+
expect(
169+
t,
170+
isArray(ret)
171+
// clean ret
172+
? [...ret]
173+
: ret,
174+
stringify(parsed, null, 2), parsed, ret)
65175
})
66176
})

0 commit comments

Comments
 (0)