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
//takes function to be debounced and delay time to fire. | |
/*returns passed function wrapped with settimeout to delay fire | |
and updates to settimeout whenever called. */ | |
let debounce=(fn,timedelay)=>{ | |
let timerid=null; | |
return function(){ | |
clearTimeout(timerid); | |
timerid=setTimeout(fn.bind(this,...arguments),timedelay); | |
} | |
}; |
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
//takes the function to be throttled and time delay in between subsequent fires as parameters. | |
//returns the passed function wrapped with settimeout and pass check. Takes latest arguments in subsequent calls. | |
let throttle=(fn,timedelay)=>{ | |
let ref={pass:true,arguments:[]}; | |
return function(){ | |
ref.arguments=arguments; | |
if(ref.pass){ | |
setTimeout(()=>{ | |
fn.apply(this,ref.arguments); | |
ref.pass=true; |
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 createState=(initialvalue)=>{ | |
let state={...initialvalue}; | |
let getstate=()=>({...state}); | |
let setstate=(input)=>{ | |
if(input.constructor===Object)state={...state,...input}; |
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
//map function. | |
var map=(array,operator)=>{ | |
let maparr=[]; | |
for(let i=0;i<array.length;i++){ | |
maparr.push(operator(array[i],i,array)) | |
} | |
return maparr; | |
} |
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
// Compose Function | |
var compose=(...args)=>(input)=>args.reduceRight((acc,fn)=>fn(acc),input); | |
var resulant=compose(h,g,f); //(input)=>h(g(f(input))); | |
// Pipe Function | |
var pipe=(...args)=>(input)=>args.reduce((acc,fn)=>fn(acc),input); | |
var resulant=pipe(f,g,h); //(input)=>h(g(f(input))); |
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
var url="www.go.myurlexample.com/007w3?id1=123&id2=abc"; | |
var getLastString=(param)=>(string)=>string.split(param).slice(-1).join(); | |
var split=(param)=>(string)=>string.split(param); | |
var getParamObj=(arr)=>arr.reduce((acc,str)=>{ | |
var arr=split("=")(str); | |
acc[arr[0]]=arr[1]; | |
return acc;} |
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
//Curried Function taking two parameters. | |
var repeatString=(string)=>(count)=>Array(count).fill(string).join(""); | |
//Creating custom functions with currying. | |
var repeatStar=repeatString("*"); // (count)=>Array(count).fill("*").join(""); | |
var repeatSpace=repeatString(" "); // (count)=>Array(count).fill(" ").join(""); | |
for(let i=0;i<8;i++){ | |
console.log(repeatSpace(8-i)+repeatStar(2*i+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
//Global Scope. | |
var globaldata={user:"xyz",loggedin:false,currentstate:"none"} | |
var userinput="Current State Info"; | |
/* Impure way of handling login. Changing Global State, | |
operation is dependent upon global variable 'userinput', | |
no return value. | |
*/ | |
var login=(globaldata)=>{ | |
globaldata.loggedin=true; |
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
var regex=/\d/y; | |
regex.lastIndex=3; | |
regex.exec("0123")[0]; // returns '3' instead of '0' as the regex starts the search from the index 3 of string. |
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
//Using two flags 'g' and 'm' together on a Regex. | |
var regex1= /^[0-9]{9,10}/gm; // /^[0-9]{9,10}/gm | |
var regex2= new RegExp("^[0-9]{9,10}","gm") // /^[0-9]{9,10}/gm |