shim.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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) {
  27. result[i] = val;
  28. });
  29. } else {
  30. result[0] = this;
  31. }
  32. forEach.call(arguments, function (arg) {
  33. var base;
  34. if (isSpreadable(arg)) {
  35. base = result.length;
  36. result.length += toPosInt(arg.length);
  37. forEach.call(arg, function (val, i) {
  38. result[base + i] = val;
  39. });
  40. return;
  41. }
  42. result.push(arg);
  43. });
  44. return result;
  45. };