fill-range
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
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install
Install with npm:
$ npm install --save fill-range
Usage
Expands numbers and letters, optionally using a step
as the last argument. (Numbers may be defined as JavaScript numbers or strings).
const fill = require('fill-range');
console.log(fill('1', '10'));
console.log(fill('1', '10', { toRegex: true }));
Params
from
: {String|Number} the number or letter to start withto
: {String|Number} the number or letter to end withstep
: {String|Number|Object|Function} Optionally pass a step to use.options
: {Object|Function}: See all available options
Examples
By default, an array of values is returned.
Alphabetical ranges
console.log(fill('a', 'e'));
console.log(fill('A', 'E'));
Numerical ranges
Numbers can be defined as actual numbers or strings.
console.log(fill(1, 5));
console.log(fill('1', '5'));
Negative ranges
Numbers can be defined as actual numbers or strings.
console.log(fill('-5', '-1'));
console.log(fill('-5', '5'));
Steps (increments)
console.log(fill('0', '25', 4));
console.log(fill('0', '25', 5));
console.log(fill('0', '25', 6));
console.log(fill('a', 'z', 4));
console.log(fill('a', 'z', 5));
console.log(fill('a', 'z', 6));
Options
options.step
Type: number
(formatted as a string or number)
Default: undefined
Description: The increment to use for the range. Can be used with letters or numbers.
Example(s)
console.log(fill('1', '10', 2));
console.log(fill('1', '10', 3));
console.log(fill('1', '10', 4));
console.log(fill('a', 'z', 5));
console.log(fill('a', 'z', 7));
console.log(fill('a', 'z', 9));
options.strictRanges
Type: boolean
Default: false
Description: By default, null
is returned when an invalid range is passed. Enable this option to throw a RangeError
on invalid ranges.
Example(s)
The following are all invalid:
fill('1.1', '2');
fill('a', '2');
fill(1, 10, 'foo');
options.stringify
Type: boolean
Default: undefined
Description: Cast all returned values to strings. By default, integers are returned as numbers.
Example(s)
console.log(fill(1, 5));
console.log(fill(1, 5, { stringify: true }));
options.toRegex
Type: boolean
Default: undefined
Description: Create a regex-compatible source string, instead of expanding values to an array.
Example(s)
console.log(fill('a', 'e', { toRegex: true }));
console.log(fill('a', 'z', 3, { toRegex: true }));
console.log(fill('1', '100', { toRegex: true }));
console.log(fill('000001', '100000', { toRegex: true }));
options.transform
Type: function
Default: undefined
Description: Customize each value in the returned array (or string). (you can also pass this function as the last argument to fill()
).
Example(s)
console.log(fill(1, 5, value => String(value).padStart(4, '0')));
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
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:
$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb
Contributors
Author
Jon Schlinkert
Please consider supporting me on Patreon, or start your own Patreon page!
License
Copyright © 2019, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on April 08, 2019.