Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/globby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2b49f02ac7211b8de3eb570e0d81d8733b249a9b
Choose a base ref
...
head repository: sindresorhus/globby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3f0e82bc72a35ba54d052ff0edaad55228f4ee8e
Choose a head ref
  • 7 commits
  • 6 files changed
  • 3 contributors

Commits on Jul 14, 2015

  1. Use arrify

    kevva committed Jul 14, 2015
    Copy the full SHA
    fcafcf6 View commit details
  2. Tweaks

    kevva committed Jul 14, 2015
    Copy the full SHA
    3b6afd2 View commit details

Commits on Aug 29, 2015

  1. Use a Promise API instead of a callback.

    wbinnssmith authored and Will Binns-Smith committed Aug 29, 2015
    Copy the full SHA
    f74370a View commit details

Commits on Aug 30, 2015

  1. Merge pull request #17 from wbinnssmith/wbinnssmith/promise-api

    Use a Promise API instead of a callback.
    sindresorhus committed Aug 30, 2015
    Copy the full SHA
    e1f23cd View commit details
  2. minor doc tweaks

    sindresorhus committed Aug 30, 2015
    Copy the full SHA
    f287438 View commit details
  3. add XO

    sindresorhus committed Aug 30, 2015
    Copy the full SHA
    9b010a0 View commit details
  4. 3.0.0

    sindresorhus committed Aug 30, 2015
    Copy the full SHA
    3f0e82b View commit details
Showing with 86 additions and 106 deletions.
  1. +0 −13 .jshintrc
  2. +1 −0 bench.js
  3. +16 −36 index.js
  4. +38 −28 package.json
  5. +11 −10 readme.md
  6. +20 −19 test.js
13 changes: 0 additions & 13 deletions .jshintrc

This file was deleted.

1 change: 1 addition & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global bench */
'use strict';
var fs = require('fs');
var rimraf = require('rimraf');
52 changes: 16 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';
var union = require('array-union');
var assign = require('object-assign');
var async = require('async');
var Promise = require('pinkie-promise');
var arrayUnion = require('array-union');
var objectAssign = require('object-assign');
var glob = require('glob');

function arrayify(arr) {
return Array.isArray(arr) ? arr : [arr];
}
var arrify = require('arrify');
var pify = require('pify');

function sortPatterns(patterns) {
patterns = arrayify(patterns);
patterns = arrify(patterns);

var positives = [];
var negatives = [];
@@ -29,7 +27,7 @@ function sortPatterns(patterns) {
}

function setIgnore(opts, negatives, positiveIndex) {
opts = assign({}, opts);
opts = objectAssign({}, opts);

var negativePatterns = negatives.filter(function (negative) {
return negative.index > positiveIndex;
@@ -41,37 +39,19 @@ function setIgnore(opts, negatives, positiveIndex) {
return opts;
}

module.exports = function (patterns, opts, cb) {
module.exports = function (patterns, opts) {
var sortedPatterns = sortPatterns(patterns);

if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = opts || {};

if (sortedPatterns.positives.length === 0) {
cb(null, []);
return;
return Promise.resolve([]);
}

async.parallel(sortedPatterns.positives.map(function (positive) {
return function (cb2) {
glob(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index), function (err, paths) {
if (err) {
cb2(err);
return;
}

cb2(null, paths);
});
};
}), function (err, paths) {
if (err) {
cb(err);
return;
}

cb(null, union.apply(null, paths));
return Promise.all(sortedPatterns.positives.map(function (positive) {
var globOpts = setIgnore(opts, sortedPatterns.negatives, positive.index);
return pify(glob, Promise)(positive.pattern, globOpts);
})).then(function (paths) {
return arrayUnion.apply(null, paths);
});
};

@@ -83,6 +63,6 @@ module.exports.sync = function (patterns, opts) {
}

return sortedPatterns.positives.reduce(function (ret, positive) {
return union(ret, glob.sync(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index)));
return arrayUnion(ret, glob.sync(positive.pattern, setIgnore(opts, sortedPatterns.negatives, positive.index)));
}, []);
};
66 changes: 38 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
{
"name": "globby",
"version": "2.1.0",
"description": "Extends `glob` with support for multiple patterns",
"version": "3.0.0",
"description": "Extends `glob` with support for multiple patterns and exposes a Promise API",
"license": "MIT",
"repository": "sindresorhus/globby",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"name": "Sindre Sorhus",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"bench": "npm update globby glob-stream && matcha bench.js",
"test": "mocha"
"test": "xo && mocha"
},
"files": [
"index.js"
],
"keywords": [
"all",
"array",
"directories",
"dirs",
"expand",
"files",
"filesystem",
"filter",
"find",
"fnmatch",
"folders",
"fs",
"glob",
"globs",
"globbing",
"minimatch",
"globs",
"gulpfriendly",
"match",
"matcher",
"pattern",
"patterns",
"find",
"paths",
"files",
"folders",
"dirs",
"directories",
"wildcard",
"wildcards",
"expand",
"minimatch",
"multi",
"multiple",
"array",
"all",
"gulpfriendly",
"fnmatch",
"paths",
"pattern",
"patterns",
"traverse",
"filesystem",
"fs",
"filter",
"util",
"utility"
"utility",
"wildcard",
"wildcards",
"promise"
],
"dependencies": {
"array-union": "^1.0.1",
"async": "^1.2.1",
"arrify": "^1.0.0",
"glob": "^5.0.3",
"object-assign": "^3.0.0"
"object-assign": "^4.0.1",
"pify": "^1.0.0",
"pinkie-promise": "^1.0.0"
},
"devDependencies": {
"glob-stream": "wearefractal/glob-stream#master",
"globby": "sindresorhus/globby#master",
"matcha": "^0.6.0",
"mocha": "*",
"rimraf": "^2.2.8"
"rimraf": "^2.2.8",
"xo": "*"
},
"xo": {
"envs": [
"node",
"mocha"
]
}
}
21 changes: 11 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# globby [![Build Status](https://travis-ci.org/sindresorhus/globby.svg?branch=master)](https://travis-ci.org/sindresorhus/globby)

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

## Install
@@ -19,35 +19,36 @@ $ npm install --save globby
```

```js
var globby = require('globby');
const globby = require('globby');

globby(['*', '!cake'], function (err, paths) {
console.log(paths);
globby(['*', '!cake']).then(paths => {
console.log(paths)
//=> ['unicorn', 'rainbows']
});
```


## API

### globby(patterns, [options], callback)
### globby(patterns, [options])

Returns a promise that resolves to an array of matching paths.

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

Returns an array of matching paths.

#### patterns

*Required*
Type: `string`, `array`

See supported minimatch [patterns](https://github.com/isaacs/minimatch#usage).
See supported `minimatch` [patterns](https://github.com/isaacs/minimatch#usage).

#### options

Type: `object`

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

#### callback(err, paths)
See the `node-glob` [options](https://github.com/isaacs/node-glob#options).


## Globbing patterns
39 changes: 20 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -22,27 +22,21 @@ after(function () {
fixture.forEach(fs.unlinkSync.bind(fs));
});

it('should glob - async', function (cb) {
globby('*.tmp', function (err, paths) {
assert(!err, err);
it('should glob - async', function () {
return globby('*.tmp').then(function (paths) {
assert.deepEqual(paths, ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
cb();
});
});

it('should glob with multiple patterns - async', function (cb) {
globby(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], function (err, paths) {
assert(!err, err);
it('should glob with multiple patterns - async', function () {
return globby(['a.tmp', '*.tmp', '!{c,d,e}.tmp']).then(function (paths) {
assert.deepEqual(paths, ['a.tmp', 'b.tmp']);
cb();
});
});

it('should respect patterns order - async', function (cb) {
globby(['!*.tmp', 'a.tmp'], function (err, paths) {
assert(!err, err);
it('should respect patterns order - async', function () {
return globby(['!*.tmp', 'a.tmp']).then(function (paths) {
assert.deepEqual(paths, ['a.tmp']);
cb();
});
});

@@ -52,20 +46,27 @@ it('should glob - sync', function () {
assert.deepEqual(globby.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
});

it('should return [] for all negative patterns - sync', function () {
assert.deepEqual(globby.sync(['!a.tmp', '!b.tmp']), []);
});

it('should return [] for all negative patterns - async', function () {
return globby(['!a.tmp', '!b.tmp']).then(function (paths) {
assert.deepEqual(paths, []);
});
});

it('cwd option', function () {
process.chdir('tmp');
assert.deepEqual(globby.sync('*.tmp', {cwd: cwd}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
assert.deepEqual(globby.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], {cwd: cwd}), ['a.tmp', 'b.tmp']);
process.chdir(cwd);
});

it('should not mutate the options object - async', function(cb) {
globby(['*.tmp', '!b.tmp'], Object.freeze({ ignore: Object.freeze([]) }), function (err, paths) {
assert(!err, err);
cb();
});
it('should not mutate the options object - async', function () {
return globby(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
});

it('should not mutate the options object - sync', function() {
globby.sync(['*.tmp', '!b.tmp'], Object.freeze({ ignore: Object.freeze([]) }));
it('should not mutate the options object - sync', function () {
globby.sync(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
});