Skip to content

Commit e1f23cd

Browse files
committedAug 30, 2015
Merge pull request #17 from wbinnssmith/wbinnssmith/promise-api
Use a Promise API instead of a callback.
2 parents 3b6afd2 + f74370a commit e1f23cd

File tree

4 files changed

+35
-50
lines changed

4 files changed

+35
-50
lines changed
 

‎index.js

+10-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
2+
var Promise = require('pinkie-promise');
23
var arrayUnion = require('array-union');
34
var objectAssign = require('object-assign');
4-
var async = require('async');
55
var glob = require('glob');
66
var arrify = require('arrify');
7+
var pify = require('pify');
78

89
function sortPatterns(patterns) {
910
patterns = arrify(patterns);
@@ -38,37 +39,19 @@ function setIgnore(opts, negatives, positiveIndex) {
3839
return opts;
3940
}
4041

41-
module.exports = function (patterns, opts, cb) {
42+
module.exports = function (patterns, opts) {
4243
var sortedPatterns = sortPatterns(patterns);
43-
44-
if (typeof opts === 'function') {
45-
cb = opts;
46-
opts = {};
47-
}
44+
opts = opts || {};
4845

4946
if (sortedPatterns.positives.length === 0) {
50-
cb(null, []);
51-
return;
47+
return Promise.resolve([]);
5248
}
5349

54-
async.parallel(sortedPatterns.positives.map(function (positive) {
55-
return function (cb2) {
56-
glob(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index), function (err, paths) {
57-
if (err) {
58-
cb2(err);
59-
return;
60-
}
61-
62-
cb2(null, paths);
63-
});
64-
};
65-
}), function (err, paths) {
66-
if (err) {
67-
cb(err);
68-
return;
69-
}
70-
71-
cb(null, arrayUnion.apply(null, paths));
50+
return Promise.all(sortedPatterns.positives.map(function (positive) {
51+
var globOpts = setIgnore(opts, sortedPatterns.negatives, positive.index);
52+
return pify(glob, Promise)(positive.pattern, globOpts);
53+
})).then(function (paths) {
54+
return arrayUnion.apply(null, paths);
7255
});
7356
};
7457

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353
"dependencies": {
5454
"array-union": "^1.0.1",
5555
"arrify": "^1.0.0",
56-
"async": "^1.2.1",
5756
"glob": "^5.0.3",
58-
"object-assign": "^3.0.0"
57+
"object-assign": "^3.0.0",
58+
"pify": "^1.0.0",
59+
"pinkie-promise": "^1.0.0"
5960
},
6061
"devDependencies": {
6162
"glob-stream": "wearefractal/glob-stream#master",

‎readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# globby [![Build Status](https://travis-ci.org/sindresorhus/globby.svg?branch=master)](https://travis-ci.org/sindresorhus/globby)
22

3-
> Extends [glob](https://github.com/isaacs/node-glob) with support for multiple patterns
3+
> Extends [glob](https://github.com/isaacs/node-glob) with support for multiple patterns and exposes a Promise API
44
55

66
## Install
@@ -30,7 +30,7 @@ globby(['*', '!cake'], function (err, paths) {
3030

3131
## API
3232

33-
### globby(patterns, [options], callback)
33+
### globby(patterns, [options])
3434

3535
### globby.sync(patterns, [options])
3636

@@ -47,8 +47,8 @@ Type: `object`
4747

4848
See the node-glob [options](https://github.com/isaacs/node-glob#options).
4949

50-
#### callback(err, paths)
51-
50+
`globby.sync` synchronously returns an array of paths matching the glob expression.
51+
`globby` returns a Promise object that resolves to an array of matching paths or is rejected with an error.
5252

5353
## Globbing patterns
5454

‎test.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,21 @@ after(function () {
2222
fixture.forEach(fs.unlinkSync.bind(fs));
2323
});
2424

25-
it('should glob - async', function (cb) {
26-
globby('*.tmp', function (err, paths) {
27-
assert(!err, err);
25+
it('should glob - async', function () {
26+
return globby('*.tmp').then(function (paths) {
2827
assert.deepEqual(paths, ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
29-
cb();
3028
});
3129
});
3230

33-
it('should glob with multiple patterns - async', function (cb) {
34-
globby(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], function (err, paths) {
35-
assert(!err, err);
31+
it('should glob with multiple patterns - async', function () {
32+
return globby(['a.tmp', '*.tmp', '!{c,d,e}.tmp']).then(function (paths) {
3633
assert.deepEqual(paths, ['a.tmp', 'b.tmp']);
37-
cb();
3834
});
3935
});
4036

41-
it('should respect patterns order - async', function (cb) {
42-
globby(['!*.tmp', 'a.tmp'], function (err, paths) {
43-
assert(!err, err);
37+
it('should respect patterns order - async', function () {
38+
return globby(['!*.tmp', 'a.tmp']).then(function (paths) {
4439
assert.deepEqual(paths, ['a.tmp']);
45-
cb();
4640
});
4741
});
4842

@@ -52,18 +46,25 @@ it('should glob - sync', function () {
5246
assert.deepEqual(globby.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
5347
});
5448

49+
it('should return [] for all negative patterns - sync', function () {
50+
assert.deepEqual(globby.sync(['!a.tmp', '!b.tmp']), []);
51+
})
52+
53+
it('should return [] for all negative patterns - async', function () {
54+
return globby(['!a.tmp', '!b.tmp']).then(function (paths) {
55+
assert.deepEqual(paths, []);
56+
});
57+
})
58+
5559
it('cwd option', function () {
5660
process.chdir('tmp');
5761
assert.deepEqual(globby.sync('*.tmp', {cwd: cwd}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
5862
assert.deepEqual(globby.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], {cwd: cwd}), ['a.tmp', 'b.tmp']);
5963
process.chdir(cwd);
6064
});
6165

62-
it('should not mutate the options object - async', function (cb) {
63-
globby(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}), function (err, paths) {
64-
assert(!err, err);
65-
cb();
66-
});
66+
it('should not mutate the options object - async', function () {
67+
return globby(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
6768
});
6869

6970
it('should not mutate the options object - sync', function () {

0 commit comments

Comments
 (0)
Please sign in to comment.