1 | 'use strict';
|
---|
2 |
|
---|
3 | var test = require('tape');
|
---|
4 | var mockProperty = require('mock-property');
|
---|
5 | var hasSymbols = require('has-symbols/shams')();
|
---|
6 | var isConcatSpreadable = hasSymbols && Symbol.isConcatSpreadable;
|
---|
7 | var species = hasSymbols && Symbol.species;
|
---|
8 |
|
---|
9 | var boundFnsHaveConfigurableLengths = require('set-function-length/env').boundFnsHaveConfigurableLengths;
|
---|
10 |
|
---|
11 | var safeConcat = require('../');
|
---|
12 |
|
---|
13 | test('safe-array-concat', function (t) {
|
---|
14 | t.equal(typeof safeConcat, 'function', 'is a function');
|
---|
15 | t.equal(
|
---|
16 | safeConcat.length,
|
---|
17 | boundFnsHaveConfigurableLengths ? 1 : 0,
|
---|
18 | 'has a length of ' + (boundFnsHaveConfigurableLengths ? 1 : '0 (function lengths are not configurable)')
|
---|
19 | );
|
---|
20 |
|
---|
21 | t.deepEqual(
|
---|
22 | // eslint-disable-next-line no-extra-parens
|
---|
23 | safeConcat(/** @type {(string | number | number[])[]} */ ([1, 2]), [3, 4], 'foo', 5, 6, [[7]]),
|
---|
24 | [1, 2, 3, 4, 'foo', 5, 6, [7]],
|
---|
25 | 'works with flat and nested arrays'
|
---|
26 | );
|
---|
27 |
|
---|
28 | t.deepEqual(
|
---|
29 | safeConcat(undefined, 1, 2),
|
---|
30 | [undefined, 1, 2],
|
---|
31 | 'first item as undefined is not the concat receiver, which would throw via ToObject'
|
---|
32 | );
|
---|
33 | t.deepEqual(
|
---|
34 | safeConcat(null, 1, 2),
|
---|
35 | [null, 1, 2],
|
---|
36 | 'first item as null is not the concat receiver, which would throw via ToObject'
|
---|
37 | );
|
---|
38 |
|
---|
39 | var arr = [1, 2];
|
---|
40 | arr.constructor = function C() {
|
---|
41 | return { args: arguments };
|
---|
42 | };
|
---|
43 | t.deepEqual(
|
---|
44 | safeConcat(arr, 3, 4),
|
---|
45 | [1, 2, 3, 4],
|
---|
46 | 'first item as an array with a nonArray .constructor; ignores constructor'
|
---|
47 | );
|
---|
48 |
|
---|
49 | t.test('has Symbol.species', { skip: !species }, function (st) {
|
---|
50 | var speciesArr = [1, 2];
|
---|
51 | // @ts-expect-error ts(2740) TS's `constructor` type requires a function
|
---|
52 | speciesArr.constructor = {};
|
---|
53 | // @ts-expect-error ts(2538) TS can't type narrow from tape's `skip`
|
---|
54 | speciesArr.constructor[species] = function Species() {
|
---|
55 | return { args: arguments };
|
---|
56 | };
|
---|
57 |
|
---|
58 | st.deepEqual(
|
---|
59 | safeConcat(speciesArr, 3, 4),
|
---|
60 | [1, 2, 3, 4],
|
---|
61 | 'first item as an array with a .constructor object with a Symbol.species; ignores constructor and species'
|
---|
62 | );
|
---|
63 |
|
---|
64 | st.end();
|
---|
65 | });
|
---|
66 |
|
---|
67 | t.test('has isConcatSpreadable', { skip: !isConcatSpreadable }, function (st) {
|
---|
68 | // TS can't type narrow from tape's `skip`
|
---|
69 | if (isConcatSpreadable) {
|
---|
70 | st.teardown(mockProperty(String.prototype, isConcatSpreadable, { value: true }));
|
---|
71 |
|
---|
72 | var nonSpreadable = [1, 2];
|
---|
73 | // @ts-expect-error ts(7015) TS can't handle expandos on an array
|
---|
74 | nonSpreadable[isConcatSpreadable] = false;
|
---|
75 |
|
---|
76 | st.deepEqual(
|
---|
77 | safeConcat(nonSpreadable, 3, 4, 'foo', Object('bar')),
|
---|
78 | [1, 2, 3, 4, 'foo', Object('bar')],
|
---|
79 | 'a non-concat-spreadable array is spreaded, and a concat-spreadable String is not spreaded'
|
---|
80 | );
|
---|
81 |
|
---|
82 | st.teardown(mockProperty(Array.prototype, isConcatSpreadable, { value: false }));
|
---|
83 |
|
---|
84 | st.deepEqual(
|
---|
85 | safeConcat([1, 2], 3, 4, 'foo', Object('bar')),
|
---|
86 | [1, 2, 3, 4, 'foo', Object('bar')],
|
---|
87 | 'all arrays marked non-concat-spreadable are still spreaded, and a concat-spreadable String is not spreaded'
|
---|
88 | );
|
---|
89 | }
|
---|
90 |
|
---|
91 | st.end();
|
---|
92 | });
|
---|
93 |
|
---|
94 | t.end();
|
---|
95 | });
|
---|