escape.js 1.1 KB

1234567891011121314151617181920212223242526
  1. /**
  2. * Escape all magic characters in a glob pattern.
  3. *
  4. * If the {@link MinimatchOptions.windowsPathsNoEscape}
  5. * option is used, then characters are escaped by wrapping in `[]`, because
  6. * a magic character wrapped in a character class can only be satisfied by
  7. * that exact character. In this mode, `\` is _not_ escaped, because it is
  8. * not interpreted as a magic character, but instead as a path separator.
  9. *
  10. * If the {@link MinimatchOptions.magicalBraces} option is used,
  11. * then braces (`{` and `}`) will be escaped.
  12. */
  13. export const escape = (s, { windowsPathsNoEscape = false, magicalBraces = false, } = {}) => {
  14. // don't need to escape +@! because we escape the parens
  15. // that make those magic, and escaping ! as [!] isn't valid,
  16. // because [!]] is a valid glob class meaning not ']'.
  17. if (magicalBraces) {
  18. return windowsPathsNoEscape ?
  19. s.replace(/[?*()[\]{}]/g, '[$&]')
  20. : s.replace(/[?*()[\]\\{}]/g, '\\$&');
  21. }
  22. return windowsPathsNoEscape ?
  23. s.replace(/[?*()[\]]/g, '[$&]')
  24. : s.replace(/[?*()[\]\\]/g, '\\$&');
  25. };
  26. //# sourceMappingURL=escape.js.map