Skip to content

Commit

Permalink
better support for logical or and character classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Jan 26, 2015
1 parent c96918a commit ebb78cc
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ function fillRange(a, b, step, options, fn) {

var expand, regex = false, sep = '';
var opts = options || {};

var noexpand = opts.noexpand;
step = step || opts.step;

// store a ref to unmodified arg
Expand Down Expand Up @@ -119,7 +121,7 @@ function fillRange(a, b, step, options, fn) {

// by this point both are the same, so we
// can use A to check going forward.
var isNum = !!isNumA;
var isNum = isNumA;
var num = formatStep(step);

// is the range alphabetical? or numeric?
Expand All @@ -140,14 +142,15 @@ function fillRange(a, b, step, options, fn) {
var res, pad, arr = [];
var ii = 0;

// make sure the correct separator is used
if (regex && sep === '|' || sep === '~') {
sep = detectSeparator(a, b, num, isNum, isNegative);
}

// character classes, ranges and logical `or`
if (regex && !padding && num === 1 && a < b) {
return wrap([origA, origB], sep);
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, isNegative);
}
return wrap([origA, origB], sep, opts);
}
}

while (isNegative ? (a >= b) : (a <= b)) {
Expand Down Expand Up @@ -186,9 +189,13 @@ function fillRange(a, b, step, options, fn) {
// 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) {
if ((regex || expand) && !noexpand) {
// make sure the correct separator is used
if (sep === '|' || sep === '~') {
sep = detectSeparator(a, b, num, isNum, isNegative);
}
if (arr.length === 1 || a < 0 || b < 0) { return arr; }
return wrap(arr, sep);
return wrap(arr, sep, opts);
}

return arr;
Expand All @@ -199,11 +206,14 @@ function fillRange(a, b, step, options, fn) {
* on the given `sep`
*/

function wrap(arr, 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 + ')';
}
if (sep === '-') {
Expand All @@ -218,8 +228,18 @@ function wrap(arr, sep) {

function isCharClass(a, b, step, isNum, isNegative) {
if (isNegative) { return false; }
if (isNum) { return a < 9 && b < 9; }
return a < b && step === 1;
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, padding, opts) {
if (isNum && (a > 9 || b > 9)) { return false; }
return !padding && num === 1 && a < b;
}

/**
Expand All @@ -230,9 +250,8 @@ function detectSeparator(a, b, step, isNum, isNegative) {
var isChar = isCharClass(a, b, step, isNum, isNegative);
if (!isChar) {
return '|';
} else {
return '~';
}
return '~';
}

/**
Expand Down

0 comments on commit ebb78cc

Please sign in to comment.