Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

expand-brackets

Package Overview
Dependencies
Maintainers
3
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expand-brackets - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

lib/utils.js

10

changelog.md
## Changelog
### v2.0.0
**Breaking changes**
- The main export now returns the compiled string, instead of the object returned from the compiler
**Added features**
- Adds a `.create` method to do what the main function did before v0.3.0
### v0.2.0

@@ -4,0 +14,0 @@

88

index.js

@@ -20,31 +20,5 @@ 'use strict';

/**
* Parses the given POSIX character class `pattern` and returns an object
* with the compiled `output` and optional source `map`.
* Parses the given POSIX character class `pattern` and returns a
* string that can be used for creating regular expressions for matching.
*
* ```js
* var brackets = require('expand-brackets');
* console.log(brackets('[[:alpha:]]'));
* // { options: { source: 'string' },
* // input: '[[:alpha:]]',
* // state: {},
* // compilers:
* // { eos: [Function],
* // noop: [Function],
* // bos: [Function],
* // not: [Function],
* // escape: [Function],
* // text: [Function],
* // posix: [Function],
* // bracket: [Function],
* // 'bracket.open': [Function],
* // 'bracket.inner': [Function],
* // 'bracket.literal': [Function],
* // 'bracket.close': [Function] },
* // output: '[a-zA-Z]',
* // ast:
* // { type: 'root',
* // errors: [],
* // nodes: [ [Object], [Object], [Object] ] },
* // parsingErrors: [] }
* ```
* @param {String} `pattern`

@@ -58,9 +32,4 @@ * @param {Object} `options`

debug('initializing from <%s>', __filename);
var snapdragon = new Snapdragon(options);
compilers(snapdragon);
parsers(snapdragon);
var ast = snapdragon.parse(pattern, options);
var res = snapdragon.compile(ast, options);
res.input = pattern;
return res;
var res = brackets.create(pattern, options);
return res.output;
}

@@ -179,3 +148,3 @@

brackets.makeRe = function(pattern, options) {
var res = brackets(pattern, options);
var res = brackets.create(pattern, options);
var opts = extend({strictErrors: false}, options);

@@ -186,2 +155,49 @@ return toRegex(res.output, opts);

/**
* Parses the given POSIX character class `pattern` and returns an object
* with the compiled `output` and optional source `map`.
*
* ```js
* var brackets = require('expand-brackets');
* console.log(brackets('[[:alpha:]]'));
* // { options: { source: 'string' },
* // input: '[[:alpha:]]',
* // state: {},
* // compilers:
* // { eos: [Function],
* // noop: [Function],
* // bos: [Function],
* // not: [Function],
* // escape: [Function],
* // text: [Function],
* // posix: [Function],
* // bracket: [Function],
* // 'bracket.open': [Function],
* // 'bracket.inner': [Function],
* // 'bracket.literal': [Function],
* // 'bracket.close': [Function] },
* // output: '[a-zA-Z]',
* // ast:
* // { type: 'root',
* // errors: [],
* // nodes: [ [Object], [Object], [Object] ] },
* // parsingErrors: [] }
* ```
* @param {String} `pattern`
* @param {Object} `options`
* @return {Object}
* @api public
*/
brackets.create = function(pattern, options) {
var snapdragon = new Snapdragon(options);
compilers(snapdragon);
parsers(snapdragon);
var ast = snapdragon.parse(pattern, options);
ast.input = pattern;
var res = snapdragon.compile(ast, options);
res.input = pattern;
return res;
};
/**
* Expose `brackets` constructor, parsers and compilers

@@ -188,0 +204,0 @@ */

@@ -6,28 +6,7 @@ 'use strict';

module.exports = function(brackets) {
brackets.posix = brackets.posix || 0;
var count = 0;
brackets.compiler
/**
* beginning-of-string
*/
.set('bos', function(node) {
return this.emit(node.val, node);
})
.set('noop', function(node) {
return this.emit(node.val, node);
})
/**
* Negation / escaping
*/
.set('not', function(node) {
return this.emit('', node);
})
.set('escape', function(node) {
return this.emit(node.val, node);
})
/**
* Text

@@ -37,3 +16,4 @@ */

.set('text', function(node) {
return this.emit(node.val, node);
var val = node.val.replace(/([{()}])/g, '\\$1');
return this.emit(val, node);
})

@@ -46,5 +26,10 @@

.set('posix', function(node) {
if (node.val === '[::]') {
return this.emit('\\[:\\]', node);
}
var val = posix[node.inner];
if (typeof val === 'undefined') {
val = '[' + node.inner + ']';
} else {
count++;
}

@@ -62,34 +47,29 @@ return this.emit(val, node);

.set('bracket.open', function(node) {
if (node.val === '[[') {
return this.emit('[\\[', node);
}
if (node.val === '[^]') {
return this.emit('[^\\]', node);
}
return this.emit(node.val, node);
})
.set('bracket.inner', function(node) {
if (node.val === '!' && node.insideBracket) {
var val = node.val;
if (val === ']') {
return this.emit('\\]', node);
}
if (val === '^]') {
return this.emit('^\\]', node);
}
if (val === '^') {
return this.emit('^', node);
}
return this.emit(node.val, node);
// add slashes to negated brackets, per spec
if (/\^/.test(val) && !/\\?\//.test(val)) {
val += '\\/';
}
val = val.replace(/\\([1-9])/g, '$1');
return this.emit(val, node);
})
.set('bracket.literal', function(node) {
return this.emit('[\\]]', node);
})
.set('bracket.close', function(node) {
var val = node.val;
if (node.rest === '' && brackets.posix > 1) {
if (node.rest === '' && count > 1) {
val += '+';
}
return this.emit(val, node);
})
/**
* end-of-string
*/
.set('eos', function(node) {
return this.emit(node.val, node);
});
};
'use strict';
var utils = require('./utils');
var define = require('define-property');
var regex = require('regex-not');
var cache;
var cache = {};
/**
* Text regex
*/
var not = createRegex('(\\[(?=.*\\])|\\])+');
/**
* Brackets parsers
*/
module.exports = function(brackets) {
var not = cache || (cache = regex('(?:\\[:|:\\]|\\]|\\[)+', {contains: true, strictClose: false}));
brackets.parser.sets.bracket = brackets.parser.sets.bracket || [];
brackets.parser
brackets.parser
/**
* Text parser
*/
.capture('text', function() {

@@ -24,7 +39,6 @@ if (this.isInside('bracket')) return;

/**
* POSIX character classes: "[:upper:]"
* POSIX character classes: "[[:alpha:][:digits:]]"
*/
.capture('posix', function() {
var parsed = this.parsed;
var pos = this.position();

@@ -39,3 +53,3 @@ var m = this.match(/^\[:(.*?):\]/);

var node = pos({
return pos({
type: 'posix',

@@ -46,17 +60,44 @@ insideBracket: inside,

});
})
define(node, 'rest', this.input);
define(node, 'parsed', parsed);
return node;
/**
* Open
*/
.capture('bracket.open', function() {
var pos = this.position();
var m = this.match(/^\[(?=.*\])/);
if (!m) return;
var open = pos({
type: 'bracket.open',
val: m[0]
});
var prev = this.prev();
var last = utils.last(prev.nodes);
if (last.type === 'bracket.open') {
open.type = 'bracket.inner';
define(open, 'parent', prev);
prev.nodes.push(open);
return;
}
var node = pos({
type: 'bracket',
nodes: [open]
});
define(node, 'parent', prev);
define(open, 'parent', node);
this.push('bracket', node);
prev.nodes.push(node);
})
/**
* Non-posix brackets
* Inner
*/
.capturePair('bracket', /^(\[\^\]|\[\[(?!:)|\[(?![:\]]))/, /^\]/)
.capture('bracket.literal', /^\[\]\-?\]/)
.capture('bracket.inner', function() {
if (!this.isInside('bracket')) return;
var parsed = this.parsed;
var pos = this.position();

@@ -66,12 +107,75 @@ var m = this.match(not);

var node = pos({
var next = this.input.charAt(0);
var val = m[0];
if ((val === '^' || val === '!') && next === ']') {
val += this.input[0];
this.consume(1);
}
if (val.slice(-1) === '\\') {
val += this.input[0];
this.consume(1);
}
if (val.charAt(0) === '!') {
val = '^' + val.slice(1);
}
return pos({
type: 'bracket.inner',
insideBracket: true,
val: val
});
})
/**
* Close
*/
.capture('bracket.close', function() {
var pos = this.position();
var m = this.match(/^\]/);
if (!m) return;
var prev = this.prev();
var last = utils.last(prev.nodes);
var node = pos({
type: 'bracket.close',
rest: this.input,
val: m[0]
});
define(node, 'rest', this.input);
define(node, 'parsed', parsed);
return node;
if (last.type === 'bracket.open') {
node.type = 'bracket.inner';
define(node, 'parent', prev);
prev.nodes.push(node);
return;
}
var bracket = this.pop('bracket');
if (!this.isType(bracket, 'bracket')) {
if (this.options.strict) {
throw new Error('missing opening "["');
}
node.type = 'bracket.inner';
node.escaped = true;
return node;
}
bracket.nodes.push(node);
define(node, 'parent', bracket);
});
};
/**
* Create the regex to use for matching text
*/
function createRegex(str, or) {
if (cache.hasOwnProperty(str)) {
return cache[str];
}
var opts = {contains: true, strictClose: false};
var re = regex(str, opts);
cache[str] = re;
return re;
}
{
"name": "expand-brackets",
"description": "Expand POSIX bracket expressions (character classes) in glob patterns.",
"version": "1.0.0",
"version": "2.0.0",
"homepage": "https://github.com/jonschlinkert/expand-brackets",

@@ -34,5 +34,5 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"posix-character-classes": "^0.1.0",
"regex-not": "^0.1.2",
"regex-not": "^1.0.0",
"snapdragon": "^0.7.0",
"to-regex": "^0.1.1"
"to-regex": "^2.1.0"
},

@@ -84,2 +84,2 @@ "devDependencies": {

}
}
}

@@ -37,5 +37,6 @@ # expand-brackets [![NPM version](https://img.shields.io/npm/v/expand-brackets.svg?style=flat)](https://www.npmjs.com/package/expand-brackets) [![NPM downloads](https://img.shields.io/npm/dm/expand-brackets.svg?style=flat)](https://npmjs.org/package/expand-brackets) [![Build Status](https://img.shields.io/travis/jonschlinkert/expand-brackets.svg?style=flat)](https://travis-ci.org/jonschlinkert/expand-brackets)

### [brackets](index.js#L55)
### [brackets](index.js#L29)
Parses the given POSIX character class `pattern` and returns an object with the compiled `output` and optional source `map`.
Parses the given POSIX character class `pattern` and returns a
string that can be used for creating regular expressions for matching.

@@ -48,42 +49,6 @@ **Params**

**Example**
### [.match](index.js#L54)
```js
var brackets = require('expand-brackets');
console.log(brackets('[[:alpha:]]'));
// { options: { source: 'string' },
// input: '[[:alpha:]]',
// state: {},
// compilers:
// { eos: [Function],
// noop: [Function],
// bos: [Function],
// not: [Function],
// escape: [Function],
// text: [Function],
// posix: [Function],
// bracket: [Function],
// 'bracket.open': [Function],
// 'bracket.inner': [Function],
// 'bracket.literal': [Function],
// 'bracket.close': [Function] },
// output: '[a-zA-Z]',
// ast:
// { type: 'root',
// errors: [],
// nodes: [ [Object], [Object], [Object] ] },
// parsingErrors: [] }
```
### [.match](index.js#L85)
Takes an array of strings and a POSIX character class pattern, and returns a new array with only the strings that matched the pattern.
**Params**
* `arr` **{Array}**: Array of strings to match
* `pattern` **{String}**: POSIX character class pattern(s)
* `options` **{Object}**
* `returns` **{Array}**
**Example**

@@ -100,12 +65,12 @@

### [.isMatch](index.js#L131)
**Params**
Returns true if the specified `string` matches the given brackets `pattern`.
* `arr` **{Array}**: Array of strings to match
* `pattern` **{String}**: POSIX character class pattern(s)
* `options` **{Object}**
* `returns` **{Array}**
**Params**
### [.isMatch](index.js#L100)
* `string` **{String}**: String to match
* `pattern` **{String}**: Poxis pattern
* `options` **{String}**
* `returns` **{Boolean}**
Returns true if the specified `string` matches the given brackets `pattern`.

@@ -123,8 +88,5 @@ **Example**

### [.matcher](index.js#L154)
Takes a POSIX character class pattern and returns a matcher function. The returned function takes the string to match as its only argument.
**Params**
* `string` **{String}**: String to match
* `pattern` **{String}**: Poxis pattern

@@ -134,2 +96,6 @@ * `options` **{String}**

### [.matcher](index.js#L123)
Takes a POSIX character class pattern and returns a matcher function. The returned function takes the string to match as its only argument.
**Example**

@@ -147,6 +113,21 @@

### [.makeRe](index.js#L176)
**Params**
* `pattern` **{String}**: Poxis pattern
* `options` **{String}**
* `returns` **{Boolean}**
### [.makeRe](index.js#L145)
Create a regular expression from the given `pattern`.
**Example**
```js
var brackets = require('expand-brackets');
var re = brackets.makeRe('[[:alpha:]]');
console.log(re);
//=> /^(?:[a-zA-Z])$/
```
**Params**

@@ -158,2 +139,6 @@

### [.create](index.js#L187)
Parses the given POSIX character class `pattern` and returns an object with the compiled `output` and optional source `map`.
**Example**

@@ -163,7 +148,33 @@

var brackets = require('expand-brackets');
var re = brackets.makeRe('[[:alpha:]]');
console.log(re);
//=> /^(?:[a-zA-Z])$/
console.log(brackets('[[:alpha:]]'));
// { options: { source: 'string' },
// input: '[[:alpha:]]',
// state: {},
// compilers:
// { eos: [Function],
// noop: [Function],
// bos: [Function],
// not: [Function],
// escape: [Function],
// text: [Function],
// posix: [Function],
// bracket: [Function],
// 'bracket.open': [Function],
// 'bracket.inner': [Function],
// 'bracket.literal': [Function],
// 'bracket.close': [Function] },
// output: '[a-zA-Z]',
// ast:
// { type: 'root',
// errors: [],
// nodes: [ [Object], [Object], [Object] ] },
// parsingErrors: [] }
```
**Params**
* `pattern` **{String}**
* `options` **{Object}**
* `returns` **{Object}**
## Options

@@ -211,2 +222,12 @@

### v2.0.0
**Breaking changes**
* The main export now returns the compiled string, instead of the object returned from the compiler
**Added features**
* Adds a `.create` method to do what the main function did before v0.3.0
### v0.2.0

@@ -253,3 +274,3 @@

| --- | --- |
| 17 | [jonschlinkert](https://github.com/jonschlinkert) |
| 35 | [jonschlinkert](https://github.com/jonschlinkert) |
| 2 | [MartinKolarik](https://github.com/MartinKolarik) |

@@ -291,2 +312,2 @@ | 2 | [es128](https://github.com/es128) |

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.31, on September 27, 2016._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.31, on October 08, 2016._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc