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,111 +128,113 @@
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 // split the string from the last break
// like the beginning of a translation string // to the character before the cursor
if (str.slice(cursor, cursor + 2) === '[[') { // add that to the result array
// split the string from the last break toTranslate.push(str.slice(lastBreak, cursor));
// to the character before the cursor // set the cursor position past the beginning
// add that to the result array // brackets of the translation string
toTranslate.push(str.slice(lastBreak, cursor)); cursor += 2;
// set the cursor position past the beginning // set the last break to our current
// brackets of the translation string // spot since we just broke the string
cursor += 2; lastBreak = cursor;
// set the last break to our current // we're in a token now
// spot since we just broke the string inToken = true;
lastBreak = cursor;
// we're in a token now // the current level of nesting of the translation strings
inToken = true; var level = 0;
var char0;
// the current level of nesting of the translation strings var char1;
var level = 0; // validating the current string is actually a translation
var sliced; var textBeforeColonFound = false;
// validating the current string is actually a translation var colonFound = false;
var textBeforeColonFound = false; var textAfterColonFound = false;
var colonFound = false; var commaAfterNameFound = false;
var textAfterColonFound = false;
var commaAfterNameFound = false; while (cursor + 2 <= len) {
char0 = str[cursor];
while (cursor + 2 <= len) { char1 = str[cursor + 1];
sliced = str.slice(cursor, cursor + 2); // 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(char0)) {
if (!textBeforeColonFound && validTextRegex.test(sliced[0])) { 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 && char0 === ':') {
} else if (textBeforeColonFound && !colonFound && sliced[0] === ':') { 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(char0)) {
} else if (colonFound && !textAfterColonFound && validTextRegex.test(sliced[0])) { textAfterColonFound = true;
textAfterColonFound = true; cursor += 1;
cursor += 1; } else if (textAfterColonFound && !commaAfterNameFound && char0 === ',') {
} else if (textAfterColonFound && !commaAfterNameFound && sliced[0] === ',') { 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(char0)) {
invalidTextRegex.test(sliced[0])) { cursor += 1;
cursor += 1; lastBreak -= 2;
lastBreak -= 2; // no longer in a token
// no longer in a token inToken = false;
inToken = false; if (level > 0) {
if (level > 0) {
level -= 1;
} else {
break;
}
// if we're at the beginning of another translation string,
// we're nested, so add to our level
} else if (sliced === '[[') {
level += 1;
cursor += 2;
// if we're at the end of a translation string
} else if (sliced === ']]') {
// if we're at the base level, then this is the end
if (level === 0) {
// so grab the name and args
var currentSlice = str.slice(lastBreak, cursor);
var result = split(currentSlice);
var name = result[0];
var args = result.slice(1);
// make a backup based on the raw string of the token
// if there are arguments to the token
var backup = '';
if (args && args.length) {
backup = this.translate(currentSlice);
}
// add the translation promise to the array
toTranslate.push(this.translateKey(name, args, backup));
// skip past the ending brackets
cursor += 2;
// set this as our last break
lastBreak = cursor;
// and we're no longer in a translation string,
// so continue with the main loop
inToken = false;
break;
}
// otherwise we lower the level
level -= 1; level -= 1;
// and skip past the ending brackets
cursor += 2;
} else { } else {
// otherwise just move to the next character break;
cursor += 1;
} }
// if we're at the beginning of another translation string,
// we're nested, so add to our level
} else if (char0 === '[' && char1 === '[') {
level += 1;
cursor += 2;
// if we're at the end of a translation string
} else if (char0 === ']' && char1 === ']') {
// if we're at the base level, then this is the end
if (level === 0) {
// so grab the name and args
var currentSlice = str.slice(lastBreak, cursor);
var result = split(currentSlice);
var name = result[0];
var args = result.slice(1);
// make a backup based on the raw string of the token
// if there are arguments to the token
var backup = '';
if (args && args.length) {
backup = this.translate(currentSlice);
}
// add the translation promise to the array
toTranslate.push(this.translateKey(name, args, backup));
// skip past the ending brackets
cursor += 2;
// set this as our last break
lastBreak = cursor;
// and we're no longer in a translation string,
// so continue with the main loop
inToken = false;
break;
}
// otherwise we lower the level
level -= 1;
// and skip past the ending brackets
cursor += 2;
} else {
// otherwise just move to the next character
cursor += 1;
} }
} }
// move to the next character
cursor += 1; // skip to the next [[
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