c444609e844ea5787ad3ca2b1ec24f6efe32977aec9fa705a2eab85fa31257e7681dde8db168ce7a4cd7540ee9a37371c09628a4e5c873cd2368b22d983e2d 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # fill-range [![NPM version](https://img.shields.io/npm/v/fill-range.svg?style=flat)](https://www.npmjs.com/package/fill-range) [![NPM monthly downloads](https://img.shields.io/npm/dm/fill-range.svg?style=flat)](https://npmjs.org/package/fill-range) [![NPM total downloads](https://img.shields.io/npm/dt/fill-range.svg?style=flat)](https://npmjs.org/package/fill-range) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/fill-range.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/fill-range)
  2. > Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. - [Install](#install)
  5. - [Usage](#usage)
  6. * [Invalid ranges](#invalid-ranges)
  7. * [Custom function](#custom-function)
  8. * [Special characters](#special-characters)
  9. + [plus](#plus)
  10. + [pipe and tilde](#pipe-and-tilde)
  11. + [angle bracket](#angle-bracket)
  12. + [question mark](#question-mark)
  13. - [About](#about)
  14. _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
  15. ## Install
  16. Install with [npm](https://www.npmjs.com/):
  17. ```sh
  18. $ npm install --save fill-range
  19. ```
  20. ## Usage
  21. ```js
  22. var range = require('fill-range');
  23. range('a', 'e');
  24. //=> ['a', 'b', 'c', 'd', 'e']
  25. ```
  26. **Params**
  27. ```js
  28. range(start, stop, step, options, fn);
  29. ```
  30. * `start`: **{String|Number}** the number or letter to start with
  31. * `end`: **{String|Number}** the number or letter to end with
  32. * `step`: **{String|Number}** optionally pass the step to use. works for letters or numbers.
  33. * `options`: **{Object}**:
  34. - `makeRe`: return a regex-compatible string (still returned as an array for consistency)
  35. - `step`: pass the step on the options as an alternative to passing it as an argument
  36. - `silent`: `true` by default, set to false to throw errors for invalid ranges.
  37. * `fn`: **{Function}** optionally [pass a function](#custom-function) to modify each character
  38. **Examples**
  39. ```js
  40. range(1, 3)
  41. //=> ['1', '2', '3']
  42. range('1', '3')
  43. //=> ['1', '2', '3']
  44. range('0', '-5')
  45. //=> [ '0', '-1', '-2', '-3', '-4', '-5' ]
  46. range(-9, 9, 3)
  47. //=> [ '-9', '-6', '-3', '0', '3', '6', '9' ])
  48. range('-1', '-10', '-2')
  49. //=> [ '-1', '-3', '-5', '-7', '-9' ]
  50. range('1', '10', '2')
  51. //=> [ '1', '3', '5', '7', '9' ]
  52. range('a', 'e')
  53. //=> ['a', 'b', 'c', 'd', 'e']
  54. range('a', 'e', 2)
  55. //=> ['a', 'c', 'e']
  56. range('A', 'E', 2)
  57. //=> ['A', 'C', 'E']
  58. ```
  59. ### Invalid ranges
  60. When an invalid range is passed, `null` is returned.
  61. ```js
  62. range('1.1', '2');
  63. //=> null
  64. range('a', '2');
  65. //=> null
  66. range(1, 10, 'foo');
  67. //=> null
  68. ```
  69. If you want errors to be throw, pass `silent: false` on the options:
  70. ### Custom function
  71. Optionally pass a custom function as the third or fourth argument:
  72. ```js
  73. range('a', 'e', function (val, isNumber, pad, i) {
  74. if (!isNumber) {
  75. return String.fromCharCode(val) + i;
  76. }
  77. return val;
  78. });
  79. //=> ['a0', 'b1', 'c2', 'd3', 'e4']
  80. ```
  81. ### Special characters
  82. A special character may be passed as the third arg instead of a step increment. These characters can be pretty useful for brace expansion, creating file paths, test fixtures and similar use case.
  83. ```js
  84. range('a', 'z', SPECIAL_CHARACTER_HERE);
  85. ```
  86. **Supported characters**
  87. * `+`: repeat the given string `n` times
  88. * `|`: create a regex-ready string, instead of an array
  89. * `>`: join values to single array element
  90. * `?`: randomize the given pattern using [randomatic]
  91. #### plus
  92. Character: _(`+`)_
  93. Repeat the first argument the number of times passed on the second argument.
  94. **Examples:**
  95. ```js
  96. range('a', 3, '+');
  97. //=> ['a', 'a', 'a']
  98. range('abc', 2, '+');
  99. //=> ['abc', 'abc']
  100. ```
  101. #### pipe and tilde
  102. Characters: _(`|` and `~`)_
  103. Creates a regex-capable string (either a logical `or` or a character class) from the expanded arguments.
  104. **Examples:**
  105. ```js
  106. range('a', 'c', '|');
  107. //=> ['(a|b|c)'
  108. range('a', 'c', '~');
  109. //=> ['[a-c]'
  110. range('a', 'z', '|5');
  111. //=> ['(a|f|k|p|u|z)'
  112. ```
  113. **Automatic separator correction**
  114. To avoid this error:
  115. > `Range out of order in character class`
  116. Fill-range detects invalid sequences and uses the correct syntax. For example:
  117. **invalid** (regex)
  118. If you pass these:
  119. ```js
  120. range('a', 'z', '~5');
  121. // which would result in this
  122. //=> ['[a-f-k-p-u-z]']
  123. range('10', '20', '~');
  124. // which would result in this
  125. //=> ['[10-20]']
  126. ```
  127. **valid** (regex)
  128. fill-range corrects them to this:
  129. ```js
  130. range('a', 'z', '~5');
  131. //=> ['(a|f|k|p|u|z)'
  132. range('10', '20', '~');
  133. //=> ['(10-20)'
  134. ```
  135. #### angle bracket
  136. Character: _(`>`)_
  137. Joins all values in the returned array to a single value.
  138. **Examples:**
  139. ```js
  140. range('a', 'e', '>');
  141. //=> ['abcde']
  142. range('5', '8', '>');
  143. //=> ['5678']
  144. range('2', '20', '2>');
  145. //=> ['2468101214161820']
  146. ```
  147. #### question mark
  148. Character: _(`?`)_
  149. Uses [randomatic] to generate randomized alpha, numeric, or alpha-numeric patterns based on the provided arguments.
  150. **Examples:**
  151. _(actual results would obviously be randomized)_
  152. Generate a 5-character, uppercase, alphabetical string:
  153. ```js
  154. range('A', 5, '?');
  155. //=> ['NSHAK']
  156. ```
  157. Generate a 5-digit random number:
  158. ```js
  159. range('0', 5, '?');
  160. //=> ['36583']
  161. ```
  162. Generate a 10-character alpha-numeric string:
  163. ```js
  164. range('A0', 10, '?');
  165. //=> ['5YJD60VQNN']
  166. ```
  167. See the [randomatic] repo for all available options and or to create issues or feature requests related to randomization.
  168. ## About
  169. <details>
  170. <summary><strong>Contributing</strong></summary>
  171. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  172. </details>
  173. <details>
  174. <summary><strong>Running Tests</strong></summary>
  175. 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:
  176. ```sh
  177. $ npm install && npm test
  178. ```
  179. </details>
  180. <details>
  181. <summary><strong>Building docs</strong></summary>
  182. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  183. To generate the readme, run the following command:
  184. ```sh
  185. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  186. ```
  187. </details>
  188. ### Related projects
  189. You might also be interested in these projects:
  190. * [braces](https://www.npmjs.com/package/braces): Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support… [more](https://github.com/micromatch/braces) | [homepage](https://github.com/micromatch/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.")
  191. * [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used… [more](https://github.com/jonschlinkert/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range "Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used by [micromatch].")
  192. * [is-glob](https://www.npmjs.com/package/is-glob): Returns `true` if the given string looks like a glob pattern or an extglob pattern… [more](https://github.com/jonschlinkert/is-glob) | [homepage](https://github.com/jonschlinkert/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
  193. * [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/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
  194. ### Contributors
  195. | **Commits** | **Contributor** |
  196. | --- | --- |
  197. | 111 | [jonschlinkert](https://github.com/jonschlinkert) |
  198. | 2 | [paulmillr](https://github.com/paulmillr) |
  199. | 1 | [edorivai](https://github.com/edorivai) |
  200. | 1 | [realityking](https://github.com/realityking) |
  201. | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  202. ### Author
  203. **Jon Schlinkert**
  204. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  205. * [GitHub Profile](https://github.com/jonschlinkert)
  206. * [Twitter Profile](https://twitter.com/jonschlinkert)
  207. ### License
  208. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  209. Released under the [MIT License](LICENSE).
  210. ***
  211. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 08, 2018._