@lifaon/path
Version:
Useful tool to manage paths like the URL object
28 lines • 963 B
JavaScript
import { isRootPathSegment } from '../../segment/functions/is/is-root-path-segement.js';
/**
* Converts a relative path to an absolute path, by appending `rootSegment` at the beginning of the path.
*/
export function forcePathSegmentsAsAbsolute(segments, rootSegment, { rootRegExp }) {
if (isRootPathSegment(rootSegment, rootRegExp)) {
const firstSegment = segments[0];
if (firstSegment === '.') {
segments[0] = rootSegment;
}
else if (firstSegment === '..') {
while (segments[0] === '..') {
segments.shift();
}
segments.unshift(rootSegment);
}
else if (isRootPathSegment(firstSegment, rootRegExp)) {
// do nothing
}
else {
throw new Error('Path is not normalized');
}
}
else {
throw new Error('Root is invalid');
}
}
//# sourceMappingURL=force-path-segments-as-absolute.js.map