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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-24: Ability to avoid autoreplace \\ in patterns even on Windows #25

Merged
merged 1 commit into from Sep 21, 2019
Merged
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
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -36,10 +36,19 @@ globParent('path/foo'); // 'path' (see issue #3 for details)

## API

### `globParent(maybeGlobString)`
### `globParent(maybeGlobString, [options])`

Takes a string and returns the part of the path before the glob begins. Be aware of Escaping rules and Limitations below.

#### options

```js
{
// Disables the automatic conversion of slashes for Windows
flipBackslashes: true
}
```

## Escaping

The following characters have special significance in glob patterns and must be escaped if you want them to be treated as regular path characters:
Expand Down
11 changes: 9 additions & 2 deletions index.js
Expand Up @@ -10,9 +10,16 @@ var enclosure = /[\{\[].*[\/]*.*[\}\]]$/;
var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
var escaped = /\\([\*\?\|\[\]\(\)\{\}])/g;

module.exports = function globParent(str) {
/**
* @param {string} str
* @param {Object} opts
* @param {boolean} [opts.flipBackslashes=true]
*/
module.exports = function globParent(str, opts) {
var options = Object.assign({ flipBackslashes: true }, opts);

// flip windows path separators
if (isWin32 && str.indexOf(slash) < 0) {
if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
str = str.replace(backslash, slash);
}

Expand Down
13 changes: 12 additions & 1 deletion test/index.test.js
Expand Up @@ -84,7 +84,12 @@ describe('glob-parent', function() {
assert.equal(gp('\\{foo,bar}/'), '{foo,bar}');
assert.equal(gp('\\{foo,bar\\}/'), '{foo,bar}');
assert.equal(gp('{foo,bar\\}/'), '.');
if (!isWin32) {

if (isWin32) {
// On Windows we are trying to flip backslashes foo-\\( → foo-/(
assert.equal(gp('foo-\\(bar\\).md'), 'foo-');
} else {
assert.equal(gp('foo-\\(bar\\).md'), '.');
assert.equal(gp('\\[bar]'), '[bar]');
assert.equal(gp('[bar\\]'), '.');
assert.equal(gp('\\{foo,bar\\}'), '{foo,bar}');
Expand Down Expand Up @@ -135,6 +140,12 @@ describe('glob-parent', function() {

done();
});

it('should respect disabled auto flip backslashes', function(done) {
assert.equal(gp('foo-\\(bar\\).md', { flipBackslashes: false }), '.');

done();
});
});

describe('glob2base test patterns', function() {
Expand Down