Skip to content

Commit

Permalink
fix regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Apr 8, 2019
1 parent 23d5fef commit 65a7182
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ const pad = (input, maxLength, toNumber) => {

const toMaxLen = (input, maxLength) => {
let negative = input[0] === '-' ? '-' : '';
if (negative) input = input.slice(1);
if (negative) {
input = input.slice(1);
maxLength--;
}
while (input.length < maxLength) input = '0' + input;
return negative ? ('-' + input.slice(1)) : input;
return negative ? ('-' + input) : input;
};

const toSequence = (parts, options) => {
Expand Down Expand Up @@ -91,7 +94,10 @@ const toRange = (a, b, isNumbers, options) => {
if (isNumbers) {
return toRegexRange(a, b, { wrap: false, ...options });
}

let start = String.fromCharCode(a);
if (a === b) return start;

let stop = String.fromCharCode(b);
return `[${start}-${stop}]`;
};
Expand Down Expand Up @@ -178,30 +184,33 @@ const fillLetters = (start, end, step = 1, options = {}) => {
return invalidRange(start, end, options);
}


let format = options.transform || (val => String.fromCharCode(val));
let a = `${start}`.charCodeAt(0);
let b = `${end}`.charCodeAt(0);

let descending = a > b;
let min = Math.min(a, b);
let max = Math.max(a, b);

if (options.toRegex && step === 1) {
return toRange(min, max, false, options);
}

let range = [];
let index = 0;
let array = [format(min, index)];

while (min < max - step + 1) {
while (descending ? a >= b : a <= b) {
range.push(format(a, index));
a = descending ? a - step : a + step;
index++;
array.push(format(min += step, index));
}

let result = a > b ? array.reverse() : array;
if (options.toRegex === true) {
return toRegex(result, null, { wrap: false, options });
return toRegex(range, null, { wrap: false, options });
}
return result;

return range;
};

const fill = (start, end, step, options = {}) => {
Expand Down
1 change: 1 addition & 0 deletions test/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('steps', () => {

describe('steps: letters', () => {
it('should use increments with alphabetical ranges', () => {
exact(fill('z', 'a', -2), ['z', 'x', 'v', 't', 'r', 'p', 'n', 'l', 'j', 'h', 'f', 'd', 'b']);
exact(fill('a', 'e', 2), ['a','c', 'e']);
exact(fill('E', 'A', 2), ['E', 'C', 'A']);
});
Expand Down

0 comments on commit 65a7182

Please sign in to comment.