This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A function that partitions an array based on the passed logic | |
* @param arr The array to be partitioned | |
* @param callback The logic function to base the partition on | |
*/ | |
const partition = (arr, callback) => { | |
// The result object which will ultimately have two properties | |
// positive and negative | |
const result = {}; | |
arr.forEach((item, index) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method for creation of array | |
function array(...arguments) { | |
let arr = Object.create(array.prototype); | |
Object.defineProperty(arr, "length", { | |
value: 0, | |
enumerable: false, | |
writable: true | |
}) | |
for(let i=0;i<arguments.length;i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function curry(func, length = func.length) { | |
// passing a second paramenter as the number of arguments | |
// passed to the function | |
return function (...args) { | |
if (args.length >= length) { | |
// if the function call contains arguments which are | |
// greater than or equal to the max, just the return the func | |
return func(...args); | |
} else { | |
// otherwise call the curry again, binding the already |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to be called on click | |
const mainFunc = () => { | |
console.log("Called me?😃"); | |
} | |
const oncifyHelper = () => { | |
let clickCount = 0; | |
const oncifyHelperInner = (func) => { | |
if(clickCount===0) { | |
func(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function normalFunc(x,y,callback) { | |
const result = x + y; | |
callback(result); | |
} | |
async function run() { | |
const promisedFn = promisify(normalFunc); | |
const result = await promisedFn(4,3).then(data => data*data); | |
console.log(result); | |
} | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = { | |
a: 32, | |
b: 34 | |
} | |
Object.defineProperty(obj, Symbol.iterator, { | |
configurable: false, | |
enumerable: false, | |
writable: false, | |
value: function() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.bind = function(...args) { | |
let obj = this; | |
let params = args.slice(1); | |
return function(...args2) { | |
obj.apply(args[0], [...params, ...args2]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPromise { | |
static all(promises) { | |
let results = []; // to be resolved array of values | |
let completedPromises = 0; | |
// return a promise | |
return new Promise((resolve, reject) => { | |
promises.forEach((promise, index) => { | |
promise.then((value) => { | |
results[index] = value; | |
completedPromises += 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = [ | |
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]]]]]]]]]]] | |
]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function PromisePolyfill(executor) { | |
let onResolve, onReject; | |
let fulfilled = false, | |
rejected = false, | |
called = false, | |
value; | |
function resolve(v) { | |
fulfilled = true; | |
value = v; |
OlderNewer