-
-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reimplement "normalize-path"
- Loading branch information
Showing
18 changed files
with
180 additions
and
73 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
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,50 @@ | ||
/** | ||
* Reimplementation of "normalize-path" | ||
* @see https://github.com/jonschlinkert/normalize-path/blob/52c3a95ebebc2d98c1ad7606cbafa7e658656899/index.js | ||
*/ | ||
|
||
/*! | ||
* normalize-path <https://github.com/jonschlinkert/normalize-path> | ||
* | ||
* Copyright (c) 2014-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
|
||
import path from 'node:path' | ||
|
||
/** | ||
* A file starting with \\?\ | ||
* @see https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#win32-file-namespaces | ||
*/ | ||
const WIN32_FILE_NS = '\\\\?\\' | ||
|
||
/** | ||
* A file starting with \\.\ | ||
* @see https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#win32-file-namespaces | ||
*/ | ||
const WIN32_DEVICE_NS = '\\\\.\\' | ||
|
||
/** | ||
* Normalize input file path to use POSIX separators | ||
* @param {String} input | ||
* @returns String | ||
*/ | ||
export const normalizePath = (input) => { | ||
if (input === path.posix.sep || input === path.win32.sep) { | ||
return path.posix.sep | ||
} | ||
|
||
let normalized = input.split(/[/\\]+/).join(path.posix.sep) | ||
|
||
/** Handle win32 Namespaced paths by changing e.g. \\.\ to //./ */ | ||
if (input.startsWith(WIN32_FILE_NS) || input.startsWith(WIN32_DEVICE_NS)) { | ||
normalized = normalized.replace(/^\/(\.|\?)/, '//$1') | ||
} | ||
|
||
/** Remove trailing slash */ | ||
if (normalized.endsWith(path.posix.sep)) { | ||
normalized = normalized.slice(0, -1) | ||
} | ||
|
||
return normalized | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.