index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. const escapePathForRegex = exports.escapePathForRegex = dir => {
  10. if (_path2.default.sep === '\\') {
  11. // Replace "\" with "/" so it's not escaped by escapeStrForRegex.
  12. // replacePathSepForRegex will convert it back.
  13. dir = dir.replace(/\\/g, '/');
  14. }
  15. return replacePathSepForRegex(escapeStrForRegex(dir));
  16. }; /**
  17. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  18. *
  19. * This source code is licensed under the MIT license found in the
  20. * LICENSE file in the root directory of this source tree.
  21. *
  22. *
  23. */
  24. const escapeStrForRegex = exports.escapeStrForRegex = string => string.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&');
  25. const replacePathSepForRegex = exports.replacePathSepForRegex = string => {
  26. if (_path2.default.sep === '\\') {
  27. return string.replace(/(\/|\\(?!\.))/g, '\\\\');
  28. }
  29. return string;
  30. };