@lifaon/path
Version:
Useful tool to manage paths like the URL object
32 lines • 1.16 kB
JavaScript
import { isRootPathSegment } from '../../segment/functions/is/is-root-path-segement.js';
import { pushUncheckedSegmentIntoMutablePathSegments, } from '../mutate/push-unchecked-segment-into-mutable-path-segments.js';
/**
* Ensures every segment from `segments` is correct and optimizes the result.
* See`IPathSegments` for more details.
*/
export function convertUncheckedPathSegmentsIntoPathSegments(segments, options) {
const length = segments.length;
if (length === 0) {
return ['.'];
}
else {
const normalized = [];
const firstSegment = segments[0];
let i = 0;
if (isRootPathSegment(firstSegment, options.rootRegExp)) {
normalized.push(firstSegment);
i++;
}
else {
normalized.push('.');
}
for (; i < length; i++) {
if (segments[i] !== '') {
// removes ending / or //
pushUncheckedSegmentIntoMutablePathSegments(normalized, segments[i], options);
}
}
return normalized;
}
}
//# sourceMappingURL=convert-unchecked-path-segments-into-path-segments.js.map