index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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. var empty = [];
  10. var $concatApply = isConcatSpreadable ? callBind.apply($concat, empty) : null;
  11. var $concatCall = isConcatSpreadable ? null : callBind($concat, empty);
  12. var isArray = isConcatSpreadable ? require('isarray') : null;
  13. module.exports = isConcatSpreadable
  14. // eslint-disable-next-line no-unused-vars
  15. ? function safeArrayConcat(item) {
  16. for (var i = 0; i < arguments.length; i += 1) {
  17. var arg = arguments[i];
  18. if (arg && typeof arg === 'object' && typeof arg[isConcatSpreadable] === 'boolean') {
  19. if (!empty[isConcatSpreadable]) {
  20. empty[isConcatSpreadable] = true;
  21. }
  22. var arr = isArray(arg) ? $slice(arg) : [arg];
  23. arr[isConcatSpreadable] = true; // shadow the property. TODO: use [[Define]]
  24. arguments[i] = arr;
  25. }
  26. }
  27. return $concatApply(arguments);
  28. }
  29. : $concatCall;