Skip to content

Commit

Permalink
cache regex
Browse files Browse the repository at this point in the history
perf improves a little bit
  • Loading branch information
stevemao committed Jul 21, 2016
1 parent 8a18a6e commit 8713c12
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ function unescapeBraces(str) {
.split(escPeriod).join('.');
}

// cache regex
var rDigit = /^-?0\d/;
var rNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/;
var rAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/
var raCloseB = /,.*\}/;

// Basically just str.split(","), but handling cases
// where we have nested braced sections, which should be
Expand Down Expand Up @@ -88,7 +93,7 @@ function embrace(str) {
return '{' + str + '}';
}
function isPadded(el) {
return /^-?0\d/.test(el);
return rDigit.test(el);
}

function lte(i, y) {
Expand All @@ -109,13 +114,13 @@ function expand(str) {
// no need to expand pre, since it is guaranteed to be free of brace-sets
var pre = m.pre;

var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
var isNumericSequence = rNumericSequence.test(m.body);
var isAlphaSequence = rAlphaSequence.test(m.body);
var isSequence = isNumericSequence || isAlphaSequence;
var isOptions = m.body.indexOf(',') >= 0;
if (!isSequence && !isOptions) {
// {a},b}
if (m.post.match(/,.*\}/)) {
if (m.post.match(raCloseB)) {
// todo: this can probably be optimised if we slightly change the behaviour of `balanced-match`
// we want m.body to be `a},b` :)
str = pre + '{' + m.body + escClose + m.post;
Expand Down

0 comments on commit 8713c12

Please sign in to comment.