Skip to content

Commit eecf91d

Browse files
mrmlncphated
authored andcommitted
feat: add flipBackslashes option to disable auto conversion of slashes (closes #24) (#25)
1 parent cf01fb2 commit eecf91d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,19 @@ globParent('path/foo'); // 'path' (see issue #3 for details)
3636

3737
## API
3838

39-
### `globParent(maybeGlobString)`
39+
### `globParent(maybeGlobString, [options])`
4040

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

43+
#### options
44+
45+
```js
46+
{
47+
// Disables the automatic conversion of slashes for Windows
48+
flipBackslashes: true
49+
}
50+
```
51+
4352
## Escaping
4453

4554
The following characters have special significance in glob patterns and must be escaped if you want them to be treated as regular path characters:

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ var enclosure = /[\{\[].*[\/]*.*[\}\]]$/;
1010
var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
1111
var escaped = /\\([\*\?\|\[\]\(\)\{\}])/g;
1212

13-
module.exports = function globParent(str) {
13+
/**
14+
* @param {string} str
15+
* @param {Object} opts
16+
* @param {boolean} [opts.flipBackslashes=true]
17+
*/
18+
module.exports = function globParent(str, opts) {
19+
var options = Object.assign({ flipBackslashes: true }, opts);
20+
1421
// flip windows path separators
15-
if (isWin32 && str.indexOf(slash) < 0) {
22+
if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
1623
str = str.replace(backslash, slash);
1724
}
1825

test/index.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ describe('glob-parent', function() {
8484
assert.equal(gp('\\{foo,bar}/'), '{foo,bar}');
8585
assert.equal(gp('\\{foo,bar\\}/'), '{foo,bar}');
8686
assert.equal(gp('{foo,bar\\}/'), '.');
87-
if (!isWin32) {
87+
88+
if (isWin32) {
89+
// On Windows we are trying to flip backslashes foo-\\( → foo-/(
90+
assert.equal(gp('foo-\\(bar\\).md'), 'foo-');
91+
} else {
92+
assert.equal(gp('foo-\\(bar\\).md'), '.');
8893
assert.equal(gp('\\[bar]'), '[bar]');
8994
assert.equal(gp('[bar\\]'), '.');
9095
assert.equal(gp('\\{foo,bar\\}'), '{foo,bar}');
@@ -135,6 +140,12 @@ describe('glob-parent', function() {
135140

136141
done();
137142
});
143+
144+
it('should respect disabled auto flip backslashes', function(done) {
145+
assert.equal(gp('foo-\\(bar\\).md', { flipBackslashes: false }), '.');
146+
147+
done();
148+
});
138149
});
139150

140151
describe('glob2base test patterns', function() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy