UNPKG

4.08 kBMarkdownView Raw
1# brace-expansion
2
3[Brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html),
4as known from sh/bash, in JavaScript.
5
6[![CI](https://github.com/juliangruber/brace-expansion/actions/workflows/ci.yml/badge.svg)](https://github.com/juliangruber/brace-expansion/actions/workflows/ci.yml)
7[![downloads](https://img.shields.io/npm/dm/brace-expansion.svg)](https://www.npmjs.org/package/brace-expansion)
8
9## Example
10
11```js
12import expand from 'brace-expansion'
13
14expand('file-{a,b,c}.jpg')
15// => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
16
17expand('-v{,,}')
18// => ['-v', '-v', '-v']
19
20expand('file{0..2}.jpg')
21// => ['file0.jpg', 'file1.jpg', 'file2.jpg']
22
23expand('file-{a..c}.jpg')
24// => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
25
26expand('file{2..0}.jpg')
27// => ['file2.jpg', 'file1.jpg', 'file0.jpg']
28
29expand('file{0..4..2}.jpg')
30// => ['file0.jpg', 'file2.jpg', 'file4.jpg']
31
32expand('file-{a..e..2}.jpg')
33// => ['file-a.jpg', 'file-c.jpg', 'file-e.jpg']
34
35expand('file{00..10..5}.jpg')
36// => ['file00.jpg', 'file05.jpg', 'file10.jpg']
37
38expand('{{A..C},{a..c}}')
39// => ['A', 'B', 'C', 'a', 'b', 'c']
40
41expand('ppp{,config,oe{,conf}}')
42// => ['ppp', 'pppconfig', 'pppoe', 'pppoeconf']
43```
44
45## API
46
47```js
48import expand from 'brace-expansion'
49```
50
51### const expanded = expand(str)
52
53Return an array of all possible and valid expansions of `str`. If none are
54found, `[str]` is returned.
55
56Valid expansions are:
57
58```js
59/^(.*,)+(.+)?$/
60// {a,b,...}
61```
62
63A comma separated list of options, like `{a,b}` or `{a,{b,c}}` or `{,a,}`.
64
65```js
66/^-?\d+\.\.-?\d+(\.\.-?\d+)?$/
67// {x..y[..incr]}
68```
69
70A numeric sequence from `x` to `y` inclusive, with optional increment.
71If `x` or `y` start with a leading `0`, all the numbers will be padded
72to have equal length. Negative numbers and backwards iteration work too.
73
74```js
75/^-?\d+\.\.-?\d+(\.\.-?\d+)?$/
76// {x..y[..incr]}
77```
78
79An alphabetic sequence from `x` to `y` inclusive, with optional increment.
80`x` and `y` must be exactly one character, and if given, `incr` must be a
81number.
82
83For compatibility reasons, the string `${` is not eligible for brace expansion.
84
85## Installation
86
87With [npm](https://npmjs.org) do:
88
89```bash
90npm install brace-expansion
91```
92
93## Contributors
94
95- [Julian Gruber](https://github.com/juliangruber)
96- [Isaac Z. Schlueter](https://github.com/isaacs)
97- [Haelwenn Monnier](https://github.com/lanodan)
98
99## Sponsors
100
101This module is proudly supported by my [Sponsors](https://github.com/juliangruber/sponsors)!
102
103Do you want to support modules like this to improve their quality, stability and weigh in on new features? Then please consider donating to my [Patreon](https://www.patreon.com/juliangruber). Not sure how much of my modules you're using? Try [feross/thanks](https://github.com/feross/thanks)!
104
105## Security contact information
106
107To report a security vulnerability, please use the
108[Tidelift security contact](https://tidelift.com/security).
109Tidelift will coordinate the fix and disclosure.
110
111## License
112
113(MIT)
114
115Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
116
117Permission is hereby granted, free of charge, to any person obtaining a copy of
118this software and associated documentation files (the "Software"), to deal in
119the Software without restriction, including without limitation the rights to
120use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
121of the Software, and to permit persons to whom the Software is furnished to do
122so, subject to the following conditions:
123
124The above copyright notice and this permission notice shall be included in all
125copies or substantial portions of the Software.
126
127THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
128IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
129FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
130AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
131LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
132OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
133SOFTWARE.