index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.replacePathSepForRegex =
  6. exports.escapeStrForRegex =
  7. exports.escapePathForRegex =
  8. void 0;
  9. var _path = require('path');
  10. /**
  11. * Copyright (c) Meta Platforms, Inc. and affiliates.
  12. *
  13. * This source code is licensed under the MIT license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. *
  16. */
  17. const escapePathForRegex = dir => {
  18. if (_path.sep === '\\') {
  19. // Replace "\" with "/" so it's not escaped by escapeStrForRegex.
  20. // replacePathSepForRegex will convert it back.
  21. dir = dir.replace(/\\/g, '/');
  22. }
  23. return replacePathSepForRegex(escapeStrForRegex(dir));
  24. };
  25. exports.escapePathForRegex = escapePathForRegex;
  26. const escapeStrForRegex = string =>
  27. string.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&');
  28. exports.escapeStrForRegex = escapeStrForRegex;
  29. const replacePathSepForRegex = string => {
  30. if (_path.sep === '\\') {
  31. return string.replace(
  32. /(\/|(.)?\\(?![[\]{}()*+?.^$|\\]))/g,
  33. (_match, _, p2) => (p2 && p2 !== '\\' ? `${p2}\\\\` : '\\\\')
  34. );
  35. }
  36. return string;
  37. };
  38. exports.replacePathSepForRegex = replacePathSepForRegex;