@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
21 lines (20 loc) • 699 B
JavaScript
import { EMPTY_STRING } from "@lou.codes/constants/empty.js";
import { reduce } from "./reduce.js";
/**
* Takes a `separator` string and a iterable and returns a string with the
* concatenation of all the elements separated by the `separator`.
*
* @category Reducers
* @example
* ```typescript
* const joinWithSpaces = join(" ");
* joinWithSpaces([1, 2, 3]); // "1 2 3"
* ```
* @param separator String to use as separator.
* @returns Curried function with `separator` in context.
*/
export const join = separator => iterable =>
reduce(
item => string =>
`${string ?? EMPTY_STRING}${string === undefined ? EMPTY_STRING : separator}${item}`,
)(undefined)(iterable) ?? EMPTY_STRING;