Skip to content

Commit

Permalink
feat: add flipBackslashes option to avoid auto conversion of slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Sep 9, 2019
1 parent d497548 commit a326e3b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
7 changes: 7 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('glob-parent', function() {
assert.equal(gp('[bar]/'), '.');
assert.equal(gp('./\\[bar]'), './[bar]');
assert.equal(gp('\\[bar]/'), '[bar]');
assert.equal(gp('foo-\\(bar\\).md'), 'foo-');
assert.equal(gp('[bar\\]/'), '.');
assert.equal(gp('path/foo \\[bar]/'), 'path/foo [bar]');
assert.equal(gp('path/\\{foo,bar}/'), 'path/{foo,bar}');
Expand Down Expand Up @@ -135,6 +136,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

0 comments on commit a326e3b

Please sign in to comment.