-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
escapePath
utility function (#36)
Co-authored-by: Ben McCann <[email protected]> --------- Co-authored-by: Superchupu <[email protected]>
- Loading branch information
1 parent
0fcb1b3
commit cace1a1
Showing
4 changed files
with
57 additions
and
1 deletion.
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,18 @@ | ||
// #region escapePath | ||
|
||
/* | ||
Matches the following unescaped symbols: | ||
`(){}[]`, `!+@` before `(`, `!` at the beginning, | ||
plus the following platform-specific symbols: | ||
Posix: `*?|`, `\` before non-special characters. | ||
*/ | ||
const POSIX_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}*?|]|^!|[!+@](?=\()|\\(?![()[\]{}!*+?@|]))/g; | ||
const WIN32_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}]|^!|[!+@](?=\())/g; | ||
|
||
export const escapePosixPath = (pattern: string): string => pattern.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, '\\$&'); | ||
export const escapeWin32Path = (pattern: string): string => pattern.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, '\\$&'); | ||
|
||
export const escapePath: (pattern: string) => string = process.platform === 'win32' ? escapeWin32Path : escapePosixPath; | ||
|
||
// #endregion |
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,36 @@ | ||
import assert from 'node:assert/strict'; | ||
import { describe, test } from 'node:test'; | ||
import { escapePosixPath, escapeWin32Path } from '../src/utils.ts'; | ||
|
||
for (const platform of ['win32', 'posix']) { | ||
const escapePath = platform === 'posix' ? escapePosixPath : escapeWin32Path; | ||
|
||
describe(`escapePath (${platform})`, () => { | ||
test("doesn't add backslashes to already escaped patterns", () => { | ||
assert.strictEqual(escapeWin32Path('\\['), '\\['); | ||
|
||
if (platform === 'posix') { | ||
assert.strictEqual(escapePath('\\|'), '\\|'); | ||
} | ||
}); | ||
|
||
test("doesn't add wrong backslashes", () => { | ||
assert.strictEqual(escapePath('hi!@+'), 'hi!@+'); | ||
}); | ||
|
||
test('correctly escapes characters', () => { | ||
assert.strictEqual(escapePath('!nox'), '\\!nox'); | ||
assert.strictEqual(escapePath('hi+(@(!())))'), 'hi\\+\\(\\@\\(\\!\\(\\)\\)\\)\\)'); | ||
|
||
if (platform === 'posix') { | ||
assert.strictEqual(escapePath('\\'), '\\\\'); | ||
assert.strictEqual(escapePath('meo*w'), 'meo\\*w'); | ||
} else if (platform === 'win32') { | ||
assert.strictEqual( | ||
escapePath('C:\\Users\\meeee\\New Folder (1)\\**'), | ||
'C:\\Users\\meeee\\New Folder \\(1\\)\\**' | ||
); | ||
} | ||
}); | ||
}); | ||
} |
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