index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. var isArray = isConcatSpreadable ? require('isarray') : null;
  12. /** @type {typeof Array.prototype.concat} */
  13. var safeConcat = isConcatSpreadable
  14. // eslint-disable-next-line no-unused-vars
  15. ? function safeArrayConcat(item) {
  16. for (var i = 0; i < arguments.length; i += 1) {
  17. /** @type {typeof item} */ var arg = arguments[i];
  18. // @ts-expect-error ts(2538) see https://github.com/microsoft/TypeScript/issues/9998#issuecomment-1890787975; works if `const`
  19. if (arg && typeof arg === 'object' && typeof arg[isConcatSpreadable] === 'boolean') {
  20. // @ts-expect-error ts(7015) TS doesn't yet support Symbol indexing
  21. if (!empty[isConcatSpreadable]) {
  22. // @ts-expect-error ts(7015) TS doesn't yet support Symbol indexing
  23. empty[isConcatSpreadable] = true;
  24. }
  25. // @ts-expect-error ts(2721) ts(18047) not sure why TS can't figure out this can't be nul
  26. /** @type {T[]} */ var arr = isArray(arg) ? $slice(arg) : [arg];
  27. // @ts-expect-error ts(7015) TS can't handle expandos on an array
  28. arr[isConcatSpreadable] = true; // shadow the property. TODO: use [[Define]]
  29. arguments[i] = arr;
  30. }
  31. }
  32. // @ts-expect-error ts(2345) TS doesn't understand that apply can take an arguments object
  33. return $concatApply(arguments);
  34. }
  35. : callBind($concat, empty);
  36. module.exports = safeConcat;