-
-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add escape(), unescape(), and Minimatch.hasMagic()
Also, treat single-character brace classes as their literal character, without magic. So for example, `[f]` would be parsed as just `'f'`, and not treated as a magic pattern.
- Loading branch information
Showing
13 changed files
with
2,328 additions
and
67 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,23 @@ | ||
import { MinimatchOptions } from './index.js' | ||
/** | ||
* Escape all magic characters in a glob pattern. | ||
* | ||
* If the {@link windowsPathsNoEscape | GlobOptions.windowsPathsNoEscape} | ||
* option is used, then characters are escaped by wrapping in `[]`, because | ||
* a magic character wrapped in a character class can only be satisfied by | ||
* that exact character. In this mode, `\` is _not_ escaped, because it is | ||
* not interpreted as a magic character, but instead as a path separator. | ||
*/ | ||
export const escape = ( | ||
s: string, | ||
{ | ||
windowsPathsNoEscape = false, | ||
}: Pick<MinimatchOptions, 'windowsPathsNoEscape'> = {} | ||
) => { | ||
// don't need to escape +@! because we escape the parens | ||
// that make those magic, and escaping ! as [!] isn't valid, | ||
// because [!]] is a valid glob class meaning not ']'. | ||
return windowsPathsNoEscape | ||
? s.replace(/[?*()[\]]/g, '[$&]') | ||
: s.replace(/[?*()[\]\\]/g, '\\$&') | ||
} |
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,25 @@ | ||
import { MinimatchOptions } from './index.js' | ||
/** | ||
* Un-escape a string that has been escaped with {@link escape}. | ||
* | ||
* If the {@link windowsPathsNoEscape} option is used, then square-brace | ||
* escapes are removed, but not backslash escapes. For example, it will turn | ||
* the string `'[*]'` into `*`, but it will not turn `'\\*'` into `'*'`, | ||
* becuase `\` is a path separator in `windowsPathsNoEscape` mode. | ||
* | ||
* When `windowsPathsNoEscape` is not set, then both brace escapes and | ||
* backslash escapes are removed. | ||
* | ||
* Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped | ||
* or unescaped. | ||
*/ | ||
export const unescape = ( | ||
s: string, | ||
{ | ||
windowsPathsNoEscape = false, | ||
}: Pick<MinimatchOptions, 'windowsPathsNoEscape'> = {} | ||
) => { | ||
return windowsPathsNoEscape | ||
? s.replace(/\[([^\/\\])\]/g, '$1') | ||
: s.replace(/((?!\\).|^)\[([^\/])\]/g, '$1$2').replace(/\\([^\/])/g, '$1') | ||
} |
Oops, something went wrong.