shim.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var iteratorSymbol = require("es6-symbol").iterator
  3. , isArguments = require("../../function/is-arguments")
  4. , isFunction = require("../../function/is-function")
  5. , toPosInt = require("../../number/to-pos-integer")
  6. , callable = require("../../object/valid-callable")
  7. , validValue = require("../../object/valid-value")
  8. , isValue = require("../../object/is-value")
  9. , isString = require("../../string/is-string")
  10. , isArray = Array.isArray
  11. , call = Function.prototype.call
  12. , desc = { configurable: true, enumerable: true, writable: true, value: null }
  13. , defineProperty = Object.defineProperty;
  14. // eslint-disable-next-line complexity
  15. module.exports = function (arrayLike /*, mapFn, thisArg*/) {
  16. var mapFn = arguments[1]
  17. , thisArg = arguments[2]
  18. , Context
  19. , i
  20. , j
  21. , arr
  22. , length
  23. , code
  24. , iterator
  25. , result
  26. , getIterator
  27. , value;
  28. arrayLike = Object(validValue(arrayLike));
  29. if (isValue(mapFn)) callable(mapFn);
  30. if (!this || this === Array || !isFunction(this)) {
  31. // Result: Plain array
  32. if (!mapFn) {
  33. if (isArguments(arrayLike)) {
  34. // Source: Arguments
  35. length = arrayLike.length;
  36. if (length !== 1) return Array.apply(null, arrayLike);
  37. arr = new Array(1);
  38. arr[0] = arrayLike[0];
  39. return arr;
  40. }
  41. if (isArray(arrayLike)) {
  42. // Source: Array
  43. arr = new Array(length = arrayLike.length);
  44. for (i = 0; i < length; ++i) arr[i] = arrayLike[i];
  45. return arr;
  46. }
  47. }
  48. arr = [];
  49. } else {
  50. // Result: Non plain array
  51. Context = this;
  52. }
  53. if (!isArray(arrayLike)) {
  54. if ((getIterator = arrayLike[iteratorSymbol]) !== undefined) {
  55. // Source: Iterator
  56. iterator = callable(getIterator).call(arrayLike);
  57. if (Context) arr = new Context();
  58. result = iterator.next();
  59. i = 0;
  60. while (!result.done) {
  61. value = mapFn ? call.call(mapFn, thisArg, result.value, i) : result.value;
  62. if (Context) {
  63. desc.value = value;
  64. defineProperty(arr, i, desc);
  65. } else {
  66. arr[i] = value;
  67. }
  68. result = iterator.next();
  69. ++i;
  70. }
  71. length = i;
  72. } else if (isString(arrayLike)) {
  73. // Source: String
  74. length = arrayLike.length;
  75. if (Context) arr = new Context();
  76. for (i = 0, j = 0; i < length; ++i) {
  77. value = arrayLike[i];
  78. if (i + 1 < length) {
  79. code = value.charCodeAt(0);
  80. // eslint-disable-next-line max-depth
  81. if (code >= 0xd800 && code <= 0xdbff) value += arrayLike[++i];
  82. }
  83. value = mapFn ? call.call(mapFn, thisArg, value, j) : value;
  84. if (Context) {
  85. desc.value = value;
  86. defineProperty(arr, j, desc);
  87. } else {
  88. arr[j] = value;
  89. }
  90. ++j;
  91. }
  92. length = j;
  93. }
  94. }
  95. if (length === undefined) {
  96. // Source: array or array-like
  97. length = toPosInt(arrayLike.length);
  98. if (Context) arr = new Context(length);
  99. for (i = 0; i < length; ++i) {
  100. value = mapFn ? call.call(mapFn, thisArg, arrayLike[i], i) : arrayLike[i];
  101. if (Context) {
  102. desc.value = value;
  103. defineProperty(arr, i, desc);
  104. } else {
  105. arr[i] = value;
  106. }
  107. }
  108. }
  109. if (Context) {
  110. desc.value = null;
  111. arr.length = length;
  112. }
  113. return arr;
  114. };