array.from.mjs 2.6 KB

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