Skip to content

Commit

Permalink
feat: Update "join" to allow uncurried call
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Aug 26, 2020
1 parent eea3954 commit d836ce4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/join/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
* Join all elements of an array into a string
*
* @tag Array
* @signature ( separator: string )( source: Array ): string
* @signature (separator: String)(source: Array): String
* @signature (separator: String, source: Array): String
*
* @param {string} separator Separator between each adjacent elements
* @param {string} source Source string
* @param {String} separator Separator between each adjacent elements
* @param {[]} source Source array
*
* @return {string}
* @return {String}
*
* @example
* join(",")(["lorem", "ipsum"])
* // => "lorem,ipsum"
*/
const join = separator => source => [].join.call(source, separator)
const join = (separator, ...rest) => {
if (rest.length === 0) {
return source => [].join.call(source, separator)
}

return [].join.call(rest[0], separator)
}

export { join }
12 changes: 11 additions & 1 deletion src/join/join.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import { join } from ".."
test("join", t => {
const source = ["lorem", "ipsum"]

t.equals(join(",")(source), "lorem,ipsum", "Join array with 2 string into 1")
t.equals(
join(",")(source),
"lorem,ipsum",
"Join array with 2 string into 1 (curried)"
)

t.equals(
join(",", source),
"lorem,ipsum",
"Join array with 2 string into 1 (uncurried)"
)

t.end()
})

0 comments on commit d836ce4

Please sign in to comment.