fill-range
Advanced tools
Comparing version 1.2.0 to 1.3.0
119
index.js
@@ -10,5 +10,6 @@ /*! | ||
var isNumber = require('is-number'); | ||
var randomize = require('randomatic'); | ||
var repeat = require('repeat-string'); | ||
var isNumber = require('is-number'); | ||
var repeatStr = require('repeat-string'); | ||
var repeat = require('repeat-element'); | ||
@@ -45,2 +46,3 @@ /** | ||
var origA = a; | ||
var origB = b; | ||
@@ -51,24 +53,39 @@ b = (b.toString() === '-0') ? 0 : b; | ||
if (typeof step === 'string') { | ||
var match = /\?|>|\|/g.exec(step); | ||
var match = stepRe().exec(step); | ||
var i = match && match.index; | ||
if (match && match[0] === '?') { | ||
if (match && match[0] === '+') { | ||
return repeat(a, b); | ||
} else if (match && match[0] === '?') { | ||
return [randomize(a, b)]; | ||
} | ||
if (match && match[0] === '>') { | ||
} else if (match && match[0] === '>') { | ||
step = step.substr(0, i) + step.substr(i + 1); | ||
expand = true; | ||
} | ||
if (match && match[0] === '|') { | ||
} else if (match && match[0] === '|') { | ||
step = step.substr(0, i) + step.substr(i + 1); | ||
expand = true; | ||
sep = '|'; | ||
} | ||
if (!match && !isNumber(step)) { | ||
throw new TypeError('fill-range: invalid step.'); | ||
} | ||
} | ||
if (!hasEither(a) || !hasEither(b) || hasBoth(a) || hasBoth(b)) { | ||
throw new Error('fill-range: invalid range arguments.'); | ||
} | ||
// validate arguments | ||
validateRange(a, b, step); | ||
var isNumA = isNumber(zeros(a)); | ||
var isNumB = isNumber(zeros(b)); | ||
if ((!isNumA && isNumB) || (isNumA && !isNumB)) { | ||
throw new TypeError('fill-range: first range argument is incompatible with second.'); | ||
} | ||
var num = step && isNumber(step) | ||
@@ -90,3 +107,3 @@ ? Math.abs(step) | ||
// detect padding | ||
var padding = isPadded(origA); | ||
var padding = isPadded(origA, origB); | ||
var res, pad, arr = []; | ||
@@ -132,14 +149,48 @@ var ii = 0; | ||
/** | ||
* Join `arr` with the given `sep` | ||
* Step regex | ||
*/ | ||
function join(arr, sep) { | ||
var res = arr.join(sep); | ||
if (sep === '|') { | ||
res = '(' + res + ')'; | ||
function stepRe() { | ||
return /\?|>|\||\+/g; | ||
} | ||
/** | ||
* Return true if `val` has either a letter | ||
* or a number | ||
*/ | ||
function hasEither(val) { | ||
return /[a-z0-9]/i.test(val); | ||
} | ||
/** | ||
* Return true if `val` has both a letter and | ||
* a number (invalid) | ||
*/ | ||
function hasBoth(val) { | ||
return /[a-z][0-9]|[0-9][a-z]/i.test(val); | ||
} | ||
/** | ||
* Normalize zeros for checks | ||
*/ | ||
function zeros(val) { | ||
if (/^-*0+$/.test(val)) { | ||
return '0'; | ||
} | ||
return [res]; | ||
return val; | ||
} | ||
/** | ||
* Return true if `val` has leading zeros, | ||
* or a similar valid pattern. | ||
*/ | ||
function hasZeros(val) { | ||
return /[^.]\.|^-*0+[0-9]/.test(val); | ||
} | ||
/** | ||
* Test for padding. Returns the actual padding string | ||
@@ -152,6 +203,9 @@ * or `false` if no padding. | ||
function isPadded(origA) { | ||
if (/[^.]\.|^-*0+[1-9]/.exec(origA)) { | ||
function isPadded(origA, origB) { | ||
if (hasZeros(origA) || hasZeros(origB)) { | ||
var alen = length(origA); | ||
var blen = length(origB); | ||
var len = alen >= blen ? alen : blen; | ||
return function (a) { | ||
return repeat('0', origA.toString().length - a.toString().length); | ||
return repeatStr('0', len - length(a)); | ||
}; | ||
@@ -163,16 +217,19 @@ } | ||
/** | ||
* Handle errors | ||
* Join `arr` with the given `sep` | ||
*/ | ||
function validateRange(a, b, step) { | ||
if (!/[\w\d]/.test(a) || !/[\w\d]/.test(b)) { | ||
throw new Error('fill-range: invalid range arguments.'); | ||
function join(arr, sep) { | ||
var res = arr.join(sep); | ||
if (sep === '|') { | ||
res = '(' + res + ')'; | ||
} | ||
var isNumA = isNumber(a), isNumB = isNumber(b); | ||
if ((!isNumA && isNumB) || (isNumA && !isNumB)) { | ||
throw new TypeError('fill-range: first range argument is incompatible with second.'); | ||
} | ||
if (step && (!isNumber(step) && !/>|\?|\|/.test(step))) { | ||
throw new TypeError('fill-range: invalid step.'); | ||
} | ||
} | ||
return [res]; | ||
} | ||
/** | ||
* Get the string length of `val` | ||
*/ | ||
function length(val) { | ||
return val.toString().length; | ||
} |
{ | ||
"name": "fill-range", | ||
"description": "Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"homepage": "https://github.com/jonschlinkert/fill-range", | ||
@@ -33,2 +33,3 @@ "author": { | ||
"randomatic": "^1.0.1", | ||
"repeat-element": "^1.0.0", | ||
"repeat-string": "^1.2.0" | ||
@@ -58,2 +59,2 @@ }, | ||
] | ||
} | ||
} |
@@ -33,2 +33,3 @@ # fill-range [![NPM version](https://badge.fury.io/js/fill-range.svg)](http://badge.fury.io/js/fill-range) | ||
**Examples** | ||
@@ -81,3 +82,3 @@ | ||
A special character may be passed as the third arg instead of a step increment. e.g: | ||
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. | ||
@@ -88,5 +89,38 @@ ```js | ||
Depending on how fill-range is implemented, the These characters can be pretty useful when brace expansion and similar use case. | ||
**Supported characters** | ||
- `+`: repeat the given string `n` times | ||
- `|`: create a regex-ready string, instead of an array | ||
- `>`: collapse/join values to single array element | ||
- `?`: randomize the given pattern using [randomatic] | ||
#### `+` | ||
Repeat the first argument the number of times passed on the second argument. | ||
**Examples:** | ||
```js | ||
range('a', 3, '+'); | ||
//=> ['a', 'a', 'a'] | ||
range('abc', 2, '+'); | ||
//=> ['abc', 'abc'] | ||
``` | ||
#### `|` | ||
Creates a regex-ready string from the expanded arguments. | ||
**Examples:** | ||
```js | ||
range('a', 'c', '|'); | ||
//=> ['(a|b|c)' | ||
range('a', 'z', '|5'); | ||
//=> ['(a|f|k|p|u|z)' | ||
``` | ||
#### `>` | ||
@@ -116,3 +150,3 @@ | ||
_(all results are obviously examples, since the actual results will be randomized)_ | ||
_(actual results would obviously be randomized)_ | ||
@@ -158,2 +192,4 @@ Generate a 5-character, uppercase, alphabetical string: | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 09, 2015._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on January 11, 2015._ | ||
[randomatic]: https://github.com/jonschlinkert/randomatic |
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
11563
214
190
4
+ Addedrepeat-element@^1.0.0
+ Addedrepeat-element@1.1.4(transitive)