shim.js 1.2 KB

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