Optimize translator

- Use `slice` less
- Skip iterations with `indexOf`
v1.18.x
Peter Jaszkowiak 7 years ago committed by Baris Usakli
parent 684a66a760
commit f4355efb30

@ -111,10 +111,10 @@
var level = 0; var level = 0;
while (i + 2 <= len) { while (i + 2 <= len) {
if (text.slice(i, i + 2) === '[[') { if (text[i] === '[' && text[i + 1] === '[') {
level += 1; level += 1;
i += 1; i += 1;
} else if (text.slice(i, i + 2) === ']]') { } else if (text[i] === ']' && text[i + 1] === ']') {
level -= 1; level -= 1;
i += 1; i += 1;
} else if (level === 0 && text[i] === ',' && text[i - 1] !== '\\') { } else if (level === 0 && text[i] === ',' && text[i - 1] !== '\\') {
@ -128,13 +128,13 @@
return arr; return arr;
} }
// move to the first [[
cursor = str.indexOf('[[', cursor);
// the loooop, we'll go to where the cursor // the loooop, we'll go to where the cursor
// is equal to the length of the string since // is equal to the length of the string since
// slice doesn't include the ending index // slice doesn't include the ending index
while (cursor + 2 <= len) { while (cursor + 2 <= len && cursor !== -1) {
// if the current position in the string looks
// like the beginning of a translation string
if (str.slice(cursor, cursor + 2) === '[[') {
// split the string from the last break // split the string from the last break
// to the character before the cursor // to the character before the cursor
// add that to the result array // add that to the result array
@ -150,7 +150,8 @@
// the current level of nesting of the translation strings // the current level of nesting of the translation strings
var level = 0; var level = 0;
var sliced; var char0;
var char1;
// validating the current string is actually a translation // validating the current string is actually a translation
var textBeforeColonFound = false; var textBeforeColonFound = false;
var colonFound = false; var colonFound = false;
@ -158,28 +159,29 @@
var commaAfterNameFound = false; var commaAfterNameFound = false;
while (cursor + 2 <= len) { while (cursor + 2 <= len) {
sliced = str.slice(cursor, cursor + 2); char0 = str[cursor];
char1 = str[cursor + 1];
// found some text after the double bracket, // found some text after the double bracket,
// so this is probably a translation string // so this is probably a translation string
if (!textBeforeColonFound && validTextRegex.test(sliced[0])) { if (!textBeforeColonFound && validTextRegex.test(char0)) {
textBeforeColonFound = true; textBeforeColonFound = true;
cursor += 1; cursor += 1;
// found a colon, so this is probably a translation string // found a colon, so this is probably a translation string
} else if (textBeforeColonFound && !colonFound && sliced[0] === ':') { } else if (textBeforeColonFound && !colonFound && char0 === ':') {
colonFound = true; colonFound = true;
cursor += 1; cursor += 1;
// found some text after the colon, // found some text after the colon,
// so this is probably a translation string // so this is probably a translation string
} else if (colonFound && !textAfterColonFound && validTextRegex.test(sliced[0])) { } else if (colonFound && !textAfterColonFound && validTextRegex.test(char0)) {
textAfterColonFound = true; textAfterColonFound = true;
cursor += 1; cursor += 1;
} else if (textAfterColonFound && !commaAfterNameFound && sliced[0] === ',') { } else if (textAfterColonFound && !commaAfterNameFound && char0 === ',') {
commaAfterNameFound = true; commaAfterNameFound = true;
cursor += 1; cursor += 1;
// a space or comma was found before the name // a space or comma was found before the name
// this isn't a translation string, so back out // this isn't a translation string, so back out
} else if (!(textBeforeColonFound && colonFound && textAfterColonFound && commaAfterNameFound) && } else if (!(textBeforeColonFound && colonFound && textAfterColonFound && commaAfterNameFound) &&
invalidTextRegex.test(sliced[0])) { invalidTextRegex.test(char0)) {
cursor += 1; cursor += 1;
lastBreak -= 2; lastBreak -= 2;
// no longer in a token // no longer in a token
@ -191,11 +193,11 @@
} }
// if we're at the beginning of another translation string, // if we're at the beginning of another translation string,
// we're nested, so add to our level // we're nested, so add to our level
} else if (sliced === '[[') { } else if (char0 === '[' && char1 === '[') {
level += 1; level += 1;
cursor += 2; cursor += 2;
// if we're at the end of a translation string // if we're at the end of a translation string
} else if (sliced === ']]') { } else if (char0 === ']' && char1 === ']') {
// if we're at the base level, then this is the end // if we're at the base level, then this is the end
if (level === 0) { if (level === 0) {
// so grab the name and args // so grab the name and args
@ -230,9 +232,9 @@
cursor += 1; cursor += 1;
} }
} }
}
// move to the next character // skip to the next [[
cursor += 1; cursor = str.indexOf('[[', cursor);
} }
// ending string of source // ending string of source
@ -304,7 +306,7 @@
* Load translation file (or use a cached version), and optionally return the translation of a certain key * Load translation file (or use a cached version), and optionally return the translation of a certain key
* @param {string} namespace - The file name of the translation namespace * @param {string} namespace - The file name of the translation namespace
* @param {string} [key] - The key of the specific translation to getJSON * @param {string} [key] - The key of the specific translation to getJSON
* @returns {Promise<{ [key: string]: string }>|Promise<string>} * @returns {Promise<{ [key: string]: string } | string>}
*/ */
Translator.prototype.getTranslation = function getTranslation(namespace, key) { Translator.prototype.getTranslation = function getTranslation(namespace, key) {
var translation; var translation;

Loading…
Cancel
Save