Use substring instead of slice

This commit is contained in:
toasted-nutbread 2019-12-08 15:13:22 -05:00
parent 8ca44d722c
commit bb334acab6
3 changed files with 12 additions and 12 deletions

View File

@ -87,7 +87,7 @@ async function apiTextParse(text, optionsContext) {
while (text.length > 0) { while (text.length > 0) {
const term = []; const term = [];
const [definitions, sourceLength] = await translator.findTermsInternal( const [definitions, sourceLength] = await translator.findTermsInternal(
text.slice(0, options.scanning.length), text.substring(0, options.scanning.length),
dictEnabledSet(options), dictEnabledSet(options),
options.scanning.alphanumeric, options.scanning.alphanumeric,
{} {}
@ -95,16 +95,16 @@ async function apiTextParse(text, optionsContext) {
if (definitions.length > 0) { if (definitions.length > 0) {
dictTermsSort(definitions); dictTermsSort(definitions);
const {expression, reading} = definitions[0]; const {expression, reading} = definitions[0];
const source = text.slice(0, sourceLength); const source = text.substring(0, sourceLength);
for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) { for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) {
const reading = jpConvertReading(text, furigana, options.parsing.readingMode); const reading = jpConvertReading(text, furigana, options.parsing.readingMode);
term.push({text, reading}); term.push({text, reading});
} }
text = text.slice(source.length); text = text.substring(source.length);
} else { } else {
const reading = jpConvertReading(text[0], null, options.parsing.readingMode); const reading = jpConvertReading(text[0], null, options.parsing.readingMode);
term.push({text: text[0], reading}); term.push({text: text[0], reading});
text = text.slice(1); text = text.substring(1);
} }
results.push(term); results.push(term);
} }

View File

@ -148,10 +148,9 @@ class QueryParser {
async setPreview(text) { async setPreview(text) {
const previewTerms = []; const previewTerms = [];
while (text.length > 0) { for (let i = 0, ii = text.length; i < ii; i += 2) {
const tempText = text.slice(0, 2); const tempText = text.substring(i, i + 2);
previewTerms.push([{text: Array.from(tempText)}]); previewTerms.push([{text: tempText.split('')}]);
text = text.slice(2);
} }
this.queryParser.innerHTML = await apiTemplateRender('query-parser.html', { this.queryParser.innerHTML = await apiTemplateRender('query-parser.html', {
terms: previewTerms, terms: previewTerms,

View File

@ -160,16 +160,17 @@ function jpDistributeFuriganaInflected(expression, reading, source) {
} }
const offset = source.length - stemLength; const offset = source.length - stemLength;
const stemExpression = source.slice(0, source.length - offset); const stemExpression = source.substring(0, source.length - offset);
const stemReading = reading.slice( const stemReading = reading.substring(
0, offset === 0 ? reading.length : reading.length - expression.length + stemLength 0,
offset === 0 ? reading.length : reading.length - expression.length + stemLength
); );
for (const segment of jpDistributeFurigana(stemExpression, stemReading)) { for (const segment of jpDistributeFurigana(stemExpression, stemReading)) {
output.push(segment); output.push(segment);
} }
if (stemLength !== source.length) { if (stemLength !== source.length) {
output.push({text: source.slice(stemLength)}); output.push({text: source.substring(stemLength)});
} }
return output; return output;