Skip to content

Commit

Permalink
Optimize computeErasParse and computeMonthsParse
Browse files Browse the repository at this point in the history
  • Loading branch information
Alanscut committed Jun 4, 2020
1 parent e3c6790 commit 3afc8d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
20 changes: 13 additions & 7 deletions src/lib/units/era.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,22 @@ function computeErasParse() {
mixedPieces = [],
i,
l,
erasName,
erasAbbr,
erasNarrow,
eras = this.eras();

for (i = 0, l = eras.length; i < l; ++i) {
namePieces.push(regexEscape(eras[i].name));
abbrPieces.push(regexEscape(eras[i].abbr));
narrowPieces.push(regexEscape(eras[i].narrow));

mixedPieces.push(regexEscape(eras[i].name));
mixedPieces.push(regexEscape(eras[i].abbr));
mixedPieces.push(regexEscape(eras[i].narrow));
erasName = regexEscape(eras[i].name);
erasAbbr = regexEscape(eras[i].abbr);
erasNarrow = regexEscape(eras[i].narrow);

namePieces.push(erasName);
abbrPieces.push(erasAbbr);
narrowPieces.push(erasNarrow);
mixedPieces.push(regexEscape(erasName));
mixedPieces.push(regexEscape(erasAbbr));
mixedPieces.push(regexEscape(erasNarrow));
}

this._erasRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i');
Expand Down
21 changes: 9 additions & 12 deletions src/lib/units/month.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,27 +314,24 @@ function computeMonthsParse() {
longPieces = [],
mixedPieces = [],
i,
mom;
mom,
shortP,
longP;
for (i = 0; i < 12; i++) {
// make the regex if we don't have it already
mom = createUTC([2000, i]);
shortPieces.push(this.monthsShort(mom, ''));
longPieces.push(this.months(mom, ''));
mixedPieces.push(this.months(mom, ''));
mixedPieces.push(this.monthsShort(mom, ''));
shortP = regexEscape(this.monthsShort(mom, ''));
longP = regexEscape(this.months(mom, ''));
shortPieces.push(shortP);
longPieces.push(longP);
mixedPieces.push(longP);
mixedPieces.push(shortP);
}
// Sorting makes sure if one month (or abbr) is a prefix of another it
// will match the longer piece.
shortPieces.sort(cmpLenRev);
longPieces.sort(cmpLenRev);
mixedPieces.sort(cmpLenRev);
for (i = 0; i < 12; i++) {
shortPieces[i] = regexEscape(shortPieces[i]);
longPieces[i] = regexEscape(longPieces[i]);
}
for (i = 0; i < 24; i++) {
mixedPieces[i] = regexEscape(mixedPieces[i]);
}

this._monthsRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i');
this._monthsShortRegex = this._monthsRegex;
Expand Down

0 comments on commit 3afc8d5

Please sign in to comment.