array.from.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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; // 2. Let items be ToObject(arrayLike).
  41. var items = Object(arrayLike); // 3. ReturnIfAbrupt(items).
  42. if (arrayLike == null) {
  43. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  44. } // 4. If mapfn is undefined, then let mapping be false.
  45. // const mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  46. if (typeof mapFn !== "undefined") {
  47. // 5. else
  48. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  49. if (!isCallable(mapFn)) {
  50. throw new TypeError("Array.from: when provided, the second argument must be a function");
  51. }
  52. } // 10. Let lenValue be Get(items, "length").
  53. // 11. Let len be ToLength(lenValue).
  54. var len = toLength(items.length); // 13. If IsConstructor(C) is true, then
  55. // 13. a. Let A be the result of calling the [[Construct]] internal method
  56. // of C with an argument list containing the single item len.
  57. // 14. a. Else, Let A be ArrayCreate(len).
  58. var A = isCallable(C) ? Object(new C(len)) : new Array(len); // 16. Let k be 0.
  59. var k = 0; // 17. Repeat, while k < len… (also steps a - h)
  60. var kValue;
  61. while (k < len) {
  62. kValue = items[k];
  63. if (mapFn) {
  64. A[k] = mapFn(kValue, k);
  65. } else {
  66. A[k] = kValue;
  67. }
  68. k += 1;
  69. } // 18. Let putStatus be Put(A, "length", len, true).
  70. A.length = len; // 20. Return A.
  71. return A;
  72. }
  73. //# sourceMappingURL=array.from.js.map