UNPKG

@lou.codes/iterables

Version:
21 lines (20 loc) 699 B
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;