UNPKG

rc-js-util

Version:

A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.

37 lines (36 loc) 790 B
/** * @public * Joins two strings `start` and `end` with a `separator`. * * @returns The joined path. * * @remarks * See {@link pathJoin}. */ export function pathJoin(start: string, end: string, separator: string = "/"): string { const startEndsWithSeparator = start[start.length - 1] === separator; const endStartsWithSeparator = end[0] === separator; if (startEndsWithSeparator) { if (endStartsWithSeparator) { return start.concat(end.substring(1)); } else { return start.concat(end); } } else { if (endStartsWithSeparator) { return start.concat(end); } else { return `${start}${separator}${end}`; } } }