6e8b6560caf23d36206f856aa6ef8e8d06bded0674a8f0cd451e6dbaba4afd1706242d74f99c216ae46e2ed93b4bf2585a569240661b477fca2135be642e51 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # braces [![NPM version](https://img.shields.io/npm/v/braces.svg?style=flat)](https://www.npmjs.com/package/braces) [![NPM downloads](https://img.shields.io/npm/dm/braces.svg?style=flat)](https://npmjs.org/package/braces) [![Build Status](https://img.shields.io/travis/jonschlinkert/braces.svg?style=flat)](https://travis-ci.org/jonschlinkert/braces)
  2. Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 braces specification.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install braces --save
  7. ```
  8. ## Features
  9. * Complete support for the braces part of the [Bash 4.3 Brace Expansion](www.gnu.org/software/bash/). Braces passes [all of the relevant unit tests](#bash-4-3-support) from the spec.
  10. * Expands comma-separated values: `a/{b,c}/d` => `['a/b/d', 'a/c/d']`
  11. * Expands alphabetical or numerical ranges: `{1..3}` => `['1', '2', '3']`
  12. * [Very fast](#benchmarks)
  13. * [Special characters](./patterns.md) can be used to generate interesting patterns.
  14. ## Example usage
  15. ```js
  16. var braces = require('braces');
  17. braces('a/{x,y}/c{d}e')
  18. //=> ['a/x/cde', 'a/y/cde']
  19. braces('a/b/c/{x,y}')
  20. //=> ['a/b/c/x', 'a/b/c/y']
  21. braces('a/{x,{1..5},y}/c{d}e')
  22. //=> ['a/x/cde', 'a/1/cde', 'a/y/cde', 'a/2/cde', 'a/3/cde', 'a/4/cde', 'a/5/cde']
  23. ```
  24. ### Use case: fixtures
  25. > Use braces to generate test fixtures!
  26. **Example**
  27. ```js
  28. var braces = require('./');
  29. var path = require('path');
  30. var fs = require('fs');
  31. braces('blah/{a..z}.js').forEach(function(fp) {
  32. if (!fs.existsSync(path.dirname(fp))) {
  33. fs.mkdirSync(path.dirname(fp));
  34. }
  35. fs.writeFileSync(fp, '');
  36. });
  37. ```
  38. See the [tests](./test/test.js) for more examples and use cases (also see the [bash spec tests](./test/bash-mm-adjusted.js));
  39. ### Range expansion
  40. Uses [expand-range](https://github.com/jonschlinkert/expand-range) for range expansion.
  41. ```js
  42. braces('a{1..3}b')
  43. //=> ['a1b', 'a2b', 'a3b']
  44. braces('a{5..8}b')
  45. //=> ['a5b', 'a6b', 'a7b', 'a8b']
  46. braces('a{00..05}b')
  47. //=> ['a00b', 'a01b', 'a02b', 'a03b', 'a04b', 'a05b']
  48. braces('a{01..03}b')
  49. //=> ['a01b', 'a02b', 'a03b']
  50. braces('a{000..005}b')
  51. //=> ['a000b', 'a001b', 'a002b', 'a003b', 'a004b', 'a005b']
  52. braces('a{a..e}b')
  53. //=> ['aab', 'abb', 'acb', 'adb', 'aeb']
  54. braces('a{A..E}b')
  55. //=> ['aAb', 'aBb', 'aCb', 'aDb', 'aEb']
  56. ```
  57. Pass a function as the last argument to customize range expansions:
  58. ```js
  59. var range = braces('x{a..e}y', function (str, i) {
  60. return String.fromCharCode(str) + i;
  61. });
  62. console.log(range);
  63. //=> ['xa0y', 'xb1y', 'xc2y', 'xd3y', 'xe4y']
  64. ```
  65. See [expand-range](https://github.com/jonschlinkert/expand-range) for benchmarks, tests and the full list of range expansion features.
  66. ## Options
  67. ### options.makeRe
  68. Type: `Boolean`
  69. Deafault: `false`
  70. Return a regex-optimal string. If you're using braces to generate regex, this will result in dramatically faster performance.
  71. **Examples**
  72. With the default settings (`{makeRe: false}`):
  73. ```js
  74. braces('{1..5}');
  75. //=> ['1', '2', '3', '4', '5']
  76. ```
  77. With `{makeRe: true}`:
  78. ```js
  79. braces('{1..5}', {makeRe: true});
  80. //=> ['[1-5]']
  81. braces('{3..9..3}', {makeRe: true});
  82. //=> ['(3|6|9)']
  83. ```
  84. ### options.bash
  85. Type: `Boolean`
  86. Default: `false`
  87. Enables complete support for the Bash specification. The downside is a 20-25% speed decrease.
  88. **Example**
  89. Using the default setting (`{bash: false}`):
  90. ```js
  91. braces('a{b}c');
  92. //=> ['abc']
  93. ```
  94. In bash (and minimatch), braces with one item are not expanded. To get the same result with braces, set `{bash: true}`:
  95. ```js
  96. braces('a{b}c', {bash: true});
  97. //=> ['a{b}c']
  98. ```
  99. ### options.nodupes
  100. Type: `Boolean`
  101. Deafault: `true`
  102. Duplicates are removed by default. To keep duplicates, pass `{nodupes: false}` on the options
  103. ## Bash 4.3 Support
  104. > Better support for Bash 4.3 than minimatch
  105. This project has comprehensive unit tests, including tests coverted from [Bash 4.3](www.gnu.org/software/bash/). Currently only 8 of 102 unit tests fail, and
  106. ## Run benchmarks
  107. Install dev dependencies:
  108. ```bash
  109. npm i -d && npm benchmark
  110. ```
  111. ### Latest results
  112. ```bash
  113. #1: escape.js
  114. brace-expansion.js x 114,934 ops/sec ±1.24% (93 runs sampled)
  115. braces.js x 342,254 ops/sec ±0.84% (90 runs sampled)
  116. #2: exponent.js
  117. brace-expansion.js x 12,359 ops/sec ±0.86% (96 runs sampled)
  118. braces.js x 20,389 ops/sec ±0.71% (97 runs sampled)
  119. #3: multiple.js
  120. brace-expansion.js x 114,469 ops/sec ±1.44% (94 runs sampled)
  121. braces.js x 401,621 ops/sec ±0.87% (91 runs sampled)
  122. #4: nested.js
  123. brace-expansion.js x 102,769 ops/sec ±1.55% (92 runs sampled)
  124. braces.js x 314,088 ops/sec ±0.71% (98 runs sampled)
  125. #5: normal.js
  126. brace-expansion.js x 157,577 ops/sec ±1.65% (91 runs sampled)
  127. braces.js x 1,115,950 ops/sec ±0.74% (94 runs sampled)
  128. #6: range.js
  129. brace-expansion.js x 138,822 ops/sec ±1.71% (91 runs sampled)
  130. braces.js x 1,108,353 ops/sec ±0.85% (94 runs sampled)
  131. ```
  132. ## Related projects
  133. You might also be interested in these projects:
  134. * [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… [more](https://www.npmjs.com/package/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range)
  135. * [fill-range](https://www.npmjs.com/package/fill-range): Fill in a range of numbers or letters, optionally passing an increment or multiplier to… [more](https://www.npmjs.com/package/fill-range) | [homepage](https://github.com/jonschlinkert/fill-range)
  136. * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/jonschlinkert/micromatch)
  137. ## Contributing
  138. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/braces/issues/new).
  139. ## Building docs
  140. Generate readme and API documentation with [verb](https://github.com/verbose/verb):
  141. ```sh
  142. $ npm install verb && npm run docs
  143. ```
  144. Or, if [verb](https://github.com/verbose/verb) is installed globally:
  145. ```sh
  146. $ verb
  147. ```
  148. ## Running tests
  149. Install dev dependencies:
  150. ```sh
  151. $ npm install -d && npm test
  152. ```
  153. ## Author
  154. **Jon Schlinkert**
  155. * [github/jonschlinkert](https://github.com/jonschlinkert)
  156. * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
  157. ## License
  158. Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
  159. Released under the [MIT license](https://github.com/jonschlinkert/braces/blob/master/LICENSE).
  160. ***
  161. _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on May 21, 2016._