index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(
  60. // eslint-disable-next-line no-extra-parens
  61. /** @type {Record<PropertyKey, unknown>} */ (/** @type {unknown} */ (String.prototype)),
  62. isConcatSpreadable,
  63. { value: true }
  64. ));
  65. var nonSpreadable = [1, 2];
  66. // @ts-expect-error ts(7015) TS can't handle expandos on an array
  67. nonSpreadable[isConcatSpreadable] = false;
  68. st.deepEqual(
  69. safeConcat(nonSpreadable, 3, 4, 'foo', Object('bar')),
  70. [1, 2, 3, 4, 'foo', Object('bar')],
  71. 'a non-concat-spreadable array is spreaded, and a concat-spreadable String is not spreaded'
  72. );
  73. st.teardown(mockProperty(Array.prototype, isConcatSpreadable, { value: false }));
  74. st.deepEqual(
  75. safeConcat([1, 2], 3, 4, 'foo', Object('bar')),
  76. [1, 2, 3, 4, 'foo', Object('bar')],
  77. 'all arrays marked non-concat-spreadable are still spreaded, and a concat-spreadable String is not spreaded'
  78. );
  79. }
  80. st.end();
  81. });
  82. t.end();
  83. });