-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved path handling for imports (WIP)
Slightly improved path handling for the import builtin, non-absolute string handling will be fixed in the future.
- Loading branch information
arduano@localhost
committed
May 9, 2024
1 parent
c4978f9
commit dcf5875
Showing
3 changed files
with
45 additions
and
32 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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function isAbsolutePath(path: string): boolean { | ||
return path.startsWith("/"); | ||
} | ||
|
||
export function joinPaths(abs_base: string, path: string): string { | ||
return `${abs_base}/${path}`; | ||
} | ||
|
||
export function normalizePath(path: string): string { | ||
let segments = path.split("/"); | ||
let normalizedSegments: string[] = []; | ||
for (const segment of segments) { | ||
switch (segment) { | ||
case "": | ||
break; | ||
case ".": | ||
break; | ||
case "..": | ||
normalizedSegments.pop(); | ||
break; | ||
default: | ||
normalizedSegments.push(segment); | ||
break; | ||
} | ||
} | ||
return (isAbsolutePath(path) ? "/" : "") + normalizedSegments.join("/"); | ||
} | ||
|
||
export function dirOf(path: string) { | ||
// Return everything before the final slash | ||
const lastSlash = path.lastIndexOf("/"); | ||
return path.substring(0, lastSlash); | ||
} |