array.from.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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;
  38. // 2. Let items be ToObject(arrayLike).
  39. var items = Object(arrayLike);
  40. // 3. ReturnIfAbrupt(items).
  41. if (arrayLike == null) {
  42. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  43. }
  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. }
  53. // 10. Let lenValue be Get(items, "length").
  54. // 11. Let len be ToLength(lenValue).
  55. var len = toLength(items.length);
  56. // 13. If IsConstructor(C) is true, then
  57. // 13. a. Let A be the result of calling the [[Construct]] internal method
  58. // of C with an argument list containing the single item len.
  59. // 14. a. Else, Let A be ArrayCreate(len).
  60. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  61. // 16. Let k be 0.
  62. var k = 0;
  63. // 17. Repeat, while k < len… (also steps a - h)
  64. var kValue;
  65. while (k < len) {
  66. kValue = items[k];
  67. if (mapFn) {
  68. A[k] = mapFn(kValue, k);
  69. } else {
  70. A[k] = kValue;
  71. }
  72. k += 1;
  73. }
  74. // 18. Let putStatus be Put(A, "length", len, true).
  75. A.length = len;
  76. // 20. Return A.
  77. return A;
  78. }
  79. //# sourceMappingURL=array.from.mjs.map