Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
How to implement pipe function with arg AND ...funcs?
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 pipe = (value, ...funcs) => value => funcs.reduce((value, f) => f(value), value)

I`m trying to implement pipe function, but I can`t get how to make it work so it takes param "value" as argument.

Example

const result = pipe(
  "john_doe",
  replaceUnderscoreWithSpace,
  capitalize,
  appendGreeting,
);
 
alert(result); // Hello, John Doe!
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have an extra nested function in your pipe() implementation. So pipe() returns a function that calls reduce() rather than calling reduce() itself.

Get rid of 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 pipe = (value, ...funcs) => funcs.reduce((value, f) => f(value), value);

const result = pipe(
  "john_doe",
  replaceUnderscoreWithSpace,
  capitalize,
  appendGreeting,
);
 
console.log(result); // Hello, John Doe!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!