1 |
3a515b92
|
cagy
|
# 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 |
|
|
|
3 |
|
|
> 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`
|
4 |
|
|
|
5 |
|
|
## Table of Contents
|
6 |
|
|
|
7 |
|
|
- [Install](#install)
|
8 |
|
|
- [Usage](#usage)
|
9 |
|
|
- [Examples](#examples)
|
10 |
|
|
- [Options](#options)
|
11 |
|
|
* [options.step](#optionsstep)
|
12 |
|
|
* [options.strictRanges](#optionsstrictranges)
|
13 |
|
|
* [options.stringify](#optionsstringify)
|
14 |
|
|
* [options.toRegex](#optionstoregex)
|
15 |
|
|
* [options.transform](#optionstransform)
|
16 |
|
|
- [About](#about)
|
17 |
|
|
|
18 |
|
|
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
|
19 |
|
|
|
20 |
|
|
## Install
|
21 |
|
|
|
22 |
|
|
Install with [npm](https://www.npmjs.com/):
|
23 |
|
|
|
24 |
|
|
```sh
|
25 |
|
|
$ npm install --save fill-range
|
26 |
|
|
```
|
27 |
|
|
|
28 |
|
|
Install with [yarn](https://yarnpkg.com):
|
29 |
|
|
|
30 |
|
|
```sh
|
31 |
|
|
$ yarn add fill-range
|
32 |
|
|
```
|
33 |
|
|
|
34 |
|
|
## Usage
|
35 |
|
|
|
36 |
|
|
Expands numbers and letters, optionally using a `step` as the last argument. _(Numbers may be defined as JavaScript numbers or strings)_.
|
37 |
|
|
|
38 |
|
|
```js
|
39 |
|
|
var fill = require('fill-range');
|
40 |
|
|
fill(from, to[, step, options]);
|
41 |
|
|
|
42 |
|
|
// examples
|
43 |
|
|
console.log(fill('1', '10')); //=> '[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]'
|
44 |
|
|
console.log(fill('1', '10', {toRegex: true})); //=> [1-9]|10
|
45 |
|
|
```
|
46 |
|
|
|
47 |
|
|
**Params**
|
48 |
|
|
|
49 |
|
|
* `from`: **{String|Number}** the number or letter to start with
|
50 |
|
|
* `to`: **{String|Number}** the number or letter to end with
|
51 |
|
|
* `step`: **{String|Number|Object|Function}** Optionally pass a [step](#optionsstep) to use.
|
52 |
|
|
* `options`: **{Object|Function}**: See all available [options](#options)
|
53 |
|
|
|
54 |
|
|
## Examples
|
55 |
|
|
|
56 |
|
|
By default, an array of values is returned.
|
57 |
|
|
|
58 |
|
|
**Alphabetical ranges**
|
59 |
|
|
|
60 |
|
|
```js
|
61 |
|
|
console.log(fill('a', 'e')); //=> ['a', 'b', 'c', 'd', 'e']
|
62 |
|
|
console.log(fill('A', 'E')); //=> [ 'A', 'B', 'C', 'D', 'E' ]
|
63 |
|
|
```
|
64 |
|
|
|
65 |
|
|
**Numerical ranges**
|
66 |
|
|
|
67 |
|
|
Numbers can be defined as actual numbers or strings.
|
68 |
|
|
|
69 |
|
|
```js
|
70 |
|
|
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
|
71 |
|
|
console.log(fill('1', '5')); //=> [ 1, 2, 3, 4, 5 ]
|
72 |
|
|
```
|
73 |
|
|
|
74 |
|
|
**Negative ranges**
|
75 |
|
|
|
76 |
|
|
Numbers can be defined as actual numbers or strings.
|
77 |
|
|
|
78 |
|
|
```js
|
79 |
|
|
console.log(fill('-5', '-1')); //=> [ '-5', '-4', '-3', '-2', '-1' ]
|
80 |
|
|
console.log(fill('-5', '5')); //=> [ '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5' ]
|
81 |
|
|
```
|
82 |
|
|
|
83 |
|
|
**Steps (increments)**
|
84 |
|
|
|
85 |
|
|
```js
|
86 |
|
|
// numerical ranges with increments
|
87 |
|
|
console.log(fill('0', '25', 4)); //=> [ '0', '4', '8', '12', '16', '20', '24' ]
|
88 |
|
|
console.log(fill('0', '25', 5)); //=> [ '0', '5', '10', '15', '20', '25' ]
|
89 |
|
|
console.log(fill('0', '25', 6)); //=> [ '0', '6', '12', '18', '24' ]
|
90 |
|
|
|
91 |
|
|
// alphabetical ranges with increments
|
92 |
|
|
console.log(fill('a', 'z', 4)); //=> [ 'a', 'e', 'i', 'm', 'q', 'u', 'y' ]
|
93 |
|
|
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
|
94 |
|
|
console.log(fill('a', 'z', 6)); //=> [ 'a', 'g', 'm', 's', 'y' ]
|
95 |
|
|
```
|
96 |
|
|
|
97 |
|
|
## Options
|
98 |
|
|
|
99 |
|
|
### options.step
|
100 |
|
|
|
101 |
|
|
**Type**: `number` (formatted as a string or number)
|
102 |
|
|
|
103 |
|
|
**Default**: `undefined`
|
104 |
|
|
|
105 |
|
|
**Description**: The increment to use for the range. Can be used with letters or numbers.
|
106 |
|
|
|
107 |
|
|
**Example(s)**
|
108 |
|
|
|
109 |
|
|
```js
|
110 |
|
|
// numbers
|
111 |
|
|
console.log(fill('1', '10', 2)); //=> [ '1', '3', '5', '7', '9' ]
|
112 |
|
|
console.log(fill('1', '10', 3)); //=> [ '1', '4', '7', '10' ]
|
113 |
|
|
console.log(fill('1', '10', 4)); //=> [ '1', '5', '9' ]
|
114 |
|
|
|
115 |
|
|
// letters
|
116 |
|
|
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
|
117 |
|
|
console.log(fill('a', 'z', 7)); //=> [ 'a', 'h', 'o', 'v' ]
|
118 |
|
|
console.log(fill('a', 'z', 9)); //=> [ 'a', 'j', 's' ]
|
119 |
|
|
```
|
120 |
|
|
|
121 |
|
|
### options.strictRanges
|
122 |
|
|
|
123 |
|
|
**Type**: `boolean`
|
124 |
|
|
|
125 |
|
|
**Default**: `false`
|
126 |
|
|
|
127 |
|
|
**Description**: By default, `null` is returned when an invalid range is passed. Enable this option to throw a `RangeError` on invalid ranges.
|
128 |
|
|
|
129 |
|
|
**Example(s)**
|
130 |
|
|
|
131 |
|
|
The following are all invalid:
|
132 |
|
|
|
133 |
|
|
```js
|
134 |
|
|
fill('1.1', '2'); // decimals not supported in ranges
|
135 |
|
|
fill('a', '2'); // incompatible range values
|
136 |
|
|
fill(1, 10, 'foo'); // invalid "step" argument
|
137 |
|
|
```
|
138 |
|
|
|
139 |
|
|
### options.stringify
|
140 |
|
|
|
141 |
|
|
**Type**: `boolean`
|
142 |
|
|
|
143 |
|
|
**Default**: `undefined`
|
144 |
|
|
|
145 |
|
|
**Description**: Cast all returned values to strings. By default, integers are returned as numbers.
|
146 |
|
|
|
147 |
|
|
**Example(s)**
|
148 |
|
|
|
149 |
|
|
```js
|
150 |
|
|
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
|
151 |
|
|
console.log(fill(1, 5, {stringify: true})); //=> [ '1', '2', '3', '4', '5' ]
|
152 |
|
|
```
|
153 |
|
|
|
154 |
|
|
### options.toRegex
|
155 |
|
|
|
156 |
|
|
**Type**: `boolean`
|
157 |
|
|
|
158 |
|
|
**Default**: `undefined`
|
159 |
|
|
|
160 |
|
|
**Description**: Create a regex-compatible source string, instead of expanding values to an array.
|
161 |
|
|
|
162 |
|
|
**Example(s)**
|
163 |
|
|
|
164 |
|
|
```js
|
165 |
|
|
// alphabetical range
|
166 |
|
|
console.log(fill('a', 'e', {toRegex: true})); //=> '[a-e]'
|
167 |
|
|
// alphabetical with step
|
168 |
|
|
console.log(fill('a', 'z', 3, {toRegex: true})); //=> 'a|d|g|j|m|p|s|v|y'
|
169 |
|
|
// numerical range
|
170 |
|
|
console.log(fill('1', '100', {toRegex: true})); //=> '[1-9]|[1-9][0-9]|100'
|
171 |
|
|
// numerical range with zero padding
|
172 |
|
|
console.log(fill('000001', '100000', {toRegex: true}));
|
173 |
|
|
//=> '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'
|
174 |
|
|
```
|
175 |
|
|
|
176 |
|
|
### options.transform
|
177 |
|
|
|
178 |
|
|
**Type**: `function`
|
179 |
|
|
|
180 |
|
|
**Default**: `undefined`
|
181 |
|
|
|
182 |
|
|
**Description**: Customize each value in the returned array (or [string](#optionstoRegex)). _(you can also pass this function as the last argument to `fill()`)_.
|
183 |
|
|
|
184 |
|
|
**Example(s)**
|
185 |
|
|
|
186 |
|
|
```js
|
187 |
|
|
// increase padding by two
|
188 |
|
|
var arr = fill('01', '05', function(val, a, b, step, idx, arr, options) {
|
189 |
|
|
return repeat('0', (options.maxLength + 2) - val.length) + val;
|
190 |
|
|
});
|
191 |
|
|
|
192 |
|
|
console.log(arr);
|
193 |
|
|
//=> ['0001', '0002', '0003', '0004', '0005']
|
194 |
|
|
```
|
195 |
|
|
|
196 |
|
|
## About
|
197 |
|
|
|
198 |
|
|
### Related projects
|
199 |
|
|
|
200 |
|
|
* [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.")
|
201 |
|
|
* [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.")
|
202 |
|
|
* [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.")
|
203 |
|
|
* [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.")
|
204 |
|
|
|
205 |
|
|
### Contributing
|
206 |
|
|
|
207 |
|
|
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
208 |
|
|
|
209 |
|
|
### Contributors
|
210 |
|
|
|
211 |
|
|
| **Commits** | **Contributor** |
|
212 |
|
|
| --- | --- |
|
213 |
|
|
| 103 | [jonschlinkert](https://github.com/jonschlinkert) |
|
214 |
|
|
| 2 | [paulmillr](https://github.com/paulmillr) |
|
215 |
|
|
| 1 | [edorivai](https://github.com/edorivai) |
|
216 |
|
|
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
|
217 |
|
|
|
218 |
|
|
### Building docs
|
219 |
|
|
|
220 |
|
|
_(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.)_
|
221 |
|
|
|
222 |
|
|
To generate the readme, run the following command:
|
223 |
|
|
|
224 |
|
|
```sh
|
225 |
|
|
$ npm install -g verbose/verb#dev verb-generate-readme && verb
|
226 |
|
|
```
|
227 |
|
|
|
228 |
|
|
### Running tests
|
229 |
|
|
|
230 |
|
|
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:
|
231 |
|
|
|
232 |
|
|
```sh
|
233 |
|
|
$ npm install && npm test
|
234 |
|
|
```
|
235 |
|
|
|
236 |
|
|
### Author
|
237 |
|
|
|
238 |
|
|
**Jon Schlinkert**
|
239 |
|
|
|
240 |
|
|
* [github/jonschlinkert](https://github.com/jonschlinkert)
|
241 |
|
|
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
242 |
|
|
|
243 |
|
|
### License
|
244 |
|
|
|
245 |
|
|
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
246 |
|
|
Released under the [MIT License](LICENSE).
|
247 |
|
|
|
248 |
|
|
***
|
249 |
|
|
|
250 |
|
|
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 23, 2017._
|