fill-range
Advanced tools
Comparing version 2.0.0 to 2.1.0
87
index.js
@@ -52,3 +52,6 @@ /*! | ||
var noexpand = opts.noexpand; | ||
if (typeof opts.silent === 'undefined') { | ||
opts.silent = true; | ||
} | ||
step = step || opts.step; | ||
@@ -62,3 +65,3 @@ | ||
if (opts.makeRe) { | ||
step = step ? step += '~' : step; | ||
step = step ? (step += '~') : step; | ||
expand = true; | ||
@@ -107,9 +110,23 @@ regex = true; | ||
} else if (!isNumber(step)) { | ||
throw new TypeError('fill-range: invalid step.'); | ||
if (!opts.silent) { | ||
throw new TypeError('fill-range: invalid step.'); | ||
} | ||
return null; | ||
} | ||
} | ||
if (/[.&*()[\]^%$#@!]/.test(a) || /[.&*()[\]^%$#@!]/.test(b)) { | ||
if (!opts.silent) { | ||
throw new RangeError('fill-range: invalid range arguments.'); | ||
} | ||
return null; | ||
} | ||
// has neither a letter nor number, or has both letters and numbers | ||
// this needs to be after the step logic | ||
if (!noAlphaNum(a) || !noAlphaNum(b) || hasBoth(a) || hasBoth(b)) { | ||
throw new RangeError('fill-range: invalid range arguments.'); | ||
if (!opts.silent) { | ||
throw new RangeError('fill-range: invalid range arguments.'); | ||
} | ||
return null; | ||
} | ||
@@ -122,3 +139,6 @@ | ||
if ((!isNumA && isNumB) || (isNumA && !isNumB)) { | ||
throw new TypeError('fill-range: first range argument is incompatible with second.'); | ||
if (!opts.silent) { | ||
throw new TypeError('fill-range: first range argument is incompatible with second.'); | ||
} | ||
return null; | ||
} | ||
@@ -141,5 +161,11 @@ | ||
// is the pattern positive or negative? | ||
var isNegative = a > b; | ||
// is the pattern descending? | ||
var isDescending = a > b; | ||
// don't create a character class if the args are < 0 | ||
if (a < 0 || b < 0) { | ||
expand = false; | ||
regex = false; | ||
} | ||
// detect padding | ||
@@ -151,11 +177,13 @@ var padding = isPadded(origA, origB); | ||
// character classes, ranges and logical `or` | ||
if (shouldExpand(a, b, num, isNum, regex, padding, opts)) { | ||
// make sure the correct separator is used | ||
if (sep === '|' || sep === '~') { | ||
sep = detectSeparator(a, b, num, isNum, isNegative); | ||
if (regex) { | ||
if (shouldExpand(a, b, num, isNum, padding, opts)) { | ||
// make sure the correct separator is used | ||
if (sep === '|' || sep === '~') { | ||
sep = detectSeparator(a, b, num, isNum, isDescending); | ||
} | ||
return wrap([origA, origB], sep, opts); | ||
} | ||
return wrap([origA, origB], sep, opts); | ||
} | ||
while (isNegative ? (a >= b) : (a <= b)) { | ||
while (isDescending ? (a >= b) : (a <= b)) { | ||
if (padding && isNum) { | ||
@@ -186,3 +214,3 @@ pad = padding(a); | ||
// increment or decrement | ||
if (isNegative) { | ||
if (isDescending) { | ||
a -= num; | ||
@@ -197,6 +225,6 @@ } else { | ||
// already handled before the loop | ||
if ((regex || expand) && !noexpand) { | ||
if ((regex || expand) && !opts.noexpand) { | ||
// make sure the correct separator is used | ||
if (sep === '|' || sep === '~') { | ||
sep = detectSeparator(a, b, num, isNum, isNegative); | ||
sep = detectSeparator(a, b, num, isNum, isDescending); | ||
} | ||
@@ -211,4 +239,4 @@ if (arr.length === 1 || a < 0 || b < 0) { return arr; } | ||
/** | ||
* Wrap the string with delims based | ||
* on the given `sep` | ||
* Wrap the string with the correct regex | ||
* syntax. | ||
*/ | ||
@@ -219,10 +247,15 @@ | ||
var str = arr.join(sep); | ||
var pre = opts && opts.regexPrefix; | ||
// regex logical `or` | ||
if (sep === '|') { | ||
if (opts.prefix) { | ||
str = opts.prefix + str; | ||
} | ||
str = pre ? pre + str : str; | ||
str = '(' + str + ')'; | ||
} | ||
// regex character class | ||
if (sep === '-') { | ||
str = (pre && pre === '^') | ||
? pre + str | ||
: str; | ||
str = '[' + str + ']'; | ||
@@ -237,4 +270,4 @@ } | ||
function isCharClass(a, b, step, isNum, isNegative) { | ||
if (isNegative) { return false; } | ||
function isCharClass(a, b, step, isNum, isDescending) { | ||
if (isDescending) { return false; } | ||
if (isNum) { return a <= 9 && b <= 9; } | ||
@@ -249,5 +282,5 @@ if (a < b) { return step === 1; } | ||
function shouldExpand(a, b, num, isNum, regex, padding, opts) { | ||
function shouldExpand(a, b, num, isNum, padding, opts) { | ||
if (isNum && (a > 9 || b > 9)) { return false; } | ||
return regex && !padding && num === 1 && a < b; | ||
return !padding && num === 1 && a < b; | ||
} | ||
@@ -259,4 +292,4 @@ | ||
function detectSeparator(a, b, step, isNum, isNegative) { | ||
var isChar = isCharClass(a, b, step, isNum, isNegative); | ||
function detectSeparator(a, b, step, isNum, isDescending) { | ||
var isChar = isCharClass(a, b, step, isNum, isDescending); | ||
if (!isChar) { | ||
@@ -263,0 +296,0 @@ return '|'; |
{ | ||
"name": "fill-range", | ||
"description": "Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"homepage": "https://github.com/jonschlinkert/fill-range", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -16,3 +16,3 @@ # fill-range [![NPM version](https://badge.fury.io/js/fill-range.svg)](http://badge.fury.io/js/fill-range) | ||
range('a', 'e') | ||
range('a', 'e'); | ||
//=> ['a', 'b', 'c', 'd', 'e'] | ||
@@ -24,8 +24,13 @@ ``` | ||
```js | ||
range(start, stop, increment); | ||
range(start, stop, step, options, fn); | ||
``` | ||
- `start`: the number or letter to start with | ||
- `end`: the number or letter to end with | ||
- `step`: optionally pass the increment to use. works for letters or numbers | ||
- `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}**: | ||
+ `makeRe`: 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 | ||
+ `silent`: `true` by default, set to false to throw errors for invalid ranges. | ||
- `fn`: **{Function}** optionally [pass a function](#custom-function) to modify each character | ||
@@ -64,2 +69,20 @@ | ||
### Invalid ranges | ||
When an invalid range is passed, `null` is returned. | ||
```js | ||
range('1.1', '2'); | ||
//=> null | ||
range('a', '2'); | ||
//=> null | ||
range(1, 10, 'foo'); | ||
//=> null | ||
``` | ||
If you want errors to be throw, pass `silent: false` on the options: | ||
### Custom function | ||
@@ -241,3 +264,3 @@ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 26, 2015._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 30, 2015._ | ||
@@ -248,2 +271,2 @@ [randomatic]: https://github.com/jonschlinkert/randomatic | ||
[micromatch]: https://github.com/jonschlinkert/micromatch | ||
[braces]: https://github.com/jonschlinkert/braces | ||
[braces]: https://github.com/jonschlinkert/braces |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15264
330
268