index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $concat = GetIntrinsic('%Array.prototype.concat%');
  4. var callBind = require('call-bind');
  5. var callBound = require('call-bind/callBound');
  6. var $slice = callBound('Array.prototype.slice');
  7. var hasSymbols = require('has-symbols/shams')();
  8. var isConcatSpreadable = hasSymbols && Symbol.isConcatSpreadable;
  9. /** @type {never[]} */ var empty = [];
  10. var $concatApply = isConcatSpreadable ? callBind.apply($concat, empty) : null;
  11. // eslint-disable-next-line no-extra-parens
  12. var isArray = isConcatSpreadable ? /** @type {(value: unknown) => value is unknown[]} */ (require('isarray')) : null;
  13. /** @type {import('.')} */
  14. module.exports = isConcatSpreadable
  15. // eslint-disable-next-line no-unused-vars
  16. ? function safeArrayConcat(item) {
  17. for (var i = 0; i < arguments.length; i += 1) {
  18. /** @type {typeof item} */ var arg = arguments[i];
  19. // @ts-expect-error ts(2538) see https://github.com/microsoft/TypeScript/issues/9998#issuecomment-1890787975; works if `const`
  20. if (arg && typeof arg === 'object' && typeof arg[isConcatSpreadable] === 'boolean') {
  21. // @ts-expect-error ts(7015) TS doesn't yet support Symbol indexing
  22. if (!empty[isConcatSpreadable]) {
  23. // @ts-expect-error ts(7015) TS doesn't yet support Symbol indexing
  24. empty[isConcatSpreadable] = true;
  25. }
  26. // @ts-expect-error ts(2721) ts(18047) not sure why TS can't figure out this can't be null
  27. var arr = isArray(arg) ? $slice(arg) : [arg];
  28. // @ts-expect-error ts(7015) TS can't handle expandos on an array
  29. arr[isConcatSpreadable] = true; // shadow the property. TODO: use [[Define]]
  30. arguments[i] = arr;
  31. }
  32. }
  33. // @ts-expect-error ts(2345) https://github.com/microsoft/TypeScript/issues/57164 TS doesn't understand that apply can take an arguments object
  34. return $concatApply(arguments);
  35. }
  36. : callBind($concat, empty);