| [81bc7da] | 1 | # brace-expansion
|
|---|
| 2 |
|
|---|
| 3 | [Brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html),
|
|---|
| 4 | as known from sh/bash, in JavaScript.
|
|---|
| 5 |
|
|---|
| 6 | [](https://github.com/juliangruber/brace-expansion/actions/workflows/ci.yml)
|
|---|
| 7 | [](https://www.npmjs.org/package/brace-expansion)
|
|---|
| 8 |
|
|---|
| 9 | ## Example
|
|---|
| 10 |
|
|---|
| 11 | ```js
|
|---|
| 12 | import { expand } from 'brace-expansion'
|
|---|
| 13 |
|
|---|
| 14 | expand('file-{a,b,c}.jpg')
|
|---|
| 15 | // => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
|
|---|
| 16 |
|
|---|
| 17 | expand('-v{,,}')
|
|---|
| 18 | // => ['-v', '-v', '-v']
|
|---|
| 19 |
|
|---|
| 20 | expand('file{0..2}.jpg')
|
|---|
| 21 | // => ['file0.jpg', 'file1.jpg', 'file2.jpg']
|
|---|
| 22 |
|
|---|
| 23 | expand('file-{a..c}.jpg')
|
|---|
| 24 | // => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
|
|---|
| 25 |
|
|---|
| 26 | expand('file{2..0}.jpg')
|
|---|
| 27 | // => ['file2.jpg', 'file1.jpg', 'file0.jpg']
|
|---|
| 28 |
|
|---|
| 29 | expand('file{0..4..2}.jpg')
|
|---|
| 30 | // => ['file0.jpg', 'file2.jpg', 'file4.jpg']
|
|---|
| 31 |
|
|---|
| 32 | expand('file-{a..e..2}.jpg')
|
|---|
| 33 | // => ['file-a.jpg', 'file-c.jpg', 'file-e.jpg']
|
|---|
| 34 |
|
|---|
| 35 | expand('file{00..10..5}.jpg')
|
|---|
| 36 | // => ['file00.jpg', 'file05.jpg', 'file10.jpg']
|
|---|
| 37 |
|
|---|
| 38 | expand('{{A..C},{a..c}}')
|
|---|
| 39 | // => ['A', 'B', 'C', 'a', 'b', 'c']
|
|---|
| 40 |
|
|---|
| 41 | expand('ppp{,config,oe{,conf}}')
|
|---|
| 42 | // => ['ppp', 'pppconfig', 'pppoe', 'pppoeconf']
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | ## API
|
|---|
| 46 |
|
|---|
| 47 | ```js
|
|---|
| 48 | import { expand } from '@isaacs/brace-expansion'
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | ### const expanded = expand(str, [options])
|
|---|
| 52 |
|
|---|
| 53 | Return an array of all possible and valid expansions of `str`. If
|
|---|
| 54 | none are found, `[str]` is returned.
|
|---|
| 55 |
|
|---|
| 56 | The `options` object can provide a `max` value to cap the number
|
|---|
| 57 | of expansions allowed. This is limited to `100_000` by default,
|
|---|
| 58 | to prevent DoS attacks.
|
|---|
| 59 |
|
|---|
| 60 | ```js
|
|---|
| 61 | const expansions = expand('{1..100}'.repeat(5), {
|
|---|
| 62 | max: 100,
|
|---|
| 63 | })
|
|---|
| 64 | // expansions.length will be 100, not 100^5
|
|---|
| 65 | ```
|
|---|
| 66 |
|
|---|
| 67 | Valid expansions are:
|
|---|
| 68 |
|
|---|
| 69 | ```js
|
|---|
| 70 | ;/^(.*,)+(.+)?$/
|
|---|
| 71 | // {a,b,...}
|
|---|
| 72 | ```
|
|---|
| 73 |
|
|---|
| 74 | A comma separated list of options, like `{a,b}` or `{a,{b,c}}` or `{,a,}`.
|
|---|
| 75 |
|
|---|
| 76 | ```js
|
|---|
| 77 | ;/^-?\d+\.\.-?\d+(\.\.-?\d+)?$/
|
|---|
| 78 | // {x..y[..incr]}
|
|---|
| 79 | ```
|
|---|
| 80 |
|
|---|
| 81 | A numeric sequence from `x` to `y` inclusive, with optional increment.
|
|---|
| 82 | If `x` or `y` start with a leading `0`, all the numbers will be padded
|
|---|
| 83 | to have equal length. Negative numbers and backwards iteration work too.
|
|---|
| 84 |
|
|---|
| 85 | ```js
|
|---|
| 86 | ;/^-?\d+\.\.-?\d+(\.\.-?\d+)?$/
|
|---|
| 87 | // {x..y[..incr]}
|
|---|
| 88 | ```
|
|---|
| 89 |
|
|---|
| 90 | An alphabetic sequence from `x` to `y` inclusive, with optional increment.
|
|---|
| 91 | `x` and `y` must be exactly one character, and if given, `incr` must be a
|
|---|
| 92 | number.
|
|---|
| 93 |
|
|---|
| 94 | For compatibility reasons, the string `${` is not eligible for brace expansion.
|
|---|