shim.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var isPlainArray = require('../../is-plain-array')
  3. , toPosInt = require('../../../number/to-pos-integer')
  4. , isObject = require('../../../object/is-object')
  5. , isArray = Array.isArray, concat = Array.prototype.concat
  6. , forEach = Array.prototype.forEach
  7. , isSpreadable;
  8. isSpreadable = function (value) {
  9. if (!value) return false;
  10. if (!isObject(value)) return false;
  11. if (value['@@isConcatSpreadable'] !== undefined) {
  12. return Boolean(value['@@isConcatSpreadable']);
  13. }
  14. return isArray(value);
  15. };
  16. module.exports = function (item/*, …items*/) {
  17. var result;
  18. if (!this || !isArray(this) || isPlainArray(this)) {
  19. return concat.apply(this, arguments);
  20. }
  21. result = new this.constructor(this.length);
  22. forEach.call(this, function (val, i) { result[i] = val; });
  23. forEach.call(arguments, function (arg) {
  24. var base;
  25. if (isSpreadable(arg)) {
  26. base = result.length;
  27. result.length += toPosInt(arg.length);
  28. forEach.call(arg, function (val, i) { result[base + i] = val; });
  29. return;
  30. }
  31. result.push(arg);
  32. });
  33. return result;
  34. };