fill-range
Advanced tools
+120
-41
@@ -51,2 +51,4 @@ /*! | ||
| var opts = options || {}; | ||
| var noexpand = opts.noexpand; | ||
| step = step || opts.step; | ||
@@ -110,3 +112,3 @@ | ||
| if (!noAlphaNum(a) || !noAlphaNum(b) || hasBoth(a) || hasBoth(b)) { | ||
| throw new Error('fill-range: invalid range arguments.'); | ||
| throw new RangeError('fill-range: invalid range arguments.'); | ||
| } | ||
@@ -125,11 +127,8 @@ | ||
| var isNum = isNumA; | ||
| var num = step && isNumber(step) && step !== 0 && step !== '0' | ||
| ? Math.abs(+step) | ||
| : 1; | ||
| var num = formatStep(step); | ||
| // is the range alphabetical? or numeric? | ||
| if (isNum) { | ||
| // if numericm coerce to an integer | ||
| a = +a; | ||
| b = +b; | ||
| // if numeric, coerce to an integer | ||
| a = +a; b = +b; | ||
| } else { | ||
@@ -142,3 +141,3 @@ // otherwise, get the charCode to expand alpha ranges | ||
| // is the pattern positive or negative? | ||
| var isNegative = a >= b; | ||
| var isNegative = a > b; | ||
@@ -151,4 +150,8 @@ // detect padding | ||
| // character classes, ranges and logical `or` | ||
| if (regex && !padding && num === 1 && a < b) { | ||
| return wrap([origA, origB], '-'); | ||
| 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); | ||
| } | ||
| return wrap([origA, origB], sep, opts); | ||
| } | ||
@@ -167,15 +170,15 @@ | ||
| } else if (!isNum) { | ||
| res = String.fromCharCode(a); | ||
| if (regex && isInvalidChar(a)) { | ||
| res = null; | ||
| } else { | ||
| res = String.fromCharCode(a); | ||
| } | ||
| // numbers | ||
| } else { | ||
| var result = pad ? pad + a : a; | ||
| if (pad && a.toString()[0] === '-') { | ||
| result = '-' + pad + a.toString().slice(1); | ||
| } | ||
| res = result.toString(); | ||
| res = formatPadding(a, pad); | ||
| } | ||
| // add result to the array | ||
| arr.push(res); | ||
| // add result to the array, filtering any nulled values | ||
| if (res !== null) arr.push(res); | ||
@@ -190,30 +193,30 @@ // increment or decrement | ||
| // handle regex character classes, ranges | ||
| // or logical `or`, now that the array is expanded | ||
| if (sep === '~') { sep = '-'; } | ||
| if (regex) { | ||
| if (a < 0 || b < 0) { return arr; } | ||
| var len = arr.length; | ||
| if (isNegative) { | ||
| return wrap(arr, '|'); | ||
| // now that the array is expanded, we need to handle regex | ||
| // character classes, ranges or logical `or` that wasn't | ||
| // already handled before the loop | ||
| if ((regex || expand) && !noexpand) { | ||
| // make sure the correct separator is used | ||
| if (sep === '|' || sep === '~') { | ||
| sep = detectSeparator(a, b, num, isNum, isNegative); | ||
| } | ||
| if (num === 1 && len === 2) { | ||
| return wrap(arr, '|'); | ||
| } | ||
| if (num > 1 /* step */) { | ||
| return wrap(arr, '|'); | ||
| } | ||
| if (arr.length === 1 || a < 0 || b < 0) { return arr; } | ||
| return wrap(arr, sep, opts); | ||
| } | ||
| return expand ? wrap(arr, sep) : arr; | ||
| return arr; | ||
| } | ||
| /** | ||
| * Step regex | ||
| * Wrap the string with delims based | ||
| * on the given `sep` | ||
| */ | ||
| function wrap(str, sep) { | ||
| str = str.join(sep); | ||
| function wrap(arr, sep, opts) { | ||
| if (sep === '~') { sep = '-'; } | ||
| var str = arr.join(sep); | ||
| if (sep === '|') { | ||
| if (opts.prefix) { | ||
| str = opts.prefix + str; | ||
| } | ||
| str = '(' + str + ')'; | ||
@@ -228,2 +231,78 @@ } | ||
| /** | ||
| * Check for invalid characters | ||
| */ | ||
| function isCharClass(a, b, step, isNum, isNegative) { | ||
| if (isNegative) { return false; } | ||
| if (isNum) { return a <= 9 && b <= 9; } | ||
| if (a < b) { return step === 1; } | ||
| return false; | ||
| } | ||
| /** | ||
| * Detect the correct separator to use | ||
| */ | ||
| function shouldExpand(a, b, num, isNum, regex, padding, opts) { | ||
| if (isNum && (a > 9 || b > 9)) { return false; } | ||
| return regex && !padding && num === 1 && a < b; | ||
| } | ||
| /** | ||
| * Detect the correct separator to use | ||
| */ | ||
| function detectSeparator(a, b, step, isNum, isNegative) { | ||
| var isChar = isCharClass(a, b, step, isNum, isNegative); | ||
| if (!isChar) { | ||
| return '|'; | ||
| } | ||
| return '~'; | ||
| } | ||
| /** | ||
| * Correctly format the step based on type | ||
| */ | ||
| function formatStep(step) { | ||
| return Math.abs(step >> 0) || 1; | ||
| } | ||
| /** | ||
| * Format padding, taking leading `-` into account | ||
| */ | ||
| function formatPadding(ch, pad) { | ||
| var res = pad ? pad + ch : ch; | ||
| if (pad && ch.toString().charAt(0) === '-') { | ||
| res = '-' + pad + ch.toString().substr(1); | ||
| } | ||
| return res.toString(); | ||
| } | ||
| /** | ||
| * Check for invalid characters | ||
| */ | ||
| function isInvalidChar(str) { | ||
| var ch = toStr(str); | ||
| return ch === '\\' | ||
| || ch === '[' | ||
| || ch === ']' | ||
| || ch === '^' | ||
| || ch === '(' | ||
| || ch === ')' | ||
| || ch === '`'; | ||
| } | ||
| /** | ||
| * Convert to a string from a charCode | ||
| */ | ||
| function toStr(ch) { | ||
| return String.fromCharCode(ch); | ||
| } | ||
| /** | ||
| * Step regex | ||
@@ -275,4 +354,4 @@ */ | ||
| /** | ||
| * Test for padding. Returns the actual padding string | ||
| * or `false` if no padding. | ||
| * If the string is padded, returns a curried function with | ||
| * the a cached padding string, or `false` if no padding. | ||
| * | ||
@@ -285,5 +364,5 @@ * @param {*} `origA` String or number. | ||
| if (hasZeros(origA) || hasZeros(origB)) { | ||
| var alen = length(origA); | ||
| var blen = length(origB); | ||
| var alen = length(origA), blen = length(origB); | ||
| var len = alen >= blen ? alen : blen; | ||
| return function (a) { | ||
@@ -290,0 +369,0 @@ return repeatStr('0', len - length(a)); |
+3
-3
| { | ||
| "name": "fill-range", | ||
| "description": "Fill in a range of numbers or letters, optionally passing an increment or multiplier to use.", | ||
| "version": "1.9.0", | ||
| "version": "2.0.0", | ||
| "homepage": "https://github.com/jonschlinkert/fill-range", | ||
@@ -34,5 +34,5 @@ "author": { | ||
| "isobject": "^0.2.0", | ||
| "randomatic": "^1.0.1", | ||
| "randomatic": "^1.1.0", | ||
| "repeat-element": "^1.0.0", | ||
| "repeat-string": "^1.4.0" | ||
| "repeat-string": "^1.5.0" | ||
| }, | ||
@@ -39,0 +39,0 @@ "devDependencies": { |
+45
-5
@@ -15,2 +15,5 @@ # fill-range [](http://badge.fury.io/js/fill-range) | ||
| var range = require('fill-range'); | ||
| range('a', 'e') | ||
| //=> ['a', 'b', 'c', 'd', 'e'] | ||
| ``` | ||
@@ -86,3 +89,3 @@ | ||
| - `|`: create a regex-ready string, instead of an array | ||
| - `>`: collapse/join values to single array element | ||
| - `>`: join values to single array element | ||
| - `?`: randomize the given pattern using [randomatic] | ||
@@ -104,5 +107,5 @@ | ||
| #### `|` | ||
| #### `|` and `~` | ||
| Creates a regex-ready string from the expanded arguments. | ||
| Creates a regex-capable string (either a logical `or` or a character class) from the expanded arguments. | ||
@@ -115,2 +118,5 @@ **Examples:** | ||
| range('a', 'c', '~'); | ||
| //=> ['[a-c]' | ||
| range('a', 'z', '|5'); | ||
@@ -120,6 +126,40 @@ //=> ['(a|f|k|p|u|z)' | ||
| **Automatic separator correction** | ||
| To avoid this error: | ||
| > `Range out of order in character class` | ||
| Fill-range detects invalid sequences and uses the correct syntax. For example: | ||
| **invalid** (regex) | ||
| If you pass these: | ||
| ```js | ||
| range('a', 'z', '~5'); | ||
| // which would result in this | ||
| //=> ['[a-f-k-p-u-z]'] | ||
| range('10', '20', '~'); | ||
| // which would result in this | ||
| //=> ['[10-20]'] | ||
| ``` | ||
| **valid** (regex) | ||
| fill-range corrects them to this: | ||
| ```js | ||
| range('a', 'z', '~5'); | ||
| //=> ['(a|f|k|p|u|z)' | ||
| range('10', '20', '~'); | ||
| //=> ['(10-20)' | ||
| ``` | ||
| #### `>` | ||
| Collapses all values in the returned array to a single value. | ||
| Joins all values in the returned array to a single value. | ||
@@ -203,3 +243,3 @@ **Examples:** | ||
| _This file was generated by [verb](https://github.com/assemble/verb) on January 25, 2015._ | ||
| _This file was generated by [verb](https://github.com/assemble/verb) on January 26, 2015._ | ||
@@ -206,0 +246,0 @@ [randomatic]: https://github.com/jonschlinkert/randomatic |
13867
20.34%301
25.94%244
19.61%Updated
Updated