
Security News
CISA Rebuffs Funding Concerns as CVE Foundation Draws Criticism
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
expand-brackets
Advanced tools
Expand POSIX bracket expressions (character classes) in glob patterns.
The expand-brackets npm package is used to expand and parse bracket expressions in strings. It is particularly useful for handling patterns in strings that involve brackets, such as character classes in regular expressions.
Expand Bracket Expressions
This feature allows you to expand bracket expressions into their constituent characters. For example, the expression '[a-c]' is expanded into ['a', 'b', 'c'].
const expandBrackets = require('expand-brackets');
console.log(expandBrackets('[a-c]')); // Output: ['a', 'b', 'c']
Handle Negated Bracket Expressions
This feature handles negated bracket expressions, expanding them while preserving the negation symbol. For example, '[^a-c]' is expanded into ['^', 'a', 'b', 'c'].
const expandBrackets = require('expand-brackets');
console.log(expandBrackets('[^a-c]')); // Output: ['^', 'a', 'b', 'c']
Custom Bracket Expansion
This feature allows for custom expansion of bracket expressions based on options provided. For example, using the option { expand: true } with '[a-c]' results in ['a', 'b', 'c'].
const expandBrackets = require('expand-brackets');
console.log(expandBrackets('[a-c]', { expand: true })); // Output: ['a', 'b', 'c']
The braces package is used for expanding and parsing brace expressions. It offers more comprehensive functionality for handling complex brace patterns, including nested and sequential expansions. Compared to expand-brackets, braces provides a broader range of pattern expansions.
Micromatch is a powerful globbing library that supports advanced matching features, including brace and bracket expansions. It is more versatile than expand-brackets, as it can handle a wide variety of glob patterns and matching rules.
Minimatch is a lightweight library for matching file paths against glob patterns. It includes support for bracket and brace expansions, making it a good alternative to expand-brackets for file path matching and pattern expansion.
Expand POSIX bracket expressions (character classes) in glob patterns.
Install with npm:
$ npm install --save expand-brackets
Install with yarn:
$ yarn add expand-brackets
var brackets = require('expand-brackets');
brackets(string[, options]);
Params
The main export is a function that takes the following parameters:
pattern
{String}: the pattern to convertoptions
{Object}: optionally supply an options objectreturns
{String}: returns a string that can be used to create a regexExample
console.log(brackets('[![:lower:]]'));
//=> '[^a-z]'
Parses the given POSIX character class pattern
and returns a
string that can be used for creating regular expressions for matching.
Params
pattern
{String}options
{Object}returns
{Object}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 matchpattern
{String}: POSIX character class pattern(s)options
{Object}returns
{Array}Example
const brackets = require('expand-brackets');
console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]'));
//=> ['a']
console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]+'));
//=> ['a', 'ab']
Returns true if the specified string
matches the given brackets pattern
.
Params
string
{String}: String to matchpattern
{String}: Poxis patternoptions
{String}returns
{Boolean}Example
const brackets = require('expand-brackets');
console.log(brackets.isMatch('a.a', '[[:alpha:]].[[:alpha:]]'));
//=> true
console.log(brackets.isMatch('1.2', '[[:alpha:]].[[:alpha:]]'));
//=> false
Takes a POSIX character class pattern and returns a matcher function. The returned function takes the string to match as its only argument.
Params
pattern
{String}: Poxis patternoptions
{String}returns
{Boolean}Example
const brackets = require('expand-brackets');
const isMatch = brackets.matcher('[[:lower:]].[[:upper:]]');
console.log(isMatch('a.a'));
//=> false
console.log(isMatch('a.A'));
//=> true
Create a regular expression from the given pattern
.
Params
pattern
{String}: The pattern to convert to regex.options
{Object}returns
{RegExp}Example
const brackets = require('expand-brackets');
const re = brackets.makeRe('[[:alpha:]]');
console.log(re);
//=> /^(?:[a-zA-Z])$/
Parses the given POSIX character class pattern
and returns an object with the compiled output
and optional source map
.
Params
pattern
{String}options
{Object}returns
{Object}Example
const 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: [] }
Generate a source map for the given pattern.
Example
var res = brackets('[:alpha:]', {sourcemap: true});
console.log(res.map);
// { version: 3,
// sources: [ 'brackets' ],
// names: [],
// mappings: 'AAAA,MAAS',
// sourcesContent: [ '[:alpha:]' ] }
The following named POSIX bracket expressions are supported:
[:alnum:]
: Alphanumeric characters (a-zA-Z0-9]
)[:alpha:]
: Alphabetic characters (a-zA-Z]
)[:blank:]
: Space and tab ([ t]
)[:digit:]
: Digits ([0-9]
)[:lower:]
: Lowercase letters ([a-z]
)[:punct:]
: Punctuation and symbols. ([!"#$%&'()*+, -./:;<=>?@ [\]^_``{|}~]
)[:upper:]
: Uppercase letters ([A-Z]
)[:word:]
: Word characters (letters, numbers and underscores) ([A-Za-z0-9_]
)[:xdigit:]
: Hexadecimal digits ([A-Fa-f0-9]
)See posix-character-classes for more details.
Not supported
Breaking changes
expand-brackets
need to also use snapdragon 0.12.Breaking changes
expand-brackets
need to also use snapdragon 0.11.Breaking changes
Added features
.create
method to do what the main function did before v2.0.0In addition to performance and matching improvements, the v0.2.0 refactor adds complete POSIX character class support, with the exception of equivalence classes and POSIX.2 collating symbols which are not relevant to node.js usage.
Added features
source map example
var brackets = require('expand-brackets');
var res = brackets('[:alpha:]');
console.log(res.map);
{ version: 3,
sources: [ 'brackets' ],
names: [],
mappings: 'AAAA,MAAS',
sourcesContent: [ '[:alpha:]' ] }
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
69 | jonschlinkert |
13 | danez |
2 | MartinKolarik |
2 | es128 |
1 | doowb |
1 | eush77 |
1 | mjbvz |
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
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:
$ npm install && npm test
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on April 30, 2018.
FAQs
Expand POSIX bracket expressions (character classes) in glob patterns.
The npm package expand-brackets receives a total of 10,675,628 weekly downloads. As such, expand-brackets popularity was classified as popular.
We found that expand-brackets demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.
Product
Module Reachability filters out unreachable CVEs so you can focus on vulnerabilities that actually matter to your application.