Comparing version 0.2.1 to 0.3.0
136
index.js
@@ -9,3 +9,2 @@ 'use strict'; | ||
var Snapdragon = require('snapdragon'); | ||
var debug = require('debug')('nanomatch'); | ||
var extend = require('extend-shallow'); | ||
@@ -40,4 +39,2 @@ | ||
function nanomatch(list, patterns, options) { | ||
debug('nanomatch <%s>', patterns); | ||
patterns = utils.arrayify(patterns); | ||
@@ -396,14 +393,7 @@ list = utils.arrayify(list); | ||
function makeRe() { | ||
var opts = extend({strictErrors: false}, options); | ||
if (opts.strictErrors === true) opts.strict = true; | ||
var res = nanomatch.create(pattern, opts); | ||
return toRegex(res.output, opts); | ||
var res = nanomatch.create(pattern, options); | ||
return toRegex(res.output, options); | ||
} | ||
var regex = memoize('makeRe', pattern, options, makeRe); | ||
if (regex.source.length > MAX_LENGTH) { | ||
throw new SyntaxError('potentially malicious regex detected'); | ||
} | ||
return regex; | ||
return memoize('makeRe', pattern, options, makeRe); | ||
}; | ||
@@ -452,8 +442,44 @@ | ||
} | ||
function create() { | ||
var snapdragon = (options && options.snapdragon) || new Snapdragon(options); | ||
compilers(snapdragon); | ||
parsers(snapdragon); | ||
return nanomatch.compile(nanomatch.parse(pattern, options), options); | ||
} | ||
return memoize('create', pattern, options, create); | ||
}; | ||
/** | ||
* Parse the given `str` with the given `options`. | ||
* | ||
* ```js | ||
* var nanomatch = require('nanomatch'); | ||
* var ast = nanomatch.parse('a/{b,c}/d'); | ||
* console.log(ast); | ||
* // { type: 'root', | ||
* // errors: [], | ||
* // input: 'a/{b,c}/d', | ||
* // nodes: | ||
* // [ { type: 'bos', val: '' }, | ||
* // { type: 'text', val: 'a/' }, | ||
* // { type: 'brace', | ||
* // nodes: | ||
* // [ { type: 'brace.open', val: '{' }, | ||
* // { type: 'text', val: 'b,c' }, | ||
* // { type: 'brace.close', val: '}' } ] }, | ||
* // { type: 'text', val: '/d' }, | ||
* // { type: 'eos', val: '' } ] } | ||
* ``` | ||
* @param {String} `str` | ||
* @param {Object} `options` | ||
* @return {Object} Returns an AST | ||
* @api public | ||
*/ | ||
nanomatch.parse = function(pattern, options) { | ||
if (typeof pattern !== 'string') { | ||
throw new TypeError('expected a string'); | ||
} | ||
function parse() { | ||
var snapdragon = instantiate(null, options); | ||
parsers(snapdragon, options); | ||
if (pattern.slice(0, 2) === './') { | ||
@@ -465,16 +491,82 @@ pattern = pattern.slice(2); | ||
var ast = snapdragon.parse(pattern, options); | ||
utils.define(ast, 'snapdragon', snapdragon); | ||
ast.input = pattern; | ||
return ast; | ||
} | ||
return memoize('parse', pattern, options, parse); | ||
}; | ||
/** | ||
* Compile the given `ast` or string with the given `options`. | ||
* | ||
* ```js | ||
* var nanomatch = require('nanomatch'); | ||
* var ast = nanomatch.parse('a/{b,c}/d'); | ||
* console.log(nanomatch.compile(ast)); | ||
* // { options: { source: 'string' }, | ||
* // state: {}, | ||
* // compilers: | ||
* // { eos: [Function], | ||
* // noop: [Function], | ||
* // bos: [Function], | ||
* // brace: [Function], | ||
* // 'brace.open': [Function], | ||
* // text: [Function], | ||
* // 'brace.close': [Function] }, | ||
* // output: [ 'a/(b|c)/d' ], | ||
* // ast: | ||
* // { ... }, | ||
* // parsingErrors: [] } | ||
* ``` | ||
* @param {Object|String} `ast` | ||
* @param {Object} `options` | ||
* @return {Object} Returns an object that has an `output` property with the compiled string. | ||
* @api public | ||
*/ | ||
nanomatch.compile = function(ast, options) { | ||
if (typeof ast === 'string') { | ||
ast = nanomatch.parse(ast, options); | ||
} | ||
function compile() { | ||
var snapdragon = instantiate(ast, options); | ||
compilers(snapdragon, options); | ||
return snapdragon.compile(ast, options); | ||
} | ||
return memoize('create', pattern, options, create); | ||
return memoize('compile', ast.input, options, compile); | ||
}; | ||
/** | ||
* Memoize a generated regex or function | ||
* Get the `Snapdragon` instance to use | ||
*/ | ||
function instantiate(ast, options) { | ||
// if an instance was created by `.parse`, use that instance | ||
if (utils.typeOf(ast) === 'object' && ast.snapdragon) { | ||
return ast.snapdragon; | ||
} | ||
// if the user supplies an instance on options, use that instance | ||
if (utils.typeOf(options) === 'object' && options.snapdragon) { | ||
return options.snapdragon; | ||
} | ||
// create a new instance | ||
return new Snapdragon(options); | ||
} | ||
/** | ||
* Memoize a generated regex or function. A unique key | ||
* from the `type` (usually method name), the `pattern`, and | ||
* user-defined options. | ||
*/ | ||
function memoize(type, pattern, options, fn) { | ||
var key = utils.createKey(type + pattern, options); | ||
var key = utils.createKey(type + '=' + pattern, options); | ||
if (options && options.cache === false) { | ||
return fn(pattern, options); | ||
} | ||
if (cache.has(type, key)) { | ||
@@ -485,6 +577,2 @@ return cache.get(type, key); | ||
var val = fn(pattern, options); | ||
if (options && options.cache === false) { | ||
return val; | ||
} | ||
cache.set(type, key, val); | ||
@@ -491,0 +579,0 @@ return val; |
@@ -7,3 +7,3 @@ 'use strict'; | ||
module.exports = function(nanomatch) { | ||
module.exports = function(nanomatch, options) { | ||
var star = '[^\\\\/]*?'; | ||
@@ -59,2 +59,4 @@ | ||
.set('slash', function(node, nodes, i) { | ||
if (!this.output) this.output = '(?=\\/)'; | ||
// word boundary | ||
@@ -246,3 +248,11 @@ if (node.rest.slice(0, 2) === '\\b') { | ||
}); | ||
/** | ||
* Allow custom compilers to be passed on options | ||
*/ | ||
if (options && typeof options.compilers === 'function') { | ||
options.compilers(nanomatch.compiler); | ||
} | ||
}; | ||
@@ -20,3 +20,3 @@ 'use strict'; | ||
module.exports = function(nanomatch) { | ||
module.exports = function(nanomatch, options) { | ||
nanomatch.state = nanomatch.state || {}; | ||
@@ -271,2 +271,9 @@ nanomatch.parser | ||
/** | ||
* Allow custom parsers to be passed on options | ||
*/ | ||
if (options && typeof options.parsers === 'function') { | ||
options.parsers(nanomatch.parser); | ||
} | ||
}; | ||
@@ -273,0 +280,0 @@ |
{ | ||
"name": "nanomatch", | ||
"description": "Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but complete Bash 4.3 wildcard support only (no support for exglobs, posix brackets or braces)", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/jonschlinkert/nanomatch", | ||
@@ -27,3 +27,2 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"array-unique": "^0.3.2", | ||
"debug": "^2.2.0", | ||
"define-property": "^0.2.5", | ||
@@ -41,3 +40,3 @@ "extend-shallow": "^2.0.1", | ||
"devDependencies": { | ||
"bash-match": "^0.1.1", | ||
"bash-match": "^0.2.0", | ||
"for-own": "^0.1.4", | ||
@@ -51,2 +50,3 @@ "gulp": "^3.9.1", | ||
"helper-changelog": "^0.3.0", | ||
"is-windows": "^0.2.0", | ||
"minimatch": "^3.0.3", | ||
@@ -53,0 +53,0 @@ "mocha": "^3.1.0", |
@@ -72,3 +72,3 @@ # nanomatch [![NPM version](https://img.shields.io/npm/v/nanomatch.svg?style=flat)](https://www.npmjs.com/package/nanomatch) [![NPM monthly downloads](https://img.shields.io/npm/dm/nanomatch.svg?style=flat)](https://npmjs.org/package/nanomatch) [![NPM total downloads](https://img.shields.io/npm/dt/nanomatch.svg?style=flat)](https://npmjs.org/package/nanomatch) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/nanomatch.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/nanomatch) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/nanomatch.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/nanomatch) | ||
### [nanomatch](index.js#L38) | ||
### [nanomatch](index.js#L37) | ||
@@ -92,3 +92,3 @@ The main function takes a list of strings and one or more glob patterns to use for matching. | ||
### [.match](index.js#L106) | ||
### [.match](index.js#L103) | ||
@@ -112,3 +112,3 @@ Similar to the main function, but `pattern` must be a string. | ||
### [.isMatch](index.js#L168) | ||
### [.isMatch](index.js#L165) | ||
@@ -134,3 +134,3 @@ Returns true if the specified `string` matches the given glob `pattern`. | ||
### [.not](index.js#L195) | ||
### [.not](index.js#L192) | ||
@@ -154,3 +154,3 @@ Returns a list of strings that do _not_ match any of the given `patterns`. | ||
### [.any](index.js#L230) | ||
### [.any](index.js#L227) | ||
@@ -176,3 +176,3 @@ Returns true if the given `string` matches any of the given glob `patterns`. | ||
### [.contains](index.js#L258) | ||
### [.contains](index.js#L255) | ||
@@ -198,3 +198,3 @@ Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but the pattern can match any part of the string. | ||
### [.matchKeys](index.js#L298) | ||
### [.matchKeys](index.js#L295) | ||
@@ -218,3 +218,3 @@ Filter the keys of the given object with the given `glob` pattern and `options`. Does not attempt to match nested keys. If you need this feature, use [glob-object](https://github.com/jonschlinkert/glob-object) instead. | ||
### [.matcher](index.js#L325) | ||
### [.matcher](index.js#L322) | ||
@@ -241,3 +241,3 @@ Creates a matcher function from the given glob `pattern` and `options`. The returned function takes a string to match as its only argument. | ||
### [.makeRe](index.js#L380) | ||
### [.makeRe](index.js#L377) | ||
@@ -260,3 +260,3 @@ Create a regular expression from the given glob `pattern`. | ||
### [.create](index.js#L445) | ||
### [.create](index.js#L435) | ||
@@ -301,2 +301,65 @@ Parses the given glob `pattern` and returns an object with the compiled `output` and optional source `map`. | ||
### [.parse](index.js#L472) | ||
Parse the given `str` with the given `options`. | ||
**Example** | ||
```js | ||
var nanomatch = require('nanomatch'); | ||
var ast = nanomatch.parse('a/{b,c}/d'); | ||
console.log(ast); | ||
// { type: 'root', | ||
// errors: [], | ||
// input: 'a/{b,c}/d', | ||
// nodes: | ||
// [ { type: 'bos', val: '' }, | ||
// { type: 'text', val: 'a/' }, | ||
// { type: 'brace', | ||
// nodes: | ||
// [ { type: 'brace.open', val: '{' }, | ||
// { type: 'text', val: 'b,c' }, | ||
// { type: 'brace.close', val: '}' } ] }, | ||
// { type: 'text', val: '/d' }, | ||
// { type: 'eos', val: '' } ] } | ||
``` | ||
**Params** | ||
* `str` **{String}** | ||
* `options` **{Object}** | ||
* `returns` **{Object}**: Returns an AST | ||
### [.compile](index.js#L523) | ||
Compile the given `ast` or string with the given `options`. | ||
**Example** | ||
```js | ||
var nanomatch = require('nanomatch'); | ||
var ast = nanomatch.parse('a/{b,c}/d'); | ||
console.log(nanomatch.compile(ast)); | ||
// { options: { source: 'string' }, | ||
// state: {}, | ||
// compilers: | ||
// { eos: [Function], | ||
// noop: [Function], | ||
// bos: [Function], | ||
// brace: [Function], | ||
// 'brace.open': [Function], | ||
// text: [Function], | ||
// 'brace.close': [Function] }, | ||
// output: [ 'a/(b|c)/d' ], | ||
// ast: | ||
// { ... }, | ||
// parsingErrors: [] } | ||
``` | ||
**Params** | ||
* `ast` **{Object|String}** | ||
* `options` **{Object}** | ||
* `returns` **{Object}**: Returns an object that has an `output` property with the compiled string. | ||
## Features | ||
@@ -448,2 +511,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on October 19, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on October 20, 2016._ |
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
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
53452
13
1149
500
14
- Removeddebug@^2.2.0