mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-11 21:34:43 -04:00

Former-commit-id: a6850161e2d1ecc9161e027ec9c9beb0899294ff [formerly 77569441219bc3c8a1bef04589f0907b00d5554b] Former-commit-id: ec38432652537c12bd730838af9f327a2e77c0fe
35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
/**
|
|
* Simple Array.from polyfill (with Set support) from https://stackoverflow.com/a/62682524/9727685
|
|
*/
|
|
(function () {
|
|
|
|
function arrayFrom(arr, callbackFn, thisArg) {
|
|
//if you need you can uncomment the following line
|
|
//if(!arr || typeof arr == 'function')throw new Error('This function requires an array-like object - not null, undefined or a function');
|
|
|
|
var arNew = [],
|
|
k = [], // used for convert Set to an Array
|
|
i = 0,
|
|
v;
|
|
|
|
//if you do not need a Set object support then
|
|
//you can comment or delete the following if statement
|
|
if (window.Set && arr instanceof Set) {
|
|
//we use forEach from Set object
|
|
arr.forEach(function (v) {
|
|
k.push(v)
|
|
});
|
|
arr = k;
|
|
}
|
|
|
|
for (; i < (arr.length || arr.size); i++) {
|
|
v = typeof arr[i] !== 'undefined' ? arr[i] : arr.get ? arr.get(i) : null;
|
|
arNew[i] = callbackFn ?
|
|
callbackFn.call(thisArg, v, i, arr) : v;
|
|
}
|
|
|
|
return arNew;
|
|
}
|
|
//You could also use it without the following line, but it is not recommended because native function is faster.
|
|
Array.from = Array.from || arrayFrom; //We set it as polyfill
|
|
}()); |