index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.replacePathSepForRegex = exports.escapeStrForRegex = exports.escapePathForRegex = void 0;
  6. var _path = _interopRequireDefault(require('path'));
  7. function _interopRequireDefault(obj) {
  8. return obj && obj.__esModule ? obj : {default: obj};
  9. }
  10. /**
  11. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  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.default.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.default.sep === '\\') {
  31. return string.replace(
  32. /(\/|(.)?\\(?![[\]{}()*+?.^$|\\]))/g,
  33. (_match, _, p2) => (p2 && p2 !== '\\' ? p2 + '\\\\' : '\\\\')
  34. );
  35. }
  36. return string;
  37. };
  38. exports.replacePathSepForRegex = replacePathSepForRegex;