Comparing version 0.1.2 to 0.1.3
127
index.js
@@ -6,5 +6,5 @@ 'use strict'; | ||
var glob = require('glob'); | ||
var _ = require('underscore'); | ||
var async = require('async'); | ||
var glob = require('glob'); | ||
var _ = require('underscore'); | ||
var async = require('async'); | ||
@@ -15,79 +15,78 @@ // Process specified wildcard glob patterns or filenames against a | ||
// Filepaths to return. | ||
var result = []; | ||
// Iterate over flattened patterns array. | ||
_.flatten(patterns).forEach(function(pattern) { | ||
// If the first character is ! it should be omitted | ||
var exclusion = pattern.indexOf('!') === 0; | ||
// If the pattern is an exclusion, remove the ! | ||
if (exclusion) { | ||
pattern = pattern.slice(1); | ||
} | ||
// Find all matching files for this pattern. | ||
var matches = fn(pattern); | ||
// Filepaths to return. | ||
var result = []; | ||
// Iterate over flattened patterns array. | ||
_.flatten(patterns).forEach(function(pattern) { | ||
// If the first character is ! it should be omitted | ||
var exclusion = pattern.indexOf('!') === 0; | ||
// If the pattern is an exclusion, remove the ! | ||
if (exclusion) { | ||
pattern = pattern.slice(1); | ||
} | ||
// Find all matching files for this pattern. | ||
var matches = fn(pattern); | ||
if (exclusion) { | ||
// If an exclusion, remove matching files. | ||
result = _.difference(result, matches); | ||
} else { | ||
// Otherwise add matching files. | ||
result = _.union(result, matches); | ||
} | ||
}); | ||
return result; | ||
if (exclusion) { | ||
// If an exclusion, remove matching files. | ||
result = _.difference(result, matches); | ||
} else { | ||
// Otherwise add matching files. | ||
result = _.union(result, matches); | ||
} | ||
}); | ||
return result; | ||
} | ||
function expand (patterns, options, callback) { | ||
patterns = Array.isArray(patterns) ? patterns : [patterns]; | ||
function expand(patterns, options, callback) { | ||
patterns = Array.isArray(patterns) ? patterns : [patterns]; | ||
if ( typeof options === 'function' ) { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
async.parallel( | ||
patterns.map(function (pattern) { | ||
return function (done) { | ||
glob(pattern, options, done); | ||
}; | ||
}), | ||
async.parallel( | ||
patterns.map(function(pattern) { | ||
return function(done) { | ||
glob(pattern, options, done); | ||
}; | ||
}), | ||
function (err, results) { | ||
if ( err ) { | ||
return callback(err); | ||
} | ||
function(err, results) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var result = []; | ||
var result = []; | ||
patterns.forEach(function(pattern, i) { | ||
var exclusion = pattern.indexOf('!') === 0; | ||
patterns.forEach(function (pattern, i) { | ||
var exclusion = pattern.indexOf('!') === 0; | ||
if (exclusion) { | ||
result = _.difference(result, results[i]); | ||
} else { | ||
result = _.union(result, results[i]); | ||
} | ||
}); | ||
if ( exclusion ) { | ||
result = _.difference(result, results[i]); | ||
} else { | ||
result = _.union(result, results[i]); | ||
} | ||
}); | ||
callback(null, result); | ||
} | ||
); | ||
callback(null, result); | ||
} | ||
); | ||
} | ||
function expandSync (patterns, options) { | ||
// Use the first argument if it's an Array, otherwise convert the arguments | ||
// object to an array and use that. | ||
patterns = Array.isArray(patterns) ? patterns : [patterns]; | ||
function expandSync(patterns, options) { | ||
// Use the first argument if it's an Array, otherwise convert the arguments | ||
// object to an array and use that. | ||
patterns = Array.isArray(patterns) ? patterns : [patterns]; | ||
return patterns.length === 0 ? [] : | ||
return patterns.length === 0 ? [] : | ||
processPatterns(patterns, function(pattern) { | ||
processPatterns(patterns, function(pattern) { | ||
// Find all matching files for this pattern. | ||
return glob.sync(pattern, options); | ||
}); | ||
// Find all matching files for this pattern. | ||
return glob.sync(pattern, options); | ||
}); | ||
} | ||
{ | ||
"name": "fs-expand", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "An extended fs glob", | ||
@@ -29,6 +29,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"async": "~0.2.10", | ||
"underscore": "~1.6.0", | ||
"glob": "~3.2.8" | ||
"glob": "~4.0.2", | ||
"async": "~0.9.0" | ||
} | ||
} |
@@ -5,2 +5,7 @@ # fs-expand [](http://badge.fury.io/js/fs-expand) [](https://travis-ci.org/kaelzhang/node-fs-expand) [](https://gemnasium.com/kaelzhang/node-fs-expand) | ||
The difference from [`glob`](http://www.npmjs.org/package/glob) is that `fs-expand` | ||
- could combine the results of several glob patterns. | ||
- supports negative matching patterns, such as '!*.js' | ||
## expand(pattern, [options], callback); | ||
@@ -17,7 +22,16 @@ | ||
``` | ||
dir/ | ||
|-- a.js | ||
|-- b.js | ||
|-- README.md | ||
``` | ||
```js | ||
var expand = require('fs-expand'); | ||
expand('*.js', function(err, files){ | ||
console.log(files); // ['a.js', 'b.js'] | ||
expand(['*.js', '*.md'], { | ||
cwd: dir | ||
}, function(err, files){ | ||
console.log(files); // ['a.js', 'b.js', README.md] | ||
}); | ||
@@ -24,0 +38,0 @@ ``` |
@@ -10,32 +10,45 @@ 'use strict'; | ||
describe("expand", function(){ | ||
it("async", function(done){ | ||
expand([ | ||
'*.js', | ||
'!c.js' | ||
], { | ||
cwd: fixtures | ||
}, function (err, files) { | ||
expect(err).to.equal(null); | ||
expect(files.sort()).to.deep.equal([ | ||
'a.js', | ||
'b.js' | ||
]); | ||
done(); | ||
}); | ||
describe("expand", function() { | ||
it("async", function(done) { | ||
expand([ | ||
'*.js', | ||
'!c.js' | ||
], { | ||
cwd: fixtures | ||
}, function(err, files) { | ||
expect(err).to.equal(null); | ||
expect(files.sort()).to.deep.equal([ | ||
'a.js', | ||
'b.js' | ||
]); | ||
done(); | ||
}); | ||
}); | ||
it("sync", function(){ | ||
var files = expand.sync([ | ||
'*.js', | ||
'!c.js' | ||
], { | ||
cwd: fixtures | ||
}); | ||
it("sync", function() { | ||
var files = expand.sync([ | ||
'*.js', | ||
'!c.js' | ||
], { | ||
cwd: fixtures | ||
}); | ||
expect(files.sort()).to.deep.equal([ | ||
'a.js', | ||
'b.js' | ||
]); | ||
expect(files.sort()).to.deep.equal([ | ||
'a.js', | ||
'b.js' | ||
]); | ||
}); | ||
it("should maintain order", function(done){ | ||
expand([ | ||
'*.js', | ||
'*.md' | ||
], { | ||
cwd: fixtures | ||
}, function (err, files) { | ||
expect(err).to.equal(null); | ||
expect(files.pop()).to.equal('README.md'); | ||
done(); | ||
}); | ||
}); | ||
}); |
7192
12
115
49
+ Addedasync@0.9.2(transitive)
+ Addedglob@4.0.6(transitive)
+ Addedgraceful-fs@3.0.12(transitive)
+ Addedminimatch@1.0.0(transitive)
+ Addednatives@1.1.6(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedasync@0.2.10(transitive)
- Removedglob@3.2.11(transitive)
- Removedminimatch@0.3.0(transitive)
Updatedasync@~0.9.0
Updatedglob@~4.0.2