//Function composition combine two function to one
//combine first name and last name and and return full name in camel case
const append=()=>{
const arr=[];
if(arguments.length==1)
{
arguments=arguments[0].split(" ");
}
for(let i=0;i<arguments.length;i++)
{
arr.push(arguments[i]);
}
return arr;
}
const cap=(...letters)=>{
let updateData="";
for(let i=0;i<letters.length;i++)
{
const letter=letters[i];
updateData+=letter.substr(0,1).toUpperCase()+letter.substr(1,letter.length-1)+" ";
}
return updateData;
}
const Pipe=()=>{
const functions=arguments;
return (...args)=>{
for(let i=0;i<functions.length;i++)
{
const Function=functions[i];
args=Function.call(this,...args);
}
return args;
}
}
const myname=Pipe(append,cap);
console.log(myname("manish","chauhan"));
console.log(myname("sachin","singh","negi"));
console.log(myname("Ranjeet","chauhan"));
//but but i can also use it a different manner make first letter captial
console.log(myname("Array has an array of methods. So by converting the object into an array, you have access to all of that. Woohoo"));
working example
https://stackblitz.com/edit/functioncomposition?file=index.js
Comments
Post a Comment