OlesiaBOM,
function isFunction(functionToCheck) {
return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
}
const pipe = (value, ...funcs) => {
let [fun, ...f] = funcs, isFun = isFunction(fun);
value = isFun ? fun(value) : 'error';
return isFun && f.length ? pipe(value, ...f) : value
};
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!