Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add replaceSeparatorsForGlob utility #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,16 @@ Clear the regex cache.
mm.clearCache();
```

### [.replaceSeparatorsForGlob](index.js#L871)

Replaces backslashes used as path separators with forward slashes. This is useful on Windows when using `path.join` et.al. See [Backslashes](#backslashes).

**Example**

```js
var reaplcedPath = mm.replaceSeparatorsForGlob(path.join(rootDir, 'other/path/**'));
```

## Options

* [basename](#optionsbasename)
Expand Down Expand Up @@ -945,6 +955,8 @@ We made this decision for micromatch for a couple of reasons:
* consistency with bash conventions.
* glob patterns are not filepaths. They are a type of [regular language](https://en.wikipedia.org/wiki/Regular_language) that is converted to a JavaScript regular expression. Thus, when forward slashes are defined in a glob pattern, the resulting regular expression will match windows or POSIX path separators just fine.

You can use `.replaceSeparatorsForGlob` to replace backslashes used as path separators.

**A note about joining paths to globs**

Note that when you pass something like `path.join('foo', '*')` to micromatch, you are creating a filepath and expecting it to still work as a glob pattern. This causes problems on windows, since the `path.sep` is `\\`.
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,19 @@ micromatch.compilers = compilers;
micromatch.parsers = parsers;
micromatch.caches = cache.caches;

/**
* Replace backslashes used as path separator with forward slashes.
* This is useful when doing `path.resolve` etc. in Windows.
*/

micromatch.replaceSeparatorsForGlob = function(path) {
if (typeof path !== 'string') {
throw new TypeError('expected a string: "' + util.inspect(path) + '"');
}

return path.replace(/\\(?![{}()+?.^$])/g, '/');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copied from the linked Jest PR, all credit goes to @aldarund

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to use this regex in the toPosixSlashes utility method in picomatch. However, I think we'll need to remove the ., since that would cause dotfiles and directories like foo\\.git to incorrect. I think we also need * to be added to the regex.

};

/**
* Expose `micromatch`
* @type {Function}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"Tom Byrer (https://github.com/tomByrer)",
"Tyler Akins (http://rumkin.com)",
"(https://github.com/DianeLooney)",
"Tvrqvoise (https://github.com/tvrqvoise)"
"Tvrqvoise (https://github.com/tvrqvoise)",
"Simen Bekkhus (https://github.com/SimenB)"
],
"repository": "micromatch/micromatch",
"bugs": {
Expand Down
20 changes: 20 additions & 0 deletions test/api.replaceSeparatorsForGlob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var assert = require('assert');
var mm = require('..');

describe('.replaceSeparatorsForGlob()', function() {
it('should throw an error when value is not a string', function() {
assert.throws(function() {
mm.replaceSeparatorsForGlob();
});
});

it('should replace backslashes', function() {
assert.equal(mm.replaceSeparatorsForGlob('/some/path\\with\\backslashes'), '/some/path/with/backslashes');
});

it('should not replace backslashes when they are escaped characters', function() {
assert.equal(mm.replaceSeparatorsForGlob('/some/path\\with\\backslashes/and\\(parens\\)'), '/some/path/with/backslashes/and\\(parens\\)');
});
});