You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 `step` to use, or create a regex-compatible range with `options.toRegex`
  3. ## Table of Contents
  4. - [Install](#install)
  5. - [Usage](#usage)
  6. - [Examples](#examples)
  7. - [Options](#options)
  8. * [options.step](#optionsstep)
  9. * [options.strictRanges](#optionsstrictranges)
  10. * [options.stringify](#optionsstringify)
  11. * [options.toRegex](#optionstoregex)
  12. * [options.transform](#optionstransform)
  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. Install with [yarn](https://yarnpkg.com):
  21. ```sh
  22. $ yarn add fill-range
  23. ```
  24. ## Usage
  25. Expands numbers and letters, optionally using a `step` as the last argument. _(Numbers may be defined as JavaScript numbers or strings)_.
  26. ```js
  27. var fill = require('fill-range');
  28. fill(from, to[, step, options]);
  29. // examples
  30. console.log(fill('1', '10')); //=> '[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]'
  31. console.log(fill('1', '10', {toRegex: true})); //=> [1-9]|10
  32. ```
  33. **Params**
  34. * `from`: **{String|Number}** the number or letter to start with
  35. * `to`: **{String|Number}** the number or letter to end with
  36. * `step`: **{String|Number|Object|Function}** Optionally pass a [step](#optionsstep) to use.
  37. * `options`: **{Object|Function}**: See all available [options](#options)
  38. ## Examples
  39. By default, an array of values is returned.
  40. **Alphabetical ranges**
  41. ```js
  42. console.log(fill('a', 'e')); //=> ['a', 'b', 'c', 'd', 'e']
  43. console.log(fill('A', 'E')); //=> [ 'A', 'B', 'C', 'D', 'E' ]
  44. ```
  45. **Numerical ranges**
  46. Numbers can be defined as actual numbers or strings.
  47. ```js
  48. console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
  49. console.log(fill('1', '5')); //=> [ 1, 2, 3, 4, 5 ]
  50. ```
  51. **Negative ranges**
  52. Numbers can be defined as actual numbers or strings.
  53. ```js
  54. console.log(fill('-5', '-1')); //=> [ '-5', '-4', '-3', '-2', '-1' ]
  55. console.log(fill('-5', '5')); //=> [ '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5' ]
  56. ```
  57. **Steps (increments)**
  58. ```js
  59. // numerical ranges with increments
  60. console.log(fill('0', '25', 4)); //=> [ '0', '4', '8', '12', '16', '20', '24' ]
  61. console.log(fill('0', '25', 5)); //=> [ '0', '5', '10', '15', '20', '25' ]
  62. console.log(fill('0', '25', 6)); //=> [ '0', '6', '12', '18', '24' ]
  63. // alphabetical ranges with increments
  64. console.log(fill('a', 'z', 4)); //=> [ 'a', 'e', 'i', 'm', 'q', 'u', 'y' ]
  65. console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
  66. console.log(fill('a', 'z', 6)); //=> [ 'a', 'g', 'm', 's', 'y' ]
  67. ```
  68. ## Options
  69. ### options.step
  70. **Type**: `number` (formatted as a string or number)
  71. **Default**: `undefined`
  72. **Description**: The increment to use for the range. Can be used with letters or numbers.
  73. **Example(s)**
  74. ```js
  75. // numbers
  76. console.log(fill('1', '10', 2)); //=> [ '1', '3', '5', '7', '9' ]
  77. console.log(fill('1', '10', 3)); //=> [ '1', '4', '7', '10' ]
  78. console.log(fill('1', '10', 4)); //=> [ '1', '5', '9' ]
  79. // letters
  80. console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
  81. console.log(fill('a', 'z', 7)); //=> [ 'a', 'h', 'o', 'v' ]
  82. console.log(fill('a', 'z', 9)); //=> [ 'a', 'j', 's' ]
  83. ```
  84. ### options.strictRanges
  85. **Type**: `boolean`
  86. **Default**: `false`
  87. **Description**: By default, `null` is returned when an invalid range is passed. Enable this option to throw a `RangeError` on invalid ranges.
  88. **Example(s)**
  89. The following are all invalid:
  90. ```js
  91. fill('1.1', '2'); // decimals not supported in ranges
  92. fill('a', '2'); // incompatible range values
  93. fill(1, 10, 'foo'); // invalid "step" argument
  94. ```
  95. ### options.stringify
  96. **Type**: `boolean`
  97. **Default**: `undefined`
  98. **Description**: Cast all returned values to strings. By default, integers are returned as numbers.
  99. **Example(s)**
  100. ```js
  101. console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
  102. console.log(fill(1, 5, {stringify: true})); //=> [ '1', '2', '3', '4', '5' ]
  103. ```
  104. ### options.toRegex
  105. **Type**: `boolean`
  106. **Default**: `undefined`
  107. **Description**: Create a regex-compatible source string, instead of expanding values to an array.
  108. **Example(s)**
  109. ```js
  110. // alphabetical range
  111. console.log(fill('a', 'e', {toRegex: true})); //=> '[a-e]'
  112. // alphabetical with step
  113. console.log(fill('a', 'z', 3, {toRegex: true})); //=> 'a|d|g|j|m|p|s|v|y'
  114. // numerical range
  115. console.log(fill('1', '100', {toRegex: true})); //=> '[1-9]|[1-9][0-9]|100'
  116. // numerical range with zero padding
  117. console.log(fill('000001', '100000', {toRegex: true}));
  118. //=> '0{5}[1-9]|0{4}[1-9][0-9]|0{3}[1-9][0-9]{2}|0{2}[1-9][0-9]{3}|0[1-9][0-9]{4}|100000'
  119. ```
  120. ### options.transform
  121. **Type**: `function`
  122. **Default**: `undefined`
  123. **Description**: Customize each value in the returned array (or [string](#optionstoRegex)). _(you can also pass this function as the last argument to `fill()`)_.
  124. **Example(s)**
  125. ```js
  126. // increase padding by two
  127. var arr = fill('01', '05', function(val, a, b, step, idx, arr, options) {
  128. return repeat('0', (options.maxLength + 2) - val.length) + val;
  129. });
  130. console.log(arr);
  131. //=> ['0001', '0002', '0003', '0004', '0005']
  132. ```
  133. ## About
  134. ### Related projects
  135. * [braces](https://www.npmjs.com/package/braces): Fast, comprehensive, bash-like brace expansion implemented in JavaScript. Complete support for the Bash 4.3 braces… [more](https://github.com/jonschlinkert/braces) | [homepage](https://github.com/jonschlinkert/braces "Fast, comprehensive, bash-like brace expansion implemented in JavaScript. Complete support for the Bash 4.3 braces specification, without sacrificing speed.")
  136. * [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://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. See the benchmarks. Used by micromatch.")
  137. * [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 "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
  138. * [to-regex-range](https://www.npmjs.com/package/to-regex-range): Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than… [more](https://github.com/jonschlinkert/to-regex-range) | [homepage](https://github.com/jonschlinkert/to-regex-range "Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.87 million test assertions.")
  139. ### Contributing
  140. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  141. ### Contributors
  142. | **Commits** | **Contributor** |
  143. | --- | --- |
  144. | 103 | [jonschlinkert](https://github.com/jonschlinkert) |
  145. | 2 | [paulmillr](https://github.com/paulmillr) |
  146. | 1 | [edorivai](https://github.com/edorivai) |
  147. | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  148. ### Building docs
  149. _(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.)_
  150. To generate the readme, run the following command:
  151. ```sh
  152. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  153. ```
  154. ### Running tests
  155. 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:
  156. ```sh
  157. $ npm install && npm test
  158. ```
  159. ### Author
  160. **Jon Schlinkert**
  161. * [github/jonschlinkert](https://github.com/jonschlinkert)
  162. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  163. ### License
  164. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  165. Released under the [MIT License](LICENSE).
  166. ***
  167. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 23, 2017._