From de01bf878ba8f3066e0098a77d7640e1b86c3ede Mon Sep 17 00:00:00 2001 From: vaishnavme <42497931+vaishnavme@users.noreply.github.com> Date: Sun, 19 Dec 2021 14:21:29 +0530 Subject: [PATCH] Polyfill for forEach, map, filter, reduce, bind and promise.all --- Snippets/Polyfill/bind-polyfill.js | 10 ++++++++++ Snippets/Polyfill/filter-polyfill.js | 12 ++++++++++++ Snippets/Polyfill/forEach-polyfill.js | 7 +++++++ Snippets/Polyfill/map-polyfill.js | 10 ++++++++++ Snippets/Polyfill/promiseAll-polyfill.js | 21 +++++++++++++++++++++ Snippets/Polyfill/reduce-polyfill.js | 14 ++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 Snippets/Polyfill/bind-polyfill.js create mode 100644 Snippets/Polyfill/filter-polyfill.js create mode 100644 Snippets/Polyfill/forEach-polyfill.js create mode 100644 Snippets/Polyfill/map-polyfill.js create mode 100644 Snippets/Polyfill/promiseAll-polyfill.js create mode 100644 Snippets/Polyfill/reduce-polyfill.js diff --git a/Snippets/Polyfill/bind-polyfill.js b/Snippets/Polyfill/bind-polyfill.js new file mode 100644 index 0000000..c3d6e7b --- /dev/null +++ b/Snippets/Polyfill/bind-polyfill.js @@ -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]); + }; +}; diff --git a/Snippets/Polyfill/filter-polyfill.js b/Snippets/Polyfill/filter-polyfill.js new file mode 100644 index 0000000..80fd40c --- /dev/null +++ b/Snippets/Polyfill/filter-polyfill.js @@ -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; +}; diff --git a/Snippets/Polyfill/forEach-polyfill.js b/Snippets/Polyfill/forEach-polyfill.js new file mode 100644 index 0000000..faeaa84 --- /dev/null +++ b/Snippets/Polyfill/forEach-polyfill.js @@ -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); + } +}; diff --git a/Snippets/Polyfill/map-polyfill.js b/Snippets/Polyfill/map-polyfill.js new file mode 100644 index 0000000..27326cf --- /dev/null +++ b/Snippets/Polyfill/map-polyfill.js @@ -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; +}; diff --git a/Snippets/Polyfill/promiseAll-polyfill.js b/Snippets/Polyfill/promiseAll-polyfill.js new file mode 100644 index 0000000..9a53798 --- /dev/null +++ b/Snippets/Polyfill/promiseAll-polyfill.js @@ -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)); + }); + }); +}; diff --git a/Snippets/Polyfill/reduce-polyfill.js b/Snippets/Polyfill/reduce-polyfill.js new file mode 100644 index 0000000..9a31b2e --- /dev/null +++ b/Snippets/Polyfill/reduce-polyfill.js @@ -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; +};