-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(filesystem): move several path* utility functions to deepkit/core
- Loading branch information
Showing
10 changed files
with
62 additions
and
51 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
/** | ||
* Normalizes the given path. | ||
* Removes duplicate slashes, removes trailing slashes, adds a leading slash. | ||
*/ | ||
export function pathNormalize(path: string): string { | ||
path = path[0] !== '/' ? '/' + path : path; | ||
path = path.length > 1 && path[path.length - 1] === '/' ? path.slice(0, -1) : path; | ||
return path.replace(/\/+/g, '/'); | ||
} | ||
|
||
/** | ||
* Returns the directory (dirname) of the given path. | ||
*/ | ||
export function pathDirectory(path: string): string { | ||
if (path === '/') return '/'; | ||
const lastSlash = path.lastIndexOf('/'); | ||
return lastSlash <= 0 ? '/' : path.slice(0, lastSlash); | ||
} | ||
|
||
/** | ||
* Returns the basename of the given path. | ||
*/ | ||
export function pathBasename(path: string): string { | ||
const lastSlash = path.lastIndexOf('/'); | ||
return lastSlash === -1 ? path : path.slice(lastSlash + 1); | ||
} | ||
|
||
/** | ||
* Returns the extension of the given path. | ||
*/ | ||
export function pathExtension(path: string): string { | ||
const basename = pathBasename(path); | ||
const lastDot = basename.lastIndexOf('.'); | ||
return lastDot === -1 ? '' : basename.slice(lastDot + 1); | ||
} | ||
|
||
export function pathJoin(...paths: string[]): string { | ||
return '/' + paths | ||
.map(v => pathNormalize(v).slice(1)) | ||
.filter(v => !!v) | ||
.join('/'); | ||
} |
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
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
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