index.js 1.3 KB

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