Esto es lo que tengo hasta ahora:
function isFunction(functionToCheck) { return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]'; } ////////////////// Implementing pipe function //////////////////// const pipe = (value, ...funcs) => { try { const result = funcs.reduce(function (acc, currFunc) { if (isFunction(currFunc) === false) throw new ('Provided argument at position 2 is not a function!'); return currFunc(acc); }, value); return result; } catch (err) { return err.message; } }; /////////////////////////////////////////////////////////////// const replaceUnderscoreWithSpace = (value) => value.replace(/_/g, ' '); const capitalize = (value) => value .split(' ') .map((val) => val.charAt(0).toUpperCase() + val.slice(1)) .join(' '); const appendGreeting = (value) => `Hello, ${value}!`; const error = pipe('john_doe', replaceUnderscoreWithSpace, capitalize, ''); alert(error); // Provided argument at position 2 is not a function! const result = pipe('john_doe', replaceUnderscoreWithSpace, capitalize, appendGreeting); alert(result); // Hello, John Doe!No pude encontrar la solución. Probablemente alguien podría ayudarme. Gracias por adelantado.
Puede usar Array#reduce para usar cada función y pasar el valor como el acumulador, también puede usar Function instanceof para asegurarse de que cada argumento sea una función.
const isFunction = (func) => func instanceof Function; const pipe = (value, ...funcs) => { return funcs.reduce((acc, func, idx) => { if (!isFunction(func)) { throw new Error( `Provided argument at position ${idx} is not a function!` ); } return func(acc); }, value); }; const split = (x) => x.split(""); const reverse = (x) => x.reverse(); const join = (x) => x.join(""); const test = pipe("a man a plan a canal, panama", split, reverse, join); console.log(test);