UNPKG

@lifaon/path

Version:

Useful tool to manage paths like the URL object

21 lines 1.11 kB
import { isRelativePathSegment } from '../is/is-relative-path-segement.js'; import { isRootPathSegment } from '../is/is-root-path-segement.js'; import { getBasenameOfEntryPathSegment } from './get-basename-of-entry-path-segment.js'; export const DEFAULT_SPECIAL_SEGMENTS_ALLOWED_FOR_BASENAME = new Set(); /** * Returns the basename of a segment, without 'ext' if provided * - returns null if segment is special (relative or root) and options.allowedSpecialSegments doesn't include it * INFO: 'basename' must be a valid entry, including relative and root */ export function getBasenameOfPathSegment(segment, ext, { rootRegExp, allowedSpecialSegments = DEFAULT_SPECIAL_SEGMENTS_ALLOWED_FOR_BASENAME, }) { if (isRelativePathSegment(segment)) { return allowedSpecialSegments.has(segment) ? segment : null; } else if (isRootPathSegment(segment, rootRegExp)) { return allowedSpecialSegments.has('root') ? (segment === '' ? '/' : '') : null; } else { return getBasenameOfEntryPathSegment(segment, ext); } } //# sourceMappingURL=get-basename-of-path-segment.js.map