index.js 2.9 KB

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