Comparing version 1.0.2 to 2.0.0
'use strict'; | ||
var glob = require('glob'); | ||
var reduce = require('async-array-reduce'); | ||
var isValidGlob = require('is-valid-glob'); | ||
var hasGlob = require('has-glob'); | ||
var utils = require('./utils'); | ||
const glob = require('./promise'); | ||
module.exports = function(patterns, config, cb) { | ||
if (typeof config === 'function') { | ||
cb = config; | ||
config = {}; | ||
module.exports = function(patterns, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
if (typeof cb !== 'function') { | ||
throw new Error('expected a callback function.'); | ||
} | ||
const promise = glob(patterns, options); | ||
if (!isValidGlob(patterns)) { | ||
cb(new Error('invalid glob pattern: ' + patterns)); | ||
if (typeof callback === 'function') { | ||
promise.then(files => callback(null, files)).catch(callback); | ||
return; | ||
} | ||
// shallow clone options | ||
var options = Object.assign({cwd: ''}, config); | ||
options.cwd = utils.cwd(options); | ||
patterns = utils.arrayify(patterns); | ||
if (!hasGlob(patterns)) { | ||
patterns = utils.getPaths(patterns, options); | ||
cb(null, patterns); | ||
return; | ||
} | ||
var sifted = utils.sift(patterns, options); | ||
var Glob = glob.Glob; | ||
var cache = []; | ||
var res; | ||
function updateOptions(inclusive) { | ||
return utils.setIgnores(options, sifted.excludes, inclusive.index); | ||
} | ||
reduce(sifted.includes, [], function(acc, include, next) { | ||
var opts = updateOptions(include); | ||
if (acc.glob) { | ||
opts.cache = acc.glob.cache; | ||
} | ||
res = new Glob(include.pattern, opts, function(err, files) { | ||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
cache.push(res.cache); | ||
acc = acc.concat(files); | ||
acc.glob = res; | ||
next(null, acc); | ||
}); | ||
}, function(err, files) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
Object.defineProperty(files, 'cache', { | ||
configurable: true, | ||
get: function() { | ||
return utils.createCache(cache); | ||
} | ||
}); | ||
delete files.glob; | ||
cb(null, files); | ||
}); | ||
return promise; | ||
}; |
'use strict'; | ||
var glob = require('./async'); | ||
const Glob = require('glob').Glob; | ||
const isValidGlob = require('is-valid-glob'); | ||
const hasGlob = require('has-glob'); | ||
const utils = require('./utils'); | ||
module.exports = function(patterns, options) { | ||
return new Promise(function(resolve, reject) { | ||
glob(patterns, options, function(err, files) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(files); | ||
module.exports = async function(patterns, options) { | ||
if (!isValidGlob(patterns)) { | ||
throw new Error('invalid glob pattern: ' + patterns); | ||
} | ||
patterns = utils.arrayify(patterns); | ||
const opts = Object.assign({ cwd: process.cwd() }, options); | ||
opts.cwd = utils.cwd(opts); | ||
opts.cache = {}; | ||
if (!hasGlob(patterns)) { | ||
return Promise.resolve(utils.getPaths(patterns, opts)); | ||
} | ||
const glob = (pattern, options) => { | ||
return new Promise((resolve, reject) => { | ||
let acc = new Glob(pattern, options, (err, files) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
acc.files = files; | ||
resolve(acc); | ||
} | ||
}); | ||
}); | ||
}; | ||
const sifted = utils.sift(patterns, opts); | ||
const excludes = sifted.excludes; | ||
const includes = sifted.includes; | ||
const cache = []; | ||
const files = []; | ||
let acc = { cache: {} }; | ||
function updateOptions(include) { | ||
return utils.setIgnores(opts, excludes, include.index); | ||
} | ||
for (const include of includes) { | ||
const opt = updateOptions(include); | ||
opt.cache = acc.cache; | ||
acc = await glob(include.pattern, opt); | ||
cache.push(acc.cache); | ||
files.push.apply(files, acc.found); | ||
} | ||
Object.defineProperty(files, 'cache', { | ||
configurable: true, | ||
enumerable: false, | ||
get: () => utils.createCache(cache) | ||
}); | ||
return files; | ||
}; |
'use strict'; | ||
var glob = require('glob'); | ||
var hasGlob = require('has-glob'); | ||
var isValidGlob = require('is-valid-glob'); | ||
var utils = require('./utils'); | ||
const Glob = require('glob').Glob; | ||
const isValidGlob = require('is-valid-glob'); | ||
const hasGlob = require('has-glob'); | ||
const utils = require('./utils'); | ||
module.exports = function(patterns, config) { | ||
module.exports = function(patterns, options) { | ||
if (!isValidGlob(patterns)) { | ||
@@ -13,37 +13,33 @@ throw new Error('invalid glob pattern: ' + patterns); | ||
patterns = utils.arrayify(patterns); | ||
// shallow clone options | ||
var options = Object.assign({cwd: '', nosort: true}, config); | ||
options.cwd = utils.cwd(options); | ||
options.cache = {}; | ||
const opts = Object.assign({ cwd: '', nosort: true }, options); | ||
opts.cwd = utils.cwd(opts); | ||
opts.cache = {}; | ||
patterns = utils.arrayify(patterns); | ||
if (!hasGlob(patterns)) { | ||
return utils.getPaths(patterns, options); | ||
return utils.getPaths(patterns, opts); | ||
} | ||
var Glob = glob.Glob; | ||
var sifted = utils.sift(patterns, options); | ||
var excludes = sifted.excludes; | ||
var includes = sifted.includes; | ||
var res = { cache: {} }; | ||
var cache = []; | ||
const sifted = utils.sift(patterns, opts); | ||
const excludes = sifted.excludes; | ||
const includes = sifted.includes; | ||
const cache = []; | ||
const files = []; | ||
let acc = { cache: {} }; | ||
function updateOptions(include) { | ||
return utils.setIgnores(options, excludes, include.index); | ||
return utils.setIgnores(opts, excludes, include.index); | ||
} | ||
var len = includes.length; | ||
var idx = -1; | ||
var files = []; | ||
for (const include of includes) { | ||
const opt = updateOptions(include); | ||
opt.cache = acc.cache; | ||
opt.sync = true; | ||
while (++idx < len) { | ||
var include = includes[idx]; | ||
var opts = updateOptions(include); | ||
opts.cache = res.cache; | ||
opts.sync = true; | ||
acc = new Glob(include.pattern, opt); | ||
cache.push(acc.cache); | ||
res = new Glob(include.pattern, opts); | ||
cache.push(res.cache); | ||
files.push.apply(files, res.found); | ||
files.push.apply(files, acc.found); | ||
} | ||
@@ -53,5 +49,4 @@ | ||
configurable: true, | ||
get: function() { | ||
return utils.createCache(cache); | ||
} | ||
enumerable: false, | ||
get: () => utils.createCache(cache) | ||
}); | ||
@@ -58,0 +53,0 @@ |
155
lib/utils.js
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var union = require('arr-union'); | ||
var resolve = require('resolve-dir'); | ||
var utils = module.exports; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const union = require('arr-union'); | ||
const resolve = require('resolve-dir'); | ||
/** | ||
* utils | ||
* Create a new `GlobStat` object with information about a | ||
* single glob pattern. | ||
* | ||
* @param {String} `pattern` GlobStat pattern | ||
* @param {Number} `idx` Index of the glob in the given array of patterns | ||
*/ | ||
utils.arrayify = function(val) { | ||
class GlobStat { | ||
constructor(pattern, idx) { | ||
this.index = idx; | ||
this.isNegated = false; | ||
this.pattern = pattern; | ||
if (pattern.charAt(0) === '!' && pattern.charAt(1) !== '(') { | ||
this.isNegated = true; | ||
this.pattern = pattern.slice(1); | ||
} | ||
} | ||
} | ||
/** | ||
* Resolve the `cwd` to use for a glob operation. | ||
* | ||
* @param {Object} `options` | ||
* @return {String} | ||
*/ | ||
exports.cwd = options => path.resolve(resolve(options.cwd)); | ||
/** | ||
* exports | ||
*/ | ||
exports.arrayify = function(val) { | ||
return val ? (Array.isArray(val) ? val : [val]) : []; | ||
@@ -32,11 +61,9 @@ }; | ||
utils.createCache = function(arr) { | ||
exports.createCache = function(arr) { | ||
const join = cwd => fp => path.join(cwd, fp); | ||
return arr.reduce(function(acc, cache) { | ||
var keys = Object.keys(cache); | ||
var len = keys.length, i = -1; | ||
for (const key of Object.keys(cache)) { | ||
const val = cache[key]; | ||
while (++i < len) { | ||
var key = keys[i]; | ||
var val = cache[key]; | ||
if (Array.isArray(val)) { | ||
@@ -53,15 +80,2 @@ union(acc, val.map(join(key))); | ||
/** | ||
* Returns a function to join `cwd` to the given file path | ||
* | ||
* @param {String} cwd | ||
* @return {String} | ||
*/ | ||
function join(cwd) { | ||
return function(fp) { | ||
return path.join(cwd, fp); | ||
}; | ||
} | ||
/** | ||
* Sift glob patterns into inclusive and exclusive patterns. | ||
@@ -74,12 +88,12 @@ * | ||
utils.sift = function(patterns, opts) { | ||
patterns = utils.arrayify(patterns); | ||
var res = { includes: [], excludes: [] }; | ||
var len = patterns.length, i = -1; | ||
exports.sift = function(patterns, opts) { | ||
patterns = exports.arrayify(patterns); | ||
const res = { includes: [], excludes: [] }; | ||
let n = 0; | ||
while (++i < len) { | ||
var stat = new utils.Stat(patterns[i], i); | ||
for (const pattern of patterns) { | ||
const stat = new GlobStat(pattern, n++); | ||
if (opts.relative) { | ||
stat.pattern = utils.toRelative(stat.pattern, opts); | ||
stat.pattern = exports.toRelative(stat.pattern, opts); | ||
delete opts.cwd; | ||
@@ -94,3 +108,2 @@ } | ||
} | ||
res.stat = stat; | ||
return res; | ||
@@ -108,14 +121,13 @@ }; | ||
utils.setIgnores = function(options, excludes, inclusiveIndex) { | ||
var opts = Object.assign({}, options); | ||
var negations = []; | ||
exports.setIgnores = function(options, excludes, inclusiveIndex) { | ||
const opts = Object.assign({}, options); | ||
const negations = []; | ||
var len = excludes.length, i = -1; | ||
while (++i < len) { | ||
var exclusion = excludes[i]; | ||
if (exclusion.index > inclusiveIndex) { | ||
negations.push(exclusion.pattern); | ||
for (const exclusive of excludes) { | ||
if (exclusive.index > inclusiveIndex) { | ||
negations.push(exclusive.pattern); | ||
} | ||
} | ||
opts.ignore = utils.arrayify(opts.ignore || []); | ||
opts.ignore = exports.arrayify(opts.ignore || []); | ||
opts.ignore.push.apply(opts.ignore, negations); | ||
@@ -126,32 +138,2 @@ return opts; | ||
/** | ||
* Create a new `Stat` object with information about a | ||
* single glob pattern. | ||
* | ||
* @param {String} `pattern` Glob pattern | ||
* @param {Number} `idx` Index of the glob in the given array of patterns | ||
*/ | ||
utils.Stat = function(pattern, idx) { | ||
this.index = idx; | ||
this.isNegated = false; | ||
this.pattern = pattern; | ||
if (pattern.charAt(0) === '!' && pattern.charAt(1) !== '(') { | ||
this.isNegated = true; | ||
this.pattern = pattern.slice(1); | ||
} | ||
}; | ||
/** | ||
* Resolve the `cwd` to use for a glob operation. | ||
* | ||
* @param {Object} `options` | ||
* @return {String} | ||
*/ | ||
utils.cwd = function(options) { | ||
return path.resolve(resolve(options.cwd)); | ||
}; | ||
/** | ||
* Make a glob pattern relative. | ||
@@ -164,5 +146,4 @@ * | ||
utils.toRelative = function(pattern, opts) { | ||
var fp = path.resolve(opts.cwd, pattern); | ||
return path.relative(process.cwd(), fp); | ||
exports.toRelative = function(pattern, opts) { | ||
return path.relative(process.cwd(), path.resolve(opts.cwd, pattern)); | ||
}; | ||
@@ -178,16 +159,8 @@ | ||
utils.getPaths = function(paths, opts) { | ||
return paths.reduce(function(acc, fp) { | ||
var abs = path.resolve(opts.cwd, fp); | ||
if (fs.existsSync(abs)) { | ||
acc.push(opts.realpath ? abs : fp); | ||
} | ||
return acc; | ||
}, []); | ||
exports.getPaths = function(paths, opts) { | ||
paths = paths.filter(fp => fs.existsSync(path.resolve(opts.cwd, fp))); | ||
if (opts.realpath) { | ||
return paths.map(fp => path.resolve(opts.cwd, fp)); | ||
} | ||
return paths; | ||
}; | ||
/** | ||
* Expose `utils` | ||
*/ | ||
module.exports = utils; |
{ | ||
"name": "matched", | ||
"description": "Adds array support to node-glob, sync and async. Also supports tilde expansion (user home) and resolving to global npm modules.", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jonschlinkert/matched", | ||
@@ -22,3 +22,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"engines": { | ||
"node": ">= 0.12.0" | ||
"node": ">= 6" | ||
}, | ||
@@ -32,14 +32,13 @@ "scripts": { | ||
"arr-union": "^3.1.0", | ||
"async-array-reduce": "^0.2.1", | ||
"glob": "^7.1.2", | ||
"has-glob": "^1.0.0", | ||
"is-valid-glob": "^1.0.0", | ||
"resolve-dir": "^1.0.0" | ||
"resolve-dir": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.0.0", | ||
"gulp-format-md": "^0.1.12", | ||
"mocha": "^3.0.2", | ||
"nyc": "^11.0.2", | ||
"rimraf": "^2.5.2" | ||
"eslint": "^4.19.0", | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^3.5.3", | ||
"nyc": "^11.6.0", | ||
"rimraf": "^2.6.2" | ||
}, | ||
@@ -46,0 +45,0 @@ "keywords": [ |
100
README.md
@@ -5,2 +5,4 @@ # matched [![NPM version](https://img.shields.io/npm/v/matched.svg?style=flat)](https://www.npmjs.com/package/matched) [![NPM monthly downloads](https://img.shields.io/npm/dm/matched.svg?style=flat)](https://npmjs.org/package/matched) [![NPM total downloads](https://img.shields.io/npm/dt/matched.svg?style=flat)](https://npmjs.org/package/matched) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/matched.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/matched) | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
@@ -19,11 +21,18 @@ | ||
```js | ||
var glob = require('matched'); | ||
const glob = require('matched'); | ||
``` | ||
**async** | ||
**promise** | ||
```js | ||
glob(['*.js'], function(err, files) { | ||
//=> ['utils.js', 'index.js'] | ||
}); | ||
glob(['*.txt']) | ||
.then(files => console.log(files)) //=> ['a.txt', 'b.txt', 'c.txt'] | ||
.catch(console.error) | ||
// or with async-await | ||
(async function() { | ||
const files = await glob('*.txt'); | ||
})(); | ||
``` | ||
@@ -34,13 +43,13 @@ | ||
```js | ||
var files = glob.sync(['*.js']); | ||
const files = glob.sync(['*.js']); | ||
//=> ['utils.js', 'index.js'] | ||
``` | ||
**promise** | ||
**callback** | ||
```js | ||
glob.promise(['*.txt']) | ||
.then(function(files) { | ||
//=> ['a.txt', 'b.txt', 'c.txt'] | ||
}); | ||
glob(['*.js'], (err, files) => { | ||
console.log(files); | ||
//=> ['utils.js', 'index.js'] | ||
}); | ||
``` | ||
@@ -50,8 +59,7 @@ | ||
All methods take options as the second argument: | ||
All methods take [node-glob](https://github.com/isaacs/node-glob) options as the second argument: | ||
```js | ||
glob(['*.js'], {cwd: 'test'}, function(err, files) { | ||
//=> ['test.js'] | ||
}); | ||
const glob = glob(['*.js'], { cwd: 'test' }); | ||
//=> ['test.js'] | ||
``` | ||
@@ -61,8 +69,4 @@ | ||
### v1.0.0 | ||
**v0.4.1** | ||
* Minor code improvements, dependency upgrades | ||
### v0.4.1 | ||
* Exposes a non-enumerable `cache` property on the returned files array. This is a patch relase since the property does not change the existing API and should not otherwise effect behavior or results. | ||
@@ -72,22 +76,23 @@ | ||
### Related projects | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
* [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet") | ||
* [look-up](https://www.npmjs.com/package/look-up): Faster drop-in replacement for find-up and findup-sync. | [homepage](https://github.com/jonschlinkert/look-up "Faster drop-in replacement for find-up and findup-sync.") | ||
* [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.") | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Contributing | ||
</details> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
### Contributors | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 48 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 7 | [TrySound](https://github.com/TrySound) | | ||
| 1 | [sindresorhus](https://github.com/sindresorhus) | | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
### Building docs | ||
</details> | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
@@ -101,10 +106,20 @@ | ||
### Running tests | ||
</details> | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
### Related projects | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
You might also be interested in these projects: | ||
* [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet") | ||
* [look-up](https://www.npmjs.com/package/look-up): Faster drop-in replacement for find-up and findup-sync. | [homepage](https://github.com/jonschlinkert/look-up "Faster drop-in replacement for find-up and findup-sync.") | ||
* [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.") | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 56 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 8 | [TrySound](https://github.com/TrySound) | | ||
| 1 | [sindresorhus](https://github.com/sindresorhus) | | ||
### Author | ||
@@ -114,8 +129,9 @@ | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) | ||
* [GitHub Profile](https://github.com/jonschlinkert) | ||
* [Twitter Profile](https://twitter.com/jonschlinkert) | ||
### License | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
@@ -125,2 +141,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 23, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 21, 2018._ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
133
14706
243
1
- Removedasync-array-reduce@^0.2.1
- Removedasync-array-reduce@0.2.1(transitive)
Updatedresolve-dir@^1.0.1