-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyfills.js
More file actions
190 lines (172 loc) · 5.43 KB
/
polyfills.js
File metadata and controls
190 lines (172 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
Function.prototype.myBind = function (obj = {}, ...args1) {
obj.function = this;
return function (...args2) {
obj.function(...args1, ...args2);
};
};
Function.prototype.myCall = function (obj = {}, ...args) {
obj.fn = this;
obj.fn(...args);
};
Function.prototype.myApply = function (obj = {}, args) {
obj.fn = this;
//args is array
obj.fn(...args);
};
Array.prototype.myMap = function (fn, thisArg) {
if (typeof fn !== "function") throw new TypeError(fn, "is not a function");
//return an array
const result = [];
for (let i = 0; i < this.length; i++) {
result.push(fn.call(thisArg, this[i], i));
}
//perform the function on each value from array
return result;
};
Array.prototype.myFilter = function (fn, thisArg) {
if (typeof fn !== "function") throw new Error("value is not a function");
//return an array
const result = [];
for (let i = 0; i < this.length; i++) {
if (fn.call(thisArg, this[i], i)) result.push(this[i]);
}
//perform the function on each value from array
return result;
};
Array.prototype.myReduce = function (fn, thisArg) {};
function once(func, context) {
let result,
hasRun = false;
return function (...args) {
if (!hasRun) {
result = func.apply(context || this, args);
hasRun = true;
return result;
}
};
}
function memoize(fn, thisArg) {
const cache = new Map(); // Or use: const cache = {}; for a plain object
return function (...args) {
const argsString = args.join("-"); // Works for both Map and object
if (cache.has(argsString)) {
// Or: if (argsString in cache) for a plain object
return cache.get(argsString); // Or: cache[argsString]
} else {
const result = fn.call(thisArg || this, ...args);
cache.set(argsString, result); // Or: cache[argsString] = result
return result;
}
};
}
Function.prototype.myDebounce = function (fn, thisArg) {};
const throttle = (fn, delay) => {
let last = 0;
return (...args) => {
const now = Date.now();
if (now - last > delay) {
fn.apply(this, args);
last = now;
}
};
};
//Promise polyfill
//Promise.all polyfill
//fail fast, if any fails return error
//otherwise return all results
Promise.customAll = function (promises) {
if (!promises) throw new TypeError("arg is not an iterable");
if (promises.constructor !== Array)
throw new TypeError("not an array of promises");
return new Promise((resolve, reject) => {
const results = new Array(promises.length);
let count = 0;
promises.forEach((promise, index) => {
Promise.resolve(promise)
.then((res) => {
results[index] = res;
count++;
if (count === results.length) resolve(results);
})
.catch((e) => reject(e));
});
// If the array is empty, resolve immediately
if (promises.length === 0) {
resolve(results);
}
});
};
//Promise.any
//returns first resolved promise, of if all fails returns all rejected promises
Promise.customAny = function (promises) {
if (!promises) throw new TypeError("not valid args");
if (promises.constructor !== Array) throw new TypeError("not valid array");
let settled = 0,
errorArray = new Array(promises.length);
return new Promise((outerResolve, outerReject) => {
promises.forEach((promise, index) => {
if (!promise.then) {
//not a promise, resolve immediately
outerResolve(promise);
}
Promise.resolve(promise)
.then((res) => outerResolve(res)) //resolve as soon any any promise is success
.catch((e) => {
settled++;
errorArray[index] = e;
if (settled === promises.length)
//all failed
outerReject(errorArray);
});
});
});
};
//Promise.allSettled polyfill
//return response to all promises , where resolved or not
Promise.myAllSettled = function (promises) {
if (!promises) throw new TypeError("not valid args");
if (promises.constructor !== Array) throw new TypeError("not valid array");
const responses = new Array(promises.length);
let settle = 0;
return new Promise((outerResolve, outerReject) => {
promises.forEach((promise, index) => {
Promise.resolve(promise)
.then((res) => {
responses[index] = { status: "fulfilled", value: res };
settle++;
if (settle === promises.length) outerResolve(responses);
})
.catch((e) => {
responses[index] = { status: "rejected", reason: e };
settle++;
if (settle === promises.length) outerResolve(responses);
});
});
});
};
//Promise.race polyfill
//return first fulfilled promise either rejected or resolved
Promise.myRace = function (promises) {
if (!promises) throw new TypeError("not valid args");
if (promises.constructor !== Array) throw new TypeError("not valid array");
return new Promise((outerResolve, outerReject) => {
promises.forEach((promise, index) => {
Promise.resolve(promise)
.then((res) => outerResolve(res)) //resolve as soon any any promise is success or failed
.catch((e) => outerReject(e));
});
});
};
function flat(arr, depth = 1) {
const result = [];
//iterate over each element, if not array append
//else if this is array, check current depth. and then do it.
arr.forEach((value) => {
if (Array.isArray(value) && depth >= 1) {
result.push(...flat(value, depth - 1));
} else {
result.push(value); // 1 , 2 , etc
}
});
return result;
}