fei.wang
10 天以前 ae7b22322555448d95fd56f505bafa325c167a26
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
 
var test = require('tape');
var mockProperty = require('mock-property');
var hasSymbols = require('has-symbols/shams')();
var isConcatSpreadable = hasSymbols && Symbol.isConcatSpreadable;
var species = hasSymbols && Symbol.species;
 
var boundFnsHaveConfigurableLengths = require('set-function-length/env').boundFnsHaveConfigurableLengths;
 
var safeConcat = require('../');
 
test('safe-array-concat', function (t) {
    t.equal(typeof safeConcat, 'function', 'is a function');
    t.equal(
        safeConcat.length,
        boundFnsHaveConfigurableLengths ? 1 : 0,
        'has a length of ' + (boundFnsHaveConfigurableLengths ? 1 : '0 (function lengths are not configurable)')
    );
 
    t.deepEqual(
        // eslint-disable-next-line no-extra-parens
        safeConcat(/** @type {(string | number | number[])[]} */ ([1, 2]), [3, 4], 'foo', 5, 6, [[7]]),
        [1, 2, 3, 4, 'foo', 5, 6, [7]],
        'works with flat and nested arrays'
    );
 
    t.deepEqual(
        safeConcat(undefined, 1, 2),
        [undefined, 1, 2],
        'first item as undefined is not the concat receiver, which would throw via ToObject'
    );
    t.deepEqual(
        safeConcat(null, 1, 2),
        [null, 1, 2],
        'first item as null is not the concat receiver, which would throw via ToObject'
    );
 
    var arr = [1, 2];
    arr.constructor = function C() {
        return { args: arguments };
    };
    t.deepEqual(
        safeConcat(arr, 3, 4),
        [1, 2, 3, 4],
        'first item as an array with a nonArray .constructor; ignores constructor'
    );
 
    t.test('has Symbol.species', { skip: !species }, function (st) {
        var speciesArr = [1, 2];
        // @ts-expect-error ts(2740) TS's `constructor` type requires a function
        speciesArr.constructor = {};
        // @ts-expect-error ts(2538) TS can't type narrow from tape's `skip`
        speciesArr.constructor[species] = function Species() {
            return { args: arguments };
        };
 
        st.deepEqual(
            safeConcat(speciesArr, 3, 4),
            [1, 2, 3, 4],
            'first item as an array with a .constructor object with a Symbol.species; ignores constructor and species'
        );
 
        st.end();
    });
 
    t.test('has isConcatSpreadable', { skip: !isConcatSpreadable }, function (st) {
        // TS can't type narrow from tape's `skip`
        if (isConcatSpreadable) {
            st.teardown(mockProperty(
                // eslint-disable-next-line no-extra-parens
                /** @type {Record<PropertyKey, unknown>} */ (/** @type {unknown} */ (String.prototype)),
                isConcatSpreadable,
                { value: true }
            ));
 
            var nonSpreadable = [1, 2];
            // @ts-expect-error ts(7015) TS can't handle expandos on an array
            nonSpreadable[isConcatSpreadable] = false;
 
            st.deepEqual(
                safeConcat(nonSpreadable, 3, 4, 'foo', Object('bar')),
                [1, 2, 3, 4, 'foo', Object('bar')],
                'a non-concat-spreadable array is spreaded, and a concat-spreadable String is not spreaded'
            );
 
            st.teardown(mockProperty(Array.prototype, isConcatSpreadable, { value: false }));
 
            st.deepEqual(
                safeConcat([1, 2], 3, 4, 'foo', Object('bar')),
                [1, 2, 3, 4, 'foo', Object('bar')],
                'all arrays marked non-concat-spreadable are still spreaded, and a concat-spreadable String is not spreaded'
            );
        }
 
        st.end();
    });
 
    t.end();
});