From ac36e2f8b80e8e5e74b82c52436ab558abec7cd0 Mon Sep 17 00:00:00 2001 From: Abhishek765 Date: Sun, 2 Oct 2022 01:24:59 +0530 Subject: [PATCH] feat: Added some JS polyfills --- 10-Js-polyfills/Promise.js | 67 +++++++++++++++++++++++++++++++++++ 10-Js-polyfills/PromiseAll.js | 58 ++++++++++++++++++++++++++++++ 10-Js-polyfills/myFilter.js | 19 ++++++++++ 10-Js-polyfills/myMap.js | 17 +++++++++ 10-Js-polyfills/myReduce.js | 18 ++++++++++ 5 files changed, 179 insertions(+) create mode 100644 10-Js-polyfills/Promise.js create mode 100644 10-Js-polyfills/PromiseAll.js create mode 100644 10-Js-polyfills/myFilter.js create mode 100644 10-Js-polyfills/myMap.js create mode 100644 10-Js-polyfills/myReduce.js diff --git a/10-Js-polyfills/Promise.js b/10-Js-polyfills/Promise.js new file mode 100644 index 00000000..33299324 --- /dev/null +++ b/10-Js-polyfills/Promise.js @@ -0,0 +1,67 @@ +function myPromise(executer) { + let onResolve, + onReject, + isFullFilled = false, + isCalled = false, + value; + + this.then = function (callback) { + onResolve = callback; + + if (isFullFilled && !isCalled) { + isCalled = true; + onResolve(value); + } + return this; + }; + + this.catch = function (callback) { + onReject = callback; + if (isFullFilled && !isCalled) { + isCalled = true; + onReject(value); + } + return this; + }; + + function resolve(val) { + isFullFilled = true; + value = val; + if (typeof onResolve === "function") { + isCalled = true; + onResolve(val); + } + } + + function reject(val) { + isFullFilled = true; + value = val; + if (typeof onReject === "function") { + isCalled = true; + onReject(val); + } + } + + // Error handling for executer + try { + executer(resolve, reject); + } catch (error) { + reject(error); + } +} + +const examplePromise = new myPromise((resolve, reject) => { + let state = false; + setTimeout(() => { + if (state) resolve("Promise resolved"); + else reject("Promise rejected"); + }, 1000); +}); + +examplePromise + .then((res) => { + console.log(res); + }) + .catch((err) => { + throw new Error(err); + }); diff --git a/10-Js-polyfills/PromiseAll.js b/10-Js-polyfills/PromiseAll.js new file mode 100644 index 00000000..2f10b118 --- /dev/null +++ b/10-Js-polyfills/PromiseAll.js @@ -0,0 +1,58 @@ +// Resolved promise +function promise1(val) { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(val); + }, 1000); + }); +} + +// Rejected promise +function promise2(val) { + return new Promise((resolve, reject) => { + setTimeout(() => { + reject(val); + }, 1000); + }); +} + +// Resolved promise +function promise3(val) { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(val); + }, 1000); + }); +} + +Promise.myAllPromise = (promises) => { + return new Promise((resolve, reject) => { + let results = []; + + if (promises.length === 0) { + resolve(results); + return; + } + + let promisePendingState = promises.length; + + promises.forEach((promise, index) => { + Promise.resolve(promise).then((res) => { + results[index] = res; + promisePendingState--; + + if (promisePendingState === 0) { + resolve(results); + } + }, reject); + }); + }); +}; + +Promise.myAllPromise([ + promise1("hello from promise 1"), + promise2("hello from promise 2"), + promise3("hello from promise 3"), +]) + .then((res) => console.log(res)) + .catch((err) => console.error(`failed Promise: ${err}`)); diff --git a/10-Js-polyfills/myFilter.js b/10-Js-polyfills/myFilter.js new file mode 100644 index 00000000..08a9b2dc --- /dev/null +++ b/10-Js-polyfills/myFilter.js @@ -0,0 +1,19 @@ +const numbers = [1, 2, 3, 4, 5]; +// structure -> Array.filter((ele, index, arr) => {}) + +Array.prototype.myFilter = function (callback) { + let newArr = []; + + for (let i = 0; i < this.length; i++) { + if (callback(this[i], i, this)) { + newArr.push(this[i]); + } + } + + return newArr; +}; + +// const filteredNumbers = numbers.filter((num) => num > 2); +const filteredNumbers = numbers.myFilter((num) => num > 2); + +console.log({ filteredNumbers }); diff --git a/10-Js-polyfills/myMap.js b/10-Js-polyfills/myMap.js new file mode 100644 index 00000000..7f4baf98 --- /dev/null +++ b/10-Js-polyfills/myMap.js @@ -0,0 +1,17 @@ +const numbers = [1, 2, 3, 4, 5]; +// structure -> Array.map((ele, index, arr) => {}) + +Array.prototype.myMap = function (callback) { + let newArr = []; + + for (let i = 0; i < this.length; i++) { + newArr.push(callback(this[i], i, this)); + } + + return newArr; +}; + +// const modifiedNumbers = numbers.map((num) => num * 2); +const modifiedNumbers = numbers.myMap((num) => num * 2); + +console.log({ modifiedNumbers }); diff --git a/10-Js-polyfills/myReduce.js b/10-Js-polyfills/myReduce.js new file mode 100644 index 00000000..ef83a730 --- /dev/null +++ b/10-Js-polyfills/myReduce.js @@ -0,0 +1,18 @@ +const numbers = [1, 2, 3, 4, 5]; +// structure -> Array.reduce((acc, curr, index, arr) => {}, initialValue) + +Array.prototype.myReduce = function (callback, initialValue) { + let accumulator = initialValue; + + // after assigning initialValue to accumulator we need to check whether accumulator exists or not + for (let i = 0; i < this.length; i++) { + accumulator = accumulator ? accumulator + this[i] : this[i]; + } + + return accumulator; +}; + +// const sum = numbers.reduce((acc, curr) => acc + curr, 0); +const sum = numbers.myReduce((acc, curr) => acc + curr, 0); + +console.log({ sum });