UNPKG

@lou.codes/iterables

Version:
25 lines (24 loc) 838 B
import { EMPTY_STRING } from "@lou.codes/constants/empty.js"; import { awaitableHandler } from "@lou.codes/utils"; import { reduce } from "./reduce.js"; /** * Takes a `separator` string and a iterable or asynchronous iterable and * returns a string with the concatenation of all the elements separated by the * `separator`. * * @category Asynchronous 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 => awaitableHandler(string => string ?? EMPTY_STRING)( reduce( item => string => `${string ?? EMPTY_STRING}${string === undefined ? EMPTY_STRING : separator}${item}`, )(undefined)(iterable), );