index.js 1.2 KB

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