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)
3 > Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.
5 Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
9 * [Invalid ranges](#invalid-ranges)
10 * [Custom function](#custom-function)
11 * [Special characters](#special-characters)
13 + [pipe and tilde](#pipe-and-tilde)
14 + [angle bracket](#angle-bracket)
15 + [question mark](#question-mark)
18 _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
22 Install with [npm](https://www.npmjs.com/):
25 $ npm install --save fill-range
31 var range = require('fill-range');
34 //=> ['a', 'b', 'c', 'd', 'e']
40 range(start, stop, step, options, fn);
43 * `start`: **{String|Number}** the number or letter to start with
44 * `end`: **{String|Number}** the number or letter to end with
45 * `step`: **{String|Number}** optionally pass the step to use. works for letters or numbers.
46 * `options`: **{Object}**:
47 - `makeRe`: return a regex-compatible string (still returned as an array for consistency)
48 - `step`: pass the step on the options as an alternative to passing it as an argument
49 - `silent`: `true` by default, set to false to throw errors for invalid ranges.
50 * `fn`: **{Function}** optionally [pass a function](#custom-function) to modify each character
62 //=> [ '0', '-1', '-2', '-3', '-4', '-5' ]
65 //=> [ '-9', '-6', '-3', '0', '3', '6', '9' ])
67 range('-1', '-10', '-2')
68 //=> [ '-1', '-3', '-5', '-7', '-9' ]
71 //=> [ '1', '3', '5', '7', '9' ]
74 //=> ['a', 'b', 'c', 'd', 'e']
85 When an invalid range is passed, `null` is returned.
98 If you want errors to be throw, pass `silent: false` on the options:
102 Optionally pass a custom function as the third or fourth argument:
105 range('a', 'e', function (val, isNumber, pad, i) {
107 return String.fromCharCode(val) + i;
111 //=> ['a0', 'b1', 'c2', 'd3', 'e4']
114 ### Special characters
116 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.
119 range('a', 'z', SPECIAL_CHARACTER_HERE);
122 **Supported characters**
124 * `+`: repeat the given string `n` times
125 * `|`: create a regex-ready string, instead of an array
126 * `>`: join values to single array element
127 * `?`: randomize the given pattern using [randomatic]
133 Repeat the first argument the number of times passed on the second argument.
141 range('abc', 2, '+');
147 Characters: _(`|` and `~`)_
149 Creates a regex-capable string (either a logical `or` or a character class) from the expanded arguments.
154 range('a', 'c', '|');
157 range('a', 'c', '~');
160 range('a', 'z', '|5');
161 //=> ['(a|f|k|p|u|z)'
164 **Automatic separator correction**
168 > `Range out of order in character class`
170 Fill-range detects invalid sequences and uses the correct syntax. For example:
177 range('a', 'z', '~5');
178 // which would result in this
179 //=> ['[a-f-k-p-u-z]']
181 range('10', '20', '~');
182 // which would result in this
188 fill-range corrects them to this:
191 range('a', 'z', '~5');
192 //=> ['(a|f|k|p|u|z)'
194 range('10', '20', '~');
202 Joins all values in the returned array to a single value.
207 range('a', 'e', '>');
210 range('5', '8', '>');
213 range('2', '20', '2>');
214 //=> ['2468101214161820']
221 Uses [randomatic] to generate randomized alpha, numeric, or alpha-numeric patterns based on the provided arguments.
225 _(actual results would obviously be randomized)_
227 Generate a 5-character, uppercase, alphabetical string:
234 Generate a 5-digit random number:
241 Generate a 10-character alpha-numeric string:
244 range('A0', 10, '?');
248 See the [randomatic] repo for all available options and or to create issues or feature requests related to randomization.
253 <summary><strong>Contributing</strong></summary>
255 Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
260 <summary><strong>Running Tests</strong></summary>
262 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:
265 $ npm install && npm test
271 <summary><strong>Building docs</strong></summary>
273 _(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.)_
275 To generate the readme, run the following command:
278 $ npm install -g verbose/verb#dev verb-generate-readme && verb
285 You might also be interested in these projects:
287 * [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.")
288 * [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].")
289 * [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")
290 * [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.")
294 | **Commits** | **Contributor** |
296 | 111 | [jonschlinkert](https://github.com/jonschlinkert) |
297 | 2 | [paulmillr](https://github.com/paulmillr) |
298 | 1 | [edorivai](https://github.com/edorivai) |
299 | 1 | [realityking](https://github.com/realityking) |
300 | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
306 * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
307 * [GitHub Profile](https://github.com/jonschlinkert)
308 * [Twitter Profile](https://twitter.com/jonschlinkert)
312 Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
313 Released under the [MIT License](LICENSE).
317 _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 08, 2018._