Skip to content

Commit

Permalink
New: Add flipBackslashes option to disable auto conversion of slash…
Browse files Browse the repository at this point in the history
…es (closes #24) (#25)
  • Loading branch information
mrmlnc authored and phated committed Sep 21, 2019
1 parent d497548 commit 92974fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 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
13 changes: 12 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
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

0 comments on commit 92974fe

Please sign in to comment.