Reduce, Reuse, React — JavaScript January

  • Briefly, it takes two arrays: (the string constants ) and (the templated string values that need to be encoded ).
  • It is passed an array of strings, but what I’m calling an array of values, is actually a variable number of arguments.
  • I’m using ES6 “rest” syntax to convert the arguments into an array of values.
  • When we reduce over strings, the zeroth element of the strings array is passed as partialUri and iteration starts with the first element, and so on.
  • It’s a special form of the ES6 template literal that we used above, but it allows us to process it with a function.

A while back while writing a React application that hit a REST endpoint, I wrote a little utility to build a properly encoded Uri path. At the time, I didn’t think much about it. It did the job and I moved on.

A beginners guide to turning that little reusable piece of code into its own npm package.

A while back while writing a React application that hit a REST endpoint, I wrote a little utility to build a properly encoded Uri path. At the time, I didn’t think much about it. It did the job and I moved on.

Months later, while working on another project, I needed something similar. I set out to find it and copy the code, but I couldn’t remember where I originally wrote it.

After a good deal of digging around — both on my local file system and on GitHub — I finally located it. I vowed that this would never happen again.

and call it a day?” Maybe something like this.

That would be fine if you could swear on your mother’s grave that the name of the resource or the userId did not contain any characters that needed to be Uri encoded — not only today, but forever.

, which would likely return all users in XML format—not at all what we’re expecting.

method, which takes an array of values and reduces them down to a single value. In our case, we take an array of strings and values, and reduce them to a single string.

OK, that’s not entirely accurate. It is passed an array of strings, but what I’m calling an array of values, is actually a variable number…

Reduce, Reuse, React — JavaScript January