2018-09-20 02:56:13 +02:00
# 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)
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
> 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`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
## Table of Contents
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
- [Install ](#install )
2016-10-10 03:43:44 +02:00
- [Usage ](#usage )
2018-09-20 02:56:13 +02:00
- [Examples ](#examples )
- [Options ](#options )
* [options.step ](#optionsstep )
* [options.strictRanges ](#optionsstrictranges )
* [options.stringify ](#optionsstringify )
* [options.toRegex ](#optionstoregex )
* [options.transform ](#optionstransform )
- [About ](#about )
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
_(TOC generated by [verb ](https://github.com/verbose/verb ) using [markdown-toc ](https://github.com/jonschlinkert/markdown-toc ))_
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
## Install
Install with [npm ](https://www.npmjs.com/ ):
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```sh
$ npm install --save fill-range
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
Install with [yarn ](https://yarnpkg.com ):
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```sh
$ yarn add fill-range
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
## Usage
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
Expands numbers and letters, optionally using a `step` as the last argument. _(Numbers may be defined as JavaScript numbers or strings)_ .
2016-10-10 03:43:44 +02:00
```js
2018-09-20 02:56:13 +02:00
var fill = require('fill-range');
fill(from, to[, step, options]);
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
// examples
console.log(fill('1', '10')); //=> '[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]'
console.log(fill('1', '10', {toRegex: true})); //=> [1-9]|10
```
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Params**
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
* `from` : ** {String|Number}** the number or letter to start with
* `to` : ** {String|Number}** the number or letter to end with
* `step` : ** {String|Number|Object|Function}** Optionally pass a [step ](#optionsstep ) to use.
* `options` : ** {Object|Function}**: See all available [options ](#options )
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
## Examples
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
By default, an array of values is returned.
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Alphabetical ranges**
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```js
console.log(fill('a', 'e')); //=> ['a', 'b', 'c', 'd', 'e']
console.log(fill('A', 'E')); //=> [ 'A', 'B', 'C', 'D', 'E' ]
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
**Numerical ranges**
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
Numbers can be defined as actual numbers or strings.
2016-10-10 03:43:44 +02:00
```js
2018-09-20 02:56:13 +02:00
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
console.log(fill('1', '5')); //=> [ 1, 2, 3, 4, 5 ]
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
**Negative ranges**
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
Numbers can be defined as actual numbers or strings.
2016-10-10 03:43:44 +02:00
```js
2018-09-20 02:56:13 +02:00
console.log(fill('-5', '-1')); //=> [ '-5', '-4', '-3', '-2', '-1' ]
console.log(fill('-5', '5')); //=> [ '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5' ]
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
**Steps (increments)**
2016-10-10 03:43:44 +02:00
```js
2018-09-20 02:56:13 +02:00
// numerical ranges with increments
console.log(fill('0', '25', 4)); //=> [ '0', '4', '8', '12', '16', '20', '24' ]
console.log(fill('0', '25', 5)); //=> [ '0', '5', '10', '15', '20', '25' ]
console.log(fill('0', '25', 6)); //=> [ '0', '6', '12', '18', '24' ]
// alphabetical ranges with increments
console.log(fill('a', 'z', 4)); //=> [ 'a', 'e', 'i', 'm', 'q', 'u', 'y' ]
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
console.log(fill('a', 'z', 6)); //=> [ 'a', 'g', 'm', 's', 'y' ]
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
## Options
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
### options.step
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Type**: `number` (formatted as a string or number)
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Default**: `undefined`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Description**: The increment to use for the range. Can be used with letters or numbers.
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Example(s)**
2016-10-10 03:43:44 +02:00
```js
2018-09-20 02:56:13 +02:00
// numbers
console.log(fill('1', '10', 2)); //=> [ '1', '3', '5', '7', '9' ]
console.log(fill('1', '10', 3)); //=> [ '1', '4', '7', '10' ]
console.log(fill('1', '10', 4)); //=> [ '1', '5', '9' ]
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
// letters
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
console.log(fill('a', 'z', 7)); //=> [ 'a', 'h', 'o', 'v' ]
console.log(fill('a', 'z', 9)); //=> [ 'a', 'j', 's' ]
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
### options.strictRanges
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Type**: `boolean`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Default**: `false`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Description**: By default, `null` is returned when an invalid range is passed. Enable this option to throw a `RangeError` on invalid ranges.
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Example(s)**
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
The following are all invalid:
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```js
fill('1.1', '2'); // decimals not supported in ranges
fill('a', '2'); // incompatible range values
fill(1, 10, 'foo'); // invalid "step" argument
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
### options.stringify
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Type**: `boolean`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Default**: `undefined`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Description**: Cast all returned values to strings. By default, integers are returned as numbers.
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Example(s)**
2016-10-10 03:43:44 +02:00
```js
2018-09-20 02:56:13 +02:00
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
console.log(fill(1, 5, {stringify: true})); //=> [ '1', '2', '3', '4', '5' ]
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
### options.toRegex
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Type**: `boolean`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Default**: `undefined`
**Description**: Create a regex-compatible source string, instead of expanding values to an array.
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Example(s)**
```js
// alphabetical range
console.log(fill('a', 'e', {toRegex: true})); //=> '[a-e]'
// alphabetical with step
console.log(fill('a', 'z', 3, {toRegex: true})); //=> 'a|d|g|j|m|p|s|v|y'
// numerical range
console.log(fill('1', '100', {toRegex: true})); //=> '[1-9]|[1-9][0-9]|100'
// numerical range with zero padding
console.log(fill('000001', '100000', {toRegex: true}));
//=> '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'
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
### options.transform
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Type**: `function`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Default**: `undefined`
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Description**: Customize each value in the returned array (or [string ](#optionstoRegex )). _(you can also pass this function as the last argument to `fill()`)_ .
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
**Example(s)**
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```js
// increase padding by two
var arr = fill('01', '05', function(val, a, b, step, idx, arr, options) {
return repeat('0', (options.maxLength + 2) - val.length) + val;
});
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
console.log(arr);
//=> ['0001', '0002', '0003', '0004', '0005']
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
## About
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
### Related projects
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
* [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." )
* [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." )
* [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." )
* [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." )
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
### Contributing
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue ](../../issues/new ).
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
### Contributors
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
| **Commits** | **Contributor** |
| --- | --- |
| 103 | [jonschlinkert ](https://github.com/jonschlinkert ) |
| 2 | [paulmillr ](https://github.com/paulmillr ) |
| 1 | [edorivai ](https://github.com/edorivai ) |
| 1 | [wtgtybhertgeghgtwtg ](https://github.com/wtgtybhertgeghgtwtg ) |
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
### Building docs
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
_(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.)_
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
To generate the readme, run the following command:
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```sh
$ npm install -g verbose/verb#dev verb-generate-readme & & verb
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
### Running tests
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
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:
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
```sh
$ npm install & & npm test
2016-10-10 03:43:44 +02:00
```
2018-09-20 02:56:13 +02:00
### Author
2016-10-10 03:43:44 +02:00
**Jon Schlinkert**
2018-09-20 02:56:13 +02:00
* [github/jonschlinkert ](https://github.com/jonschlinkert )
* [twitter/jonschlinkert ](https://twitter.com/jonschlinkert )
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
### License
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
Copyright © 2017, [Jon Schlinkert ](https://github.com/jonschlinkert ).
Released under the [MIT License ](LICENSE ).
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
***
2016-10-10 03:43:44 +02:00
2018-09-20 02:56:13 +02:00
_This file was generated by [verb-generate-readme ](https://github.com/verbose/verb-generate-readme ), v0.5.0, on April 23, 2017._