1e3bdcfe40fb3397a0b6fc032d5658e4.json 7.8 KB

1
  1. {"ast":null,"code":"/**\n * @source {https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Polyfill}\n * but without thisArg (too hard to type, no need to `this`)\n */\nvar toStr = Object.prototype.toString;\n\nfunction isCallable(fn) {\n return typeof fn === \"function\" || toStr.call(fn) === \"[object Function]\";\n}\n\nfunction toInteger(value) {\n var number = Number(value);\n\n if (isNaN(number)) {\n return 0;\n }\n\n if (number === 0 || !isFinite(number)) {\n return number;\n }\n\n return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));\n}\n\nvar maxSafeInteger = Math.pow(2, 53) - 1;\n\nfunction toLength(value) {\n var len = toInteger(value);\n return Math.min(Math.max(len, 0), maxSafeInteger);\n}\n/**\n * Creates an array from an iterable object.\n * @param iterable An iterable object to convert to an array.\n */\n\n/**\n * Creates an array from an iterable object.\n * @param iterable An iterable object to convert to an array.\n * @param mapfn A mapping function to call on every element of the array.\n * @param thisArg Value of 'this' used to invoke the mapfn.\n */\n\n\nexport default function arrayFrom(arrayLike, mapFn) {\n // 1. Let C be the this value.\n // edit(@eps1lon): we're not calling it as Array.from\n var C = Array; // 2. Let items be ToObject(arrayLike).\n\n var items = Object(arrayLike); // 3. ReturnIfAbrupt(items).\n\n if (arrayLike == null) {\n throw new TypeError(\"Array.from requires an array-like object - not null or undefined\");\n } // 4. If mapfn is undefined, then let mapping be false.\n // const mapFn = arguments.length > 1 ? arguments[1] : void undefined;\n\n\n if (typeof mapFn !== \"undefined\") {\n // 5. else\n // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.\n if (!isCallable(mapFn)) {\n throw new TypeError(\"Array.from: when provided, the second argument must be a function\");\n }\n } // 10. Let lenValue be Get(items, \"length\").\n // 11. Let len be ToLength(lenValue).\n\n\n var len = toLength(items.length); // 13. If IsConstructor(C) is true, then\n // 13. a. Let A be the result of calling the [[Construct]] internal method\n // of C with an argument list containing the single item len.\n // 14. a. Else, Let A be ArrayCreate(len).\n\n var A = isCallable(C) ? Object(new C(len)) : new Array(len); // 16. Let k be 0.\n\n var k = 0; // 17. Repeat, while k < len… (also steps a - h)\n\n var kValue;\n\n while (k < len) {\n kValue = items[k];\n\n if (mapFn) {\n A[k] = mapFn(kValue, k);\n } else {\n A[k] = kValue;\n }\n\n k += 1;\n } // 18. Let putStatus be Put(A, \"length\", len, true).\n\n\n A.length = len; // 20. Return A.\n\n return A;\n}","map":{"version":3,"sources":["../../sources/polyfills/array.from.ts"],"names":["toStr","Object","number","Number","isNaN","isFinite","Math","maxSafeInteger","len","toInteger","C","items","arrayLike","isCallable","toLength","A","k","kValue","mapFn"],"mappings":"AAAA;AACA;AACA;AACA;AACA,IAAMA,KAAK,GAAGC,MAAM,CAANA,SAAAA,CAAd,QAAA;;AACA,SAAA,UAAA,CAAA,EAAA,EAA0C;AACzC,SAAO,OAAA,EAAA,KAAA,UAAA,IAA4BD,KAAK,CAALA,IAAAA,CAAAA,EAAAA,MAAnC,mBAAA;AACA;;AACD,SAAA,SAAA,CAAA,KAAA,EAA2C;AAC1C,MAAME,MAAM,GAAGC,MAAM,CAArB,KAAqB,CAArB;;AACA,MAAIC,KAAK,CAAT,MAAS,CAAT,EAAmB;AAClB,WAAA,CAAA;AACA;;AACD,MAAIF,MAAM,KAANA,CAAAA,IAAgB,CAACG,QAAQ,CAA7B,MAA6B,CAA7B,EAAuC;AACtC,WAAA,MAAA;AACA;;AACD,SAAO,CAACH,MAAM,GAANA,CAAAA,GAAAA,CAAAA,GAAiB,CAAlB,CAAA,IAAwBI,IAAI,CAAJA,KAAAA,CAAWA,IAAI,CAAJA,GAAAA,CAA1C,MAA0CA,CAAXA,CAA/B;AACA;;AACD,IAAMC,cAAc,GAAGD,IAAI,CAAJA,GAAAA,CAAAA,CAAAA,EAAAA,EAAAA,IAAvB,CAAA;;AACA,SAAA,QAAA,CAAA,KAAA,EAA0C;AACzC,MAAME,GAAG,GAAGC,SAAS,CAArB,KAAqB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;;AACDC,IAAAA,CAAC,IAADA,CAAAA;AAjDK,GAAA,CAmDN;;;AACAD,EAAAA,CAAC,CAADA,MAAAA,GApDM,GAoDNA,CApDM,CAqDN;;AACA,SAAA,CAAA;AACA","sourcesContent":["/**\n * @source {https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Polyfill}\n * but without thisArg (too hard to type, no need to `this`)\n */\nconst toStr = Object.prototype.toString;\nfunction isCallable(fn: unknown): boolean {\n\treturn typeof fn === \"function\" || toStr.call(fn) === \"[object Function]\";\n}\nfunction toInteger(value: unknown): number {\n\tconst number = Number(value);\n\tif (isNaN(number)) {\n\t\treturn 0;\n\t}\n\tif (number === 0 || !isFinite(number)) {\n\t\treturn number;\n\t}\n\treturn (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));\n}\nconst maxSafeInteger = Math.pow(2, 53) - 1;\nfunction toLength(value: unknown): number {\n\tconst len = toInteger(value);\n\treturn Math.min(Math.max(len, 0), maxSafeInteger);\n}\n/**\n * Creates an array from an iterable object.\n * @param iterable An iterable object to convert to an array.\n */\nexport default function arrayFrom<T>(iterable: Iterable<T> | ArrayLike<T>): T[];\n/**\n * Creates an array from an iterable object.\n * @param iterable An iterable object to convert to an array.\n * @param mapfn A mapping function to call on every element of the array.\n * @param thisArg Value of 'this' used to invoke the mapfn.\n */\nexport default function arrayFrom<T, U>(\n\tarrayLike: Iterable<T> | ArrayLike<T>,\n\tmapFn?: (v: T, k: number) => U\n): U[] {\n\t// 1. Let C be the this value.\n\t// edit(@eps1lon): we're not calling it as Array.from\n\tconst C = Array;\n\n\t// 2. Let items be ToObject(arrayLike).\n\tconst items = Object(arrayLike);\n\n\t// 3. ReturnIfAbrupt(items).\n\tif (arrayLike == null) {\n\t\tthrow new TypeError(\n\t\t\t\"Array.from requires an array-like object - not null or undefined\"\n\t\t);\n\t}\n\n\t// 4. If mapfn is undefined, then let mapping be false.\n\t// const mapFn = arguments.length > 1 ? arguments[1] : void undefined;\n\n\tif (typeof mapFn !== \"undefined\") {\n\t\t// 5. else\n\t\t// 5. a If IsCallable(mapfn) is false, throw a TypeError exception.\n\t\tif (!isCallable(mapFn)) {\n\t\t\tthrow new TypeError(\n\t\t\t\t\"Array.from: when provided, the second argument must be a function\"\n\t\t\t);\n\t\t}\n\t}\n\n\t// 10. Let lenValue be Get(items, \"length\").\n\t// 11. Let len be ToLength(lenValue).\n\tconst len = toLength(items.length);\n\n\t// 13. If IsConstructor(C) is true, then\n\t// 13. a. Let A be the result of calling the [[Construct]] internal method\n\t// of C with an argument list containing the single item len.\n\t// 14. a. Else, Let A be ArrayCreate(len).\n\tconst A = isCallable(C) ? Object(new C(len)) : new Array(len);\n\n\t// 16. Let k be 0.\n\tlet k = 0;\n\t// 17. Repeat, while k < len… (also steps a - h)\n\tlet kValue;\n\twhile (k < len) {\n\t\tkValue = items[k];\n\t\tif (mapFn) {\n\t\t\tA[k] = mapFn(kValue, k);\n\t\t} else {\n\t\t\tA[k] = kValue;\n\t\t}\n\t\tk += 1;\n\t}\n\t// 18. Let putStatus be Put(A, \"length\", len, true).\n\tA.length = len;\n\t// 20. Return A.\n\treturn A;\n}\n"]},"metadata":{},"sourceType":"module"}