/** * @file Route Segment Parsing Utilities * @description Framework-agnostic utilities for parsing route segments from file paths * and directory structures. Used for file-system-based routing conventions. * * @module @/lib/routing/core/segment-parser * * @example * ```typescript * import { * parseRouteSegment, * parseDirectoryPath, * segmentsToUrlPath, * } from '@/lib/routing/core/segment-parser'; * * // Parse a filename into a route segment * parseRouteSegment('[id].tsx'); // { type: 'dynamic', name: ':id', paramName: 'id', ... } * * // Parse a directory path into segments * parseDirectoryPath('users/[id]', 'posts.tsx'); * // [{ type: 'static', name: 'users' }, { type: 'dynamic', name: ':id' }, { type: 'static', name: 'posts' }] * * // Convert segments to URL path * segmentsToUrlPath(segments); // '/users/:id/posts' * ``` */ /** * Route segment types for file-system convention * * - static: `about.tsx` -> `/about` * - dynamic: `[id].tsx` -> `/:id` * - catchAll: `[[...slug]].tsx` -> `/*` * - optional: `[[id]].tsx` -> `/:id?` * - group: `(auth)/login.tsx` -> `/login` (group ignored in path) * - layout: `_layout.tsx` -> layout wrapper * - index: `index.tsx` -> `/` */ export type RouteSegmentType = 'static' | 'dynamic' | 'catchAll' | 'optional' | 'group' | 'layout' | 'index'; /** * Parsed route segment from filename */ export interface ParsedRouteSegment { /** Type of segment */ readonly type: RouteSegmentType; /** Segment name for URL path */ readonly name: string; /** Parameter name for dynamic segments */ readonly paramName?: string; /** Whether this segment is optional */ readonly isOptional: boolean; /** Original filename before parsing */ readonly originalFilename: string; } /** * Configuration for segment parsing */ export interface SegmentParserConfig { /** Extensions to strip from filenames (default: ['.tsx', '.ts', '.jsx', '.js']) */ extensions?: readonly string[]; /** Prefix for layout files (default: '_') */ layoutPrefix?: string; /** Name for index files (default: 'index') */ indexName?: string; /** Dynamic segment brackets (default: ['[', ']']) */ dynamicBrackets?: readonly [string, string]; /** Optional segment brackets (default: ['[[', ']]']) */ optionalBrackets?: readonly [string, string]; /** Catch-all prefix (default: '...') */ catchAllPrefix?: string; /** Group segment brackets (default: ['(', ')']) */ groupBrackets?: readonly [string, string]; } /** * Default segment parser configuration */ export declare const DEFAULT_SEGMENT_PARSER_CONFIG: Required; /** * Parse a filename or directory name into a route segment * * Conventions: * - `_layout.tsx` -> layout wrapper * - `index.tsx` -> index route (/) * - `[id].tsx` -> dynamic segment (/:id) * - `[[id]].tsx` -> optional param (/:id?) * - `[[...slug]].tsx` -> catch-all (*) * - `(group)/` -> route group (ignored in URL) * - `about.tsx` -> static segment (/about) * * @param filename - The filename or directory name to parse * @param config - Optional parsing configuration * @returns Parsed route segment */ export declare function parseRouteSegment(filename: string, config?: SegmentParserConfig): ParsedRouteSegment; /** * Parse a full directory path into route segments * * @param dirPath - Directory path (relative to routes root) * @param filename - The filename within the directory * @param config - Optional parsing configuration * @returns Array of parsed route segments * * @example * ```typescript * parseDirectoryPath('users/[id]', 'posts.tsx'); * // Returns segments for: /users/:id/posts * ``` */ export declare function parseDirectoryPath(dirPath: string, filename: string, config?: SegmentParserConfig): readonly ParsedRouteSegment[]; /** * Convert parsed segments to URL path * * @param segments - Array of parsed route segments * @returns URL path string * * @example * ```typescript * const segments = parseDirectoryPath('users/[id]', 'posts.tsx'); * segmentsToUrlPath(segments); // '/users/:id/posts' * ``` */ export declare function segmentsToUrlPath(segments: readonly ParsedRouteSegment[]): string; /** * Check if a segment is dynamic (has a parameter) * * @param segment - The parsed segment to check * @returns True if the segment is dynamic */ export declare function isDynamicSegment(segment: ParsedRouteSegment): boolean; /** * Check if a segment contributes to the URL path * * @param segment - The parsed segment to check * @returns True if the segment appears in the URL */ export declare function isUrlSegment(segment: ParsedRouteSegment): boolean; /** * Extract all parameter names from segments * * @param segments - Array of parsed route segments * @returns Array of parameter names */ export declare function extractSegmentParams(segments: readonly ParsedRouteSegment[]): string[]; /** * Generate a route ID from segments * * @param segments - Array of parsed route segments * @param urlPath - Optional URL path (will be calculated if not provided) * @returns Route ID string * * @example * ```typescript * generateRouteId(segments, '/users/:id'); // 'USERS_BY_ID' * ``` */ export declare function generateRouteId(segments: readonly ParsedRouteSegment[], urlPath?: string): string; /** * Generate a human-readable display name from segments * * @param segments - Array of parsed route segments * @returns Display name string * * @example * ```typescript * generateDisplayName(segments); // 'User Detail' * ``` */ export declare function generateDisplayName(segments: readonly ParsedRouteSegment[]): string; /** * Calculate the depth of segments (excluding groups) * * @param segments - Array of parsed route segments * @returns Depth count */ export declare function calculateSegmentDepth(segments: readonly ParsedRouteSegment[]): number; /** * Check if segments contain a layout * * @param segments - Array of parsed route segments * @returns True if any segment is a layout */ export declare function hasLayoutSegment(segments: readonly ParsedRouteSegment[]): boolean; /** * Check if segments represent an index route * * @param segments - Array of parsed route segments * @returns True if the last segment is an index */ export declare function isIndexRoute(segments: readonly ParsedRouteSegment[]): boolean;