Briefly, it takes two arrays: strings (the string constants ['/', '/', ''] ) and values (the templated string values that need to be encoded ['users', '123'] ). It constructs and returns a single string ( /users/123 ).

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 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.

There are no parentheses behind the call to buildUriPath ? What is this witchcraft? This is called a Tagged Template Literal. It’s a special form of the ES6 template literal that we used above, but it allows us to process it with a function.

Remember that we don’t call our code directly. It’s called by the JavaScript’s template literal function after parsing the template into separate elements.

Fans of styled-components are already familiar with this syntax. It’s a very powerful addition to the ES6 specification.

Reuse

So now that I have a super-duper, handy-dandy buildUriPath function, let’s share it with the world so that everyone can use it. But the thing is... Even if no one else wanted to use this, I still want to be able to use it myself, over-and-over easily. We can do this by making an npm package. I’m rather keen on the name build-uri-path , so I hope that it’s available... and it is!

Because we wrote our utility in ES6, but the world still relies on ES5, we’ll use Babel to transpile it for us.

The complete source for everything described below can by found in my GitHub repo.

GitHub