Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var filepaths = globule.find('**/*.js');
## Documentation

### globule.find
Returns a unique array of all file or directory paths that match the given globbing pattern(s). This method accepts either comma separated globbing patterns or an array of globbing patterns. Paths matching patterns that begin with `!` will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant. Patterns may be specified as function arguments or as a `src` property of the options object.
Returns a unique array of all file or directory paths that match the given globbing pattern(s). This method accepts either comma separated globbing patterns, an array of globbing patterns, or an array of arrays of globbing patterns. Paths matching patterns that begin with `!` will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant. Patterns may be specified as function arguments or as a `src` property of the options object. If you specify patterns inside arrays of arrays, each level will glob together, and the results will be flattened without duplicates.

```js
globule.find(patterns [, patterns [, ...]] [, options])
Expand All @@ -31,6 +31,13 @@ The `options` object supports all [glob][] library options, along with a few ext

[glob]: https://github.com/isaacs/node-glob

**Note:** Patterns as arrays of arrays are useful if you want to negate only in part of your glob results.

```js
globule.find([['**/*.js'], ['**/*.html', '!**/excl/**/*']])
```
In this example, `excl/a.js` will be matched, but not `excl/a.html`.

### globule.match
Match one or more globbing patterns against one or more file paths. Returns a uniqued array of all file paths that match any of the specified globbing patterns. Both the `patterns` and `filepaths` arguments can be a single string or array of strings. Paths matching patterns that begin with `!` will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant.

Expand Down
33 changes: 28 additions & 5 deletions lib/globule.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,18 @@ globule.match = function(patterns, filepaths, options) {
// Return empty set if either patterns or filepaths was omitted.
if (patterns == null || filepaths == null) { return []; }
// Normalize patterns and filepaths to flattened arrays.
patterns = _.isArray(patterns) ? _.flatten(patterns) : [patterns];
patterns = _.isArray(patterns) ? patterns : [patterns];
// Merge merge results if patterns are grouped
if (_.any(patterns, _.isArray)) {
return _(patterns)
.map(function(patternArray) {
return globule.match(patternArray, filepaths, options);
})
.flatten()
.uniq()
.value()
;
}
filepaths = _.isArray(filepaths) ? _.flatten(filepaths) : [filepaths];
// Return empty set if there are no patterns or filepaths.
if (patterns.length === 0 || filepaths.length === 0) { return []; }
Expand All @@ -68,15 +79,27 @@ globule.find = function() {
// If the last argument is an options object, remove it from args.
var options = _.isPlainObject(args[args.length - 1]) ? args.pop() : {};
// If options.src was specified, use it. Otherwise, use all non-options
// arguments. Flatten nested arrays.
// arguments.
var patterns;
if (options.src) {
patterns = _.isArray(options.src) ? _.flatten(options.src) : [options.src];
} else {
patterns = _.flatten(args);
patterns = _.isArray(options.src) ? options.src : [options.src];
}
else {
patterns = _.flatten(args, true);
}
// Return empty set if there are no patterns.
if (patterns.length === 0) { return []; }
// Merge find results if patterns are grouped
if (_.any(patterns, _.isArray)) {
return _(patterns)
.map(function(patternArray) {
return globule.find(patternArray, _.omit(options, 'src'));
})
.flatten()
.uniq()
.value()
;
}
var srcBase = options.srcBase || options.cwd;
// Create glob-specific options object.
var globOptions = _.extend({}, options);
Expand Down
8 changes: 7 additions & 1 deletion test/globule_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ exports['find'] = {
test.done();
},
'exclusion': function(test) {
test.expect(8);
test.expect(10);
test.deepEqual(globule.find(['!js/*.js']), [], 'solitary exclusion should match nothing');
test.deepEqual(globule.find(['js/bar.js','!js/bar.js']), [], 'exclusion should negate match');
test.deepEqual(globule.find(['**/*.js', '!js/foo.js']),
Expand All @@ -207,6 +207,12 @@ exports['find'] = {
test.deepEqual(globule.find(['js/bar.js', '!**/b*.*', 'js/foo.js', 'css/baz.css', 'css/qux.css']),
['js/foo.js', 'css/baz.css', 'css/qux.css'],
'inclusion / exclusion order matters');
test.deepEqual(globule.find([['js/bar.js'], ['!js/b*.*', 'js/foo.js']]),
['js/bar.js', 'js/foo.js'],
'exclusion in subarray should not exclude from previous sub arrays');
test.deepEqual(globule.find([['js/bar.js'], ['js/foo.js', '!js/*']]),
['js/bar.js'],
'exclusion in subarray should exclude from current sub arrays');
test.done();
},
'options.src': function(test) {
Expand Down