forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes.array.splice.js
44 lines (42 loc) · 1.61 KB
/
es.array.splice.js
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
import { REDEFINABLE_ARRAY_LENGTH_DESCRIPTOR, STRICT } from '../helpers/constants';
import Symbol from 'core-js-pure/es/symbol';
import splice from 'core-js-pure/es/array/splice';
import defineProperty from 'core-js-pure/es/object/define-property';
QUnit.test('Array#splice', assert => {
assert.isFunction(splice);
let array = [1, 2, 3, 4, 5];
assert.deepEqual(splice(array, 2), [3, 4, 5]);
assert.deepEqual(array, [1, 2]);
array = [1, 2, 3, 4, 5];
assert.deepEqual(splice(array, -2), [4, 5]);
assert.deepEqual(array, [1, 2, 3]);
array = [1, 2, 3, 4, 5];
assert.deepEqual(splice(array, 2, 2), [3, 4]);
assert.deepEqual(array, [1, 2, 5]);
array = [1, 2, 3, 4, 5];
assert.deepEqual(splice(array, 2, -2), []);
assert.deepEqual(array, [1, 2, 3, 4, 5]);
array = [1, 2, 3, 4, 5];
assert.deepEqual(splice(array, 2, 2, 6, 7), [3, 4]);
assert.deepEqual(array, [1, 2, 6, 7, 5]);
// ES6 semantics
array = [0, 1, 2];
assert.deepEqual(splice(array, 0), [0, 1, 2]);
array = [0, 1, 2];
assert.deepEqual(splice(array, 1), [1, 2]);
array = [0, 1, 2];
assert.deepEqual(splice(array, 2), [2]);
if (STRICT) {
if (REDEFINABLE_ARRAY_LENGTH_DESCRIPTOR) {
assert.throws(() => splice(defineProperty([1, 2, 3], 'length', { writable: false }), 1, 1), TypeError, 'non-writable length');
}
assert.throws(() => splice(null), TypeError);
assert.throws(() => splice(undefined), TypeError);
}
array = [];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.same(splice(array).foo, 1, '@@species');
});