array.from.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = arrayFrom;
  4. /**
  5. * @source {https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Polyfill}
  6. * but without thisArg (too hard to type, no need to `this`)
  7. */
  8. var toStr = Object.prototype.toString;
  9. function isCallable(fn) {
  10. return typeof fn === "function" || toStr.call(fn) === "[object Function]";
  11. }
  12. function toInteger(value) {
  13. var number = Number(value);
  14. if (isNaN(number)) {
  15. return 0;
  16. }
  17. if (number === 0 || !isFinite(number)) {
  18. return number;
  19. }
  20. return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
  21. }
  22. var maxSafeInteger = Math.pow(2, 53) - 1;
  23. function toLength(value) {
  24. var len = toInteger(value);
  25. return Math.min(Math.max(len, 0), maxSafeInteger);
  26. }
  27. /**
  28. * Creates an array from an iterable object.
  29. * @param iterable An iterable object to convert to an array.
  30. */
  31. /**
  32. * Creates an array from an iterable object.
  33. * @param iterable An iterable object to convert to an array.
  34. * @param mapfn A mapping function to call on every element of the array.
  35. * @param thisArg Value of 'this' used to invoke the mapfn.
  36. */
  37. function arrayFrom(arrayLike, mapFn) {
  38. // 1. Let C be the this value.
  39. // edit(@eps1lon): we're not calling it as Array.from
  40. var C = Array;
  41. // 2. Let items be ToObject(arrayLike).
  42. var items = Object(arrayLike);
  43. // 3. ReturnIfAbrupt(items).
  44. if (arrayLike == null) {
  45. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  46. }
  47. // 4. If mapfn is undefined, then let mapping be false.
  48. // const mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  49. if (typeof mapFn !== "undefined") {
  50. // 5. else
  51. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  52. if (!isCallable(mapFn)) {
  53. throw new TypeError("Array.from: when provided, the second argument must be a function");
  54. }
  55. }
  56. // 10. Let lenValue be Get(items, "length").
  57. // 11. Let len be ToLength(lenValue).
  58. var len = toLength(items.length);
  59. // 13. If IsConstructor(C) is true, then
  60. // 13. a. Let A be the result of calling the [[Construct]] internal method
  61. // of C with an argument list containing the single item len.
  62. // 14. a. Else, Let A be ArrayCreate(len).
  63. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  64. // 16. Let k be 0.
  65. var k = 0;
  66. // 17. Repeat, while k < len… (also steps a - h)
  67. var kValue;
  68. while (k < len) {
  69. kValue = items[k];
  70. if (mapFn) {
  71. A[k] = mapFn(kValue, k);
  72. } else {
  73. A[k] = kValue;
  74. }
  75. k += 1;
  76. }
  77. // 18. Let putStatus be Put(A, "length", len, true).
  78. A.length = len;
  79. // 20. Return A.
  80. return A;
  81. }
  82. //# sourceMappingURL=array.from.js.map