-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32c50bb
commit 93ffc83
Showing
2 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,52 @@ | ||
export function route<T extends string>( | ||
path: T, | ||
params?: Record<string, any> | ||
): T { | ||
if (params) { | ||
const segments = path.split(/\/+/).map((segment) => { | ||
if (segment.startsWith(":")) { | ||
const key = segment.replace(":", "").replace("?", ""); | ||
|
||
if (key in params) { | ||
return params[key]; | ||
} | ||
export function route<T extends string>(path: T, params: Record<string, any> = {}, includeOptional?: boolean): T { | ||
// If the path doesn't contain any optional segments (marked with '?') or parameter placeholders (marked with ':'), | ||
// return the path as it is. | ||
if (!path.includes('?') && !path.includes(':')) { | ||
return path; | ||
} | ||
|
||
// If the segment is optional and it doesn't exist in params, return null to omit it from the resulting path | ||
if (segment.endsWith("?")) { | ||
return null; | ||
} | ||
} | ||
// Initialize an empty string to build the real path. | ||
let realPath = ""; | ||
// Start from the end of the path. | ||
let currentIndex = path.length; | ||
// This flag indicates whether the last segment of the path had a parameter. | ||
let lastSegmentHadParam = false; | ||
|
||
return segment; | ||
}); | ||
// Iterate through each segment of the path from the end to the beginning. | ||
while (currentIndex > 0) { | ||
// Find the index of the start of the current segment. | ||
const startSegmentIndex = path.lastIndexOf('/', currentIndex); | ||
// Extract the current segment from the path. | ||
const segment = path.slice(startSegmentIndex, currentIndex + 1); | ||
// Update the current index to the start of the current segment. | ||
currentIndex = startSegmentIndex - 1; | ||
|
||
// Filter out any null/undefined segments and join remaining segments | ||
return segments.filter((value) => value != null).join("/") as T; | ||
// If the segment is a parameter (starts with '/:'). | ||
if (segment.startsWith('/:')) { | ||
// Extract the parameter name, considering optional parameters (ending with '?'). | ||
const paramName = segment.endsWith('?') ? segment.slice(2, -1) : segment.slice(2); | ||
// Get the value of the parameter from the provided params object. | ||
const paramValue = params[paramName]; | ||
// If the parameter value is defined, add it to the real path. | ||
if (paramValue !== undefined) { | ||
lastSegmentHadParam = true; | ||
realPath = `/${paramValue}` + realPath; | ||
} | ||
} | ||
// If the segment is optional (ends with '?'). | ||
else if (segment.endsWith('?')) { | ||
// Include the segment in the real path if the last segment had a parameter or if includeOptional is true. | ||
if (lastSegmentHadParam || includeOptional) { | ||
realPath = segment.slice(0, -1) + realPath; | ||
} | ||
} | ||
// If the segment is neither a parameter nor optional. | ||
else { | ||
lastSegmentHadParam = false; | ||
realPath = segment + realPath; | ||
} | ||
} | ||
|
||
return path; | ||
} | ||
// Return the constructed real path with the same type as the input path. | ||
return realPath as T; | ||
} |