braces
Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support for the Bash 4.3 braces specification, without sacrificing speed.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install
Install with npm:
Why use braces?
Brace patterns are great for matching ranges. Users (and implementors) shouldn't have to think about whether or not they will break their application (or yours) from accidentally defining an aggressive brace pattern. Braces is the only library that offers a solution to this problem.
Safe(r): Braces isn't vulnerable to DoS attacks like brace-expansion, minimatch and multimatch (a different bug than the other regex DoS bug).
Accurate: complete support for the Bash 4.3 Brace Expansion specification (passes all of the Bash braces tests)
fast and performant: Starts fast, runs fast and scales well as patterns increase in complexity.
Organized code base: with parser and compiler that are eas(y|ier) to maintain and update when edge cases crop up.
Well-tested: thousands of test assertions. Passes 100% of the minimatch and brace-expansion unit tests as well (as of the writing of this).
Usage
The main export is a function that takes one or more brace patterns
and options
.
By default, braces returns an optimized regex-source string. To get an array of brace patterns, use brace.expand()
.
The following section explains the difference in more detail. (If you're curious about "why" braces does this by default, see brace matching pitfalls.
Optimized vs. expanded braces
Optimized
By default, patterns are optimized for regex and matching:
Expanded
To expand patterns the same way as Bash or minimatch, use the .expand method:
Or use options.expand:
Features
lists: Supports "lists":
a/{b,c}/d
=>['a/b/d', 'a/c/d']
sequences: Supports alphabetical or numerical "sequences" (ranges):
{1..3}
=>['1', '2', '3']
steps: Supports "steps" or increments:
{2..10..2}
=>['2', '4', '6', '8', '10']
Lists
Uses fill-range for expanding alphabetical or numeric lists:
Sequences
Uses fill-range for expanding alphabetical or numeric ranges (bash "sequences"):
Steps
Steps, or increments, may be used with ranges:
When the .optimize method is used, or options.optimize is set to true, sequences are passed to to-regex-range for expansion.
Nesting
Brace patterns may be nested. The results of each expanded string are not sorted, and left to right order is preserved.
"Expanded" braces
"Optimized" braces
Escaping
Escaping braces
A brace pattern will not be expanded or evaluted if either the opening or closing brace is escaped:
Escaping commas
Commas inside braces may also be escaped:
Single items
Following bash conventions, a brace pattern is also not expanded when it contains a single character:
Options
options.maxLength
Type: Number
Default: 65,536
Description: Limit the length of the input string. Useful when the input string is generated or your application allows users to pass a string, et cetera.
options.expand
Type: Boolean
Default: undefined
Description: Generate an "expanded" brace pattern (this option is unncessary with the .expand
method, which does the same thing).
options.optimize
Type: Boolean
Default: true
Description: Enabled by default.
options.nodupes
Type: Boolean
Default: true
Description: Duplicates are removed by default. To keep duplicates, pass {nodupes: false}
on the options
options.rangeLimit
Type: Number
Default: 250
Description: When braces.expand()
is used, or options.expand
is true, brace patterns will automatically be optimized when the difference between the range minimum and range maximum exceeds the rangeLimit
. This is to prevent huge ranges from freezing your application.
You can set this to any number, or change options.rangeLimit
to Inifinity
to disable this altogether.
Examples
options.transform
Type: Function
Default: undefined
Description: Customize range expansion.
options.quantifiers
Type: Boolean
Default: undefined
Description: In regular expressions, quanitifiers can be used to specify how many times a token can be repeated. For example, a{1,3}
will match the letter a
one to three times.
Unfortunately, regex quantifiers happen to share the same syntax as Bash lists
The quantifiers
option tells braces to detect when regex quantifiers are defined in the given pattern, and not to try to expand them as lists.
Examples
options.unescape
Type: Boolean
Default: undefined
Description: Strip backslashes that were used for escaping from the result.
What is "brace expansion"?
Brace expansion is a type of parameter expansion that was made popular by unix shells for generating lists of strings, as well as regex-like matching when used alongside wildcards (globs).
In addition to "expansion", braces are also used for matching. In other words:
brace expansion is for generating new lists
brace matching is for filtering existing lists
Performance
Braces is not only screaming fast, it's also more accurate the other brace expansion libraries.
Better algorithms
Fortunately there is a solution to the "brace bomb" problem: don't expand brace patterns into an array when they're used for matching.
Instead, convert the pattern into an optimized regular expression. This is easier said than done, and braces is the only library that does this currently.
The proof is in the numbers
Minimatch gets exponentially slower as patterns increase in complexity, braces does not. The following results were generated using braces()
and minimatch.braceExpand()
, respectively.
{1..1000000000000000}
41 B
(1ms 15μs)
N/A (freezes)
{1..100000000000000}
40 B
(890μs)
N/A (freezes)
{1..10000000000000}
39 B
(2ms 49μs)
N/A (freezes)
{1..1000000000000}
38 B
(608μs)
N/A (freezes)
{1..100000000000}
37 B
(397μs)
N/A (freezes)
{1..10000000000}
35 B
(983μs)
N/A (freezes)
{1..1000000000}
34 B
(798μs)
N/A (freezes)
{1..100000000}
33 B
(733μs)
N/A (freezes)
{1..10000000}
32 B
(5ms 632μs)
78.89 MB
(16s 388ms 569μs)
{1..1000000}
31 B
(1ms 381μs)
6.89 MB
(1s 496ms 887μs)
{1..100000}
30 B
(950μs)
588.89 kB
(146ms 921μs)
{1..10000}
29 B
(1ms 114μs)
48.89 kB
(14ms 187μs)
{1..1000}
28 B
(760μs)
3.89 kB
(1ms 453μs)
{1..100}
22 B
(345μs)
291 B
(196μs)
{1..10}
10 B
(533μs)
20 B
(37μs)
{1..3}
7 B
(190μs)
5 B
(27μs)
Faster algorithms
When you need expansion, braces is still much faster.
(the following results were generated using braces.expand()
and minimatch.braceExpand()
, respectively)
{1..10000000}
78.89 MB
(2s 698ms 642μs)
78.89 MB
(18s 601ms 974μs)
{1..1000000}
6.89 MB
(458ms 576μs)
6.89 MB
(1s 491ms 621μs)
{1..100000}
588.89 kB
(20ms 728μs)
588.89 kB
(156ms 919μs)
{1..10000}
48.89 kB
(2ms 202μs)
48.89 kB
(13ms 641μs)
{1..1000}
3.89 kB
(1ms 796μs)
3.89 kB
(1ms 958μs)
{1..100}
291 B
(424μs)
291 B
(211μs)
{1..10}
20 B
(487μs)
20 B
(72μs)
{1..3}
5 B
(166μs)
5 B
(27μs)
If you'd like to run these comparisons yourself, see test/support/generate.js.
Benchmarks
Running benchmarks
Install dev dependencies:
Latest results
About
Related projects
You might also be interested in these projects:
expand-brackets: Expand POSIX bracket expressions (character classes) in glob patterns. | homepage
fill-range: Fill in a range of numbers or letters, optionally passing an increment or
step
to… more | homepagemicromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | homepage
Contributors
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on February 17, 2018.
this is the largest safe integer allowed in JavaScript. ↩
Last updated