mirror of
https://github.com/hanzoai/core-js.git
synced 2026-09-24 16:58:15 +00:00
38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
import { GLOBAL, STRICT } from '../helpers/constants.js';
|
|
|
|
import Symbol from 'core-js-pure/es/symbol';
|
|
import isArray from 'core-js-pure/es/array/is-array';
|
|
import slice from 'core-js-pure/es/array/slice';
|
|
|
|
QUnit.test('Array#slice', assert => {
|
|
assert.isFunction(slice);
|
|
let array = ['1', '2', '3', '4', '5'];
|
|
assert.deepEqual(slice(array), array);
|
|
assert.deepEqual(slice(array, 1, 3), ['2', '3']);
|
|
assert.deepEqual(slice(array, 1, undefined), ['2', '3', '4', '5']);
|
|
assert.deepEqual(slice(array, 1, -1), ['2', '3', '4']);
|
|
assert.deepEqual(slice(array, -2, -1), ['4']);
|
|
assert.deepEqual(slice(array, -2, -3), []);
|
|
const string = '12345';
|
|
assert.deepEqual(slice(string), array);
|
|
assert.deepEqual(slice(string, 1, 3), ['2', '3']);
|
|
assert.deepEqual(slice(string, 1, undefined), ['2', '3', '4', '5']);
|
|
assert.deepEqual(slice(string, 1, -1), ['2', '3', '4']);
|
|
assert.deepEqual(slice(string, -2, -1), ['4']);
|
|
assert.deepEqual(slice(string, -2, -3), []);
|
|
const list = GLOBAL.document && document.body && document.body.childNodes;
|
|
if (list) {
|
|
assert.notThrows(() => isArray(slice(list)), 'works with NodeList');
|
|
}
|
|
if (STRICT) {
|
|
assert.throws(() => slice(null), TypeError);
|
|
assert.throws(() => slice(undefined), TypeError);
|
|
}
|
|
array = [];
|
|
// eslint-disable-next-line object-shorthand -- constructor
|
|
array.constructor = { [Symbol.species]: function () {
|
|
return { foo: 1 };
|
|
} };
|
|
assert.same(slice(array).foo, 1, '@@species');
|
|
});
|