fill-range
Advanced tools
Comparing version 3.1.1 to 4.0.0
33
index.js
@@ -42,3 +42,3 @@ /*! | ||
options = step; | ||
step = null; | ||
step = undefined; | ||
} | ||
@@ -52,3 +52,3 @@ | ||
if (opts.step && !isValidNumber(opts.step)) { | ||
if (opts.strict === true) { | ||
if (opts.strictRanges === true) { | ||
throw new TypeError('expected options.step to be a number'); | ||
@@ -61,3 +61,3 @@ } | ||
if (!opts.isNumber && !isValid(start, stop)) { | ||
if (opts.strict === true) { | ||
if (opts.strictRanges === true) { | ||
throw new RangeError('invalid range arguments: ' + util.inspect([start, stop])); | ||
@@ -91,3 +91,3 @@ } | ||
if (options.toRegex && step === 1) { | ||
return toRange(a, b, options); | ||
return toRange(a, b, start, stop, options); | ||
} | ||
@@ -130,3 +130,3 @@ | ||
if (options.toRegex === true) { | ||
return toSequence(arr, zero); | ||
return toSequence(arr, zero, options); | ||
} | ||
@@ -136,6 +136,11 @@ return arr; | ||
function toRange(a, b, options) { | ||
function toRange(a, b, start, stop, options) { | ||
if (options.isPadded) { | ||
return toRegex(start, stop, options); | ||
} | ||
if (options.isNumber) { | ||
return toRegex(Math.min(a, b), Math.max(a, b)); | ||
return toRegex(Math.min(a, b), Math.max(a, b), options); | ||
} | ||
var start = String.fromCharCode(Math.min(a, b)); | ||
@@ -146,3 +151,3 @@ var stop = String.fromCharCode(Math.max(a, b)); | ||
function toSequence(arr, zeros) { | ||
function toSequence(arr, zeros, options) { | ||
var greater = '', lesser = ''; | ||
@@ -155,6 +160,10 @@ if (zeros.greater.length) { | ||
} | ||
if (greater && lesser) { | ||
return greater + '|' + lesser; | ||
var res = greater && lesser | ||
? greater + '|' + lesser | ||
: greater || lesser; | ||
if (options.capture) { | ||
return '(' + res + ')'; | ||
} | ||
return greater || lesser; | ||
return res; | ||
} | ||
@@ -186,3 +195,3 @@ | ||
function isPadded(str) { | ||
return /^-*0\d+/.test(str); | ||
return /^-?0\d/.test(str); | ||
} | ||
@@ -189,0 +198,0 @@ |
{ | ||
"name": "fill-range", | ||
"description": "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`", | ||
"version": "3.1.1", | ||
"version": "4.0.0", | ||
"homepage": "https://github.com/jonschlinkert/fill-range", | ||
@@ -9,3 +9,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"<wtgtybhertgeghgtwtg@gmail.com> (https://github.com/wtgtybhertgeghgtwtg)", | ||
"Edo Rivai <edo.rivai@gmail.com> (ilumbo.org)", | ||
"Edo Rivai <edo.rivai@gmail.com> (edo.rivai.nl)", | ||
"Jon Schlinkert <jon.schlinkert@sellside.com> (http://twitter.com/jonschlinkert)", | ||
@@ -33,10 +33,10 @@ "Paul Miller <paul+gh@paulmillr.com> (paulmillr.com)" | ||
"repeat-string": "^1.6.1", | ||
"to-regex-range": "^1.0.2" | ||
"to-regex-range": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"ansi-cyan": "^0.1.1", | ||
"benchmarked": "^0.2.5", | ||
"gulp-format-md": "^0.1.11", | ||
"mocha": "^3.2.0", | ||
"yargs-parser": "^4.2.1" | ||
"benchmarked": "^1.0.0", | ||
"gulp-format-md": "^0.1.12", | ||
"minimist": "^1.2.0", | ||
"mocha": "^3.2.0" | ||
}, | ||
@@ -82,8 +82,4 @@ "keywords": [ | ||
"reflinks": true | ||
}, | ||
"reflinks": [ | ||
"verb", | ||
"verb-generate-readme" | ||
] | ||
} | ||
} | ||
} |
196
README.md
@@ -9,4 +9,9 @@ # 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) | ||
- [Usage](#usage) | ||
* [Invalid ranges](#invalid-ranges) | ||
* [Custom function](#custom-function) | ||
- [Examples](#examples) | ||
- [Options](#options) | ||
* [options.step](#optionsstep) | ||
* [options.strictRanges](#optionsstrictranges) | ||
* [options.stringify](#optionsstringify) | ||
* [options.toRegex](#optionstoregex) | ||
* [options.transform](#optionstransform) | ||
- [About](#about) | ||
@@ -24,2 +29,8 @@ | ||
Install with [yarn](https://yarnpkg.com): | ||
```sh | ||
$ yarn add fill-range | ||
``` | ||
## Usage | ||
@@ -31,99 +42,148 @@ | ||
var fill = require('fill-range'); | ||
fill(from, to[, step, options]); | ||
console.log(fill('a', 'e')); | ||
//=> ['a', 'b', 'c', 'd', 'e'] | ||
// 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 | ||
``` | ||
console.log(fill(0, 25, 5)); | ||
//=> [ 0, 5, 10, 15, 20, 25 ] | ||
**Params** | ||
console.log(fill('a', 'e', {toRegex: true})); | ||
//=> '[a-e]' | ||
* `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) | ||
console.log(fill('a', 'z', 3, {toRegex: true})); | ||
//=> 'a|d|g|j|m|p|s|v|y' | ||
## Examples | ||
console.log(fill('1', '100', {toRegex: true})); | ||
//=> '[1-9]|[1-9][0-9]|100' | ||
By default, an array of values is returned. | ||
**Alphabetical ranges** | ||
```js | ||
console.log(fill('a', 'e')); //=> ['a', 'b', 'c', 'd', 'e'] | ||
console.log(fill('A', 'E')); //=> [ 'A', 'B', 'C', 'D', 'E' ] | ||
``` | ||
Create regex-compatible ranges (returns a string, which can be used however you need to create a regex): | ||
**Numerical ranges** | ||
Numbers can be defined as actual numbers or strings. | ||
```js | ||
console.log(fill('a', 'e', {toRegex: true})); | ||
//=> '[a-e]' | ||
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ] | ||
console.log(fill('1', '5')); //=> [ 1, 2, 3, 4, 5 ] | ||
``` | ||
console.log(fill('a', 'z', 3, {toRegex: true})); | ||
//=> 'a|d|g|j|m|p|s|v|y' | ||
**Negative ranges** | ||
console.log(fill('1', '100', {toRegex: true})); | ||
//=> '[1-9]|[1-9][0-9]|100' | ||
Numbers can be defined as actual numbers or strings. | ||
```js | ||
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' ] | ||
``` | ||
**Params** | ||
**Steps (increments)** | ||
```js | ||
fill(start, stop, step, options, fn); | ||
// 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' ] | ||
``` | ||
* `start`: **{String|Number}** the number or letter to start with | ||
* `end`: **{String|Number}** the number or letter to end with | ||
* `step`: **{String|Number}** optionally pass the step to use. works for letters or numbers. | ||
* `options`: **{Object}**: | ||
- `toRegex`: return a regex-compatible string (still returned as an array for consistency) | ||
- `step`: pass the step on the options as an alternative to passing it as an argument | ||
- `strict`: `undefined` by default, set to true to throw errors on invalid ranges. | ||
* `fn`: **{Function}** optionally [pass a function](#custom-function) to modify each character. This can also be defined as `options.transform` | ||
## Options | ||
**Examples** | ||
### 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)** | ||
```js | ||
fill(1, 3) | ||
//=> ['1', '2', '3'] | ||
// 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' ] | ||
fill('1', '3') | ||
//=> ['1', '2', '3'] | ||
// 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' ] | ||
``` | ||
fill('0', '-5') | ||
//=> [ '0', '-1', '-2', '-3', '-4', '-5' ] | ||
### options.strictRanges | ||
fill(-9, 9, 3) | ||
//=> [ '-9', '-6', '-3', '0', '3', '6', '9' ]) | ||
**Type**: `boolean` | ||
fill('-1', '-10', '-2') | ||
//=> [ '-1', '-3', '-5', '-7', '-9' ] | ||
**Default**: `false` | ||
fill('1', '10', '2') | ||
//=> [ '1', '3', '5', '7', '9' ] | ||
**Description**: By default, `null` is returned when an invalid range is passed. Enable this option to throw a `RangeError` on invalid ranges. | ||
fill('a', 'e') | ||
//=> ['a', 'b', 'c', 'd', 'e'] | ||
**Example(s)** | ||
fill('a', 'e', 2) | ||
//=> ['a', 'c', 'e'] | ||
The following are all invalid: | ||
fill('A', 'E', 2) | ||
//=> ['A', 'C', 'E'] | ||
```js | ||
fill('1.1', '2'); // decimals not supported in ranges | ||
fill('a', '2'); // incompatible range values | ||
fill(1, 10, 'foo'); // invalid "step" argument | ||
``` | ||
### Invalid ranges | ||
### options.stringify | ||
When an invalid range is passed, `null` is returned. | ||
**Type**: `boolean` | ||
**Default**: `undefined` | ||
**Description**: Cast all returned values to strings. By default, integers are returned as numbers. | ||
**Example(s)** | ||
```js | ||
fill('1.1', '2'); // decimals not supported in ranges | ||
//=> null | ||
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ] | ||
console.log(fill(1, 5, {stringify: true})); //=> [ '1', '2', '3', '4', '5' ] | ||
``` | ||
fill('a', '2'); // unmatched values | ||
//=> null | ||
### options.toRegex | ||
fill(1, 10, 'foo'); // invalid step | ||
//=> null | ||
**Type**: `boolean` | ||
**Default**: `undefined` | ||
**Description**: Create a regex-compatible source string, instead of expanding values to an array. | ||
**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' | ||
``` | ||
If you want errors to be throw, set `options.strict` to true. | ||
### options.transform | ||
### Custom function | ||
**Type**: `function` | ||
Optionally pass a custom function as last argument or on `options.transform`. | ||
**Default**: `undefined` | ||
**Description**: Customize each value in the returned array (or [string](#optionstoRegex)). _(you can also pass this function as the last argument to `fill()`)_. | ||
**Example(s)** | ||
```js | ||
@@ -146,3 +206,3 @@ // increase padding by two | ||
* [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): Returns a regex-compatible range from two numbers, min and max. Validated against more than 1.1… [more](https://github.com/jonschlinkert/to-regex-range) | [homepage](https://github.com/jonschlinkert/to-regex-range "Returns a regex-compatible range from two numbers, min and max. Validated against more than 1.1 million generated unit tests that run in less than 400ms! Useful for creating regular expressions to validate numbers, ranges, years, etc.") | ||
* [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.") | ||
@@ -155,8 +215,8 @@ ### Contributing | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 96 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 2 | [paulmillr](https://github.com/paulmillr) | | ||
| 1 | [edorivai](https://github.com/edorivai) | | ||
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | | ||
| **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) | | ||
@@ -191,6 +251,6 @@ ### Building docs | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
MIT | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 13, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.5.0, on April 23, 2017._ |
16863
174
250
+ Addedto-regex-range@2.1.1(transitive)
- Removedto-regex-range@1.0.2(transitive)
Updatedto-regex-range@^2.1.0