forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes.array.slice.js
39 lines (38 loc) · 1.54 KB
/
es.array.slice.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
import { GLOBAL, STRICT } from '../helpers/constants.js';
QUnit.test('Array#slice', assert => {
const { slice } = Array.prototype;
const { isArray } = Array;
assert.isFunction(slice);
assert.arity(slice, 2);
assert.name(slice, 'slice');
assert.looksNative(slice);
assert.nonEnumerable(Array.prototype, 'slice');
let array = ['1', '2', '3', '4', '5'];
assert.deepEqual(array.slice(), array);
assert.deepEqual(array.slice(1, 3), ['2', '3']);
assert.deepEqual(array.slice(1, undefined), ['2', '3', '4', '5']);
assert.deepEqual(array.slice(1, -1), ['2', '3', '4']);
assert.deepEqual(array.slice(-2, -1), ['4']);
assert.deepEqual(array.slice(-2, -3), []);
const string = '12345';
assert.deepEqual(slice.call(string), array);
assert.deepEqual(slice.call(string, 1, 3), ['2', '3']);
assert.deepEqual(slice.call(string, 1, undefined), ['2', '3', '4', '5']);
assert.deepEqual(slice.call(string, 1, -1), ['2', '3', '4']);
assert.deepEqual(slice.call(string, -2, -1), ['4']);
assert.deepEqual(slice.call(string, -2, -3), []);
const list = GLOBAL.document && document.body && document.body.childNodes;
if (list) {
assert.notThrows(() => isArray(slice.call(list)), 'works on NodeList');
}
if (STRICT) {
assert.throws(() => slice.call(null), TypeError);
assert.throws(() => slice.call(undefined), TypeError);
}
array = [];
// eslint-disable-next-line object-shorthand -- constructor
array.constructor = { [Symbol.species]: function () {
return { foo: 1 };
} };
assert.same(array.slice().foo, 1, '@@species');
});