typedash
Version:
modern, type-safe collection of utility functions
22 lines (20 loc) • 789 B
JavaScript
//#region src/functions/join/join.ts
/**
* Same as `Array.prototype.join`, but allows specifying a non-`string` separator.
* @param elements The elements to join.
* @param separator The separator to use.
* @returns An array with the elements joined by the separator.
*/
function join(elements, separator) {
const emptySeparator = Symbol("emptySeparator");
const separatorFunction = typeof separator === "function" ? separator : () => separator;
return elements.flatMap((element, index, array) => [element, index < array.length - 1 ? separatorFunction(index) : emptySeparator]).filter((item) => item !== emptySeparator);
}
//#endregion
Object.defineProperty(exports, 'join', {
enumerable: true,
get: function () {
return join;
}
});
//# sourceMappingURL=join-CRbZSTkA.cjs.map