// ============================================================================ // Extraction // ============================================================================ /** * @description Get the basename (filename with extension) from a path. * * @example * ``` * // Expect: 'file.ts' * type Example1 = PathBasename<'src/components/file.ts'> * ``` */ export type PathBasename = S extends `${string}/${infer Rest}` ? PathBasename : S /** * @description Get the directory name from a path. * * @example * ``` * // Expect: 'src/components' * type Example1 = PathDirname<'src/components/file.ts'> * ``` */ export type PathDirname = S extends `${infer Dir}/${infer _Rest}` ? _Rest extends `${string}/${string}` ? `${Dir}/${PathDirname<_Rest>}` : Dir : "." /** * @description Get the file extension from a path. * * @example * ``` * // Expect: '.ts' * type Example1 = PathExtname<'file.ts'> * // Expect: '.spec.ts' * type Example2 = PathExtname<'file.spec.ts'> * ``` */ export type PathExtname = S extends `${infer _Base}.${infer Ext}` ? Ext extends `${string}.${string}` ? `.${Ext}` : `.${Ext}` : "" // ============================================================================ // Manipulation // ============================================================================ /** * @description Join path segments with '/'. * * @example * ``` * // Expect: 'src/components/Button.tsx' * type Example1 = PathJoin<['src', 'components', 'Button.tsx']> * ``` */ export type PathJoin = T extends [ infer First extends string, ...infer Rest extends string[], ] ? Rest extends [] ? First : `${First}/${PathJoin}` : ""