Skip to content

Commit

Permalink
feat(strings): add join() HOF
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 16, 2020
1 parent f8c626c commit 1c5c46f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/strings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./format";
export * from "./groups";
export * from "./hollerith";
export * from "./interpolate";
export * from "./join";
export * from "./pad-left";
export * from "./pad-right";
export * from "./parse";
Expand Down
26 changes: 26 additions & 0 deletions packages/strings/src/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { memoize1 } from "@thi.ng/memoize";
import type { Stringer } from "./api";

/**
* Higher-order version of `Array.join()`. Takes separator string `sep`
* and returns function which accepts an array and joins all elements w/
* separator into a result string.
*
* @example
* ```ts
* const slashes = join("/");
*
* slashes([1, 2, 3]);
* // "1/2/3"
*
* const formatOBJFace = partial(
* format, ["f ", slashes, " ", slashes, " ", slashes]
* );
*
* formatOBJFace([1, 2], [3, 4], [5, 6]);
* // "f 1/2 3/4 5/6"
* ```
*/
export const join = memoize1<string, Stringer<any[]>>((sep) => (x) =>
x.join(sep)
);

0 comments on commit 1c5c46f

Please sign in to comment.