Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Snippets/Polyfill/bind-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// bind polyfill

Function.prototype.customBind = function (...args) {
let context = this;
let params = args.slice(1);

return function (...args2) {
context.apply(args[0], [...params, ...args2]);
};
};
12 changes: 12 additions & 0 deletions Snippets/Polyfill/filter-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// filter polyfill

Array.prototype.customFilter = function (callbackFn) {
const result = [];

for (let i = 0; i < this.length; i++) {
if (callbackFn.call(this, this[i], i)) {
result.push(this[i]);
}
}
return result;
};
7 changes: 7 additions & 0 deletions Snippets/Polyfill/forEach-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// forEach polyfill

Array.prototype.customForEach = function (callbackFn) {
for (let i = 0; i < this.length; i++) {
callbackFn.call(this, this[i], i);
}
};
10 changes: 10 additions & 0 deletions Snippets/Polyfill/map-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// map polyfill

Array.prototype.customMap = function (callbackFn) {
const result = [];

for (let i = 0; i < this.length; i++) {
result.push(callbackFn.call(this, this[i], i));
}
return result;
};
21 changes: 21 additions & 0 deletions Snippets/Polyfill/promiseAll-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// promise polyfill

Promise.customAll = function (promiseArray) {
return new Promise((resolve, reject) => {
let completed = 0;
const promiseResults = [];

promiseArray.forEach((value, index) => {
Promise.resolve(value)
.then((result) => {
completed++;
promiseResults[index] = result;

if (completed === promiseArray.length) {
resolve(promiseResults);
}
})
.catch((err) => reject(err));
});
});
};
14 changes: 14 additions & 0 deletions Snippets/Polyfill/reduce-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// reduce polyfill

Array.prototype.customReduce = function (callbackFn, initialValue) {
let accumulator = initialValue;

for (let i = 0; i < this.length; i++) {
if (accumulator !== undefined) {
accumulator = callbackFn.call(undefined, accumulator, this[i], i, this);
} else {
accumulator = this[i];
}
}
return accumulator;
};