fixing syntax issues
This commit is contained in:
parent
b38450b6ce
commit
a4c3ba434a
@ -49,32 +49,32 @@ function distributeFurigana(word, reading) {
|
||||
return [str.substring(0, i), str.substring(i)];
|
||||
}
|
||||
const isKanji = c => jpIsKanji(c) ||
|
||||
c == "\u3005"; /* kurikaeshi */
|
||||
c === '\u3005'; /* kurikaeshi */
|
||||
const isKana = c => jpIsKana(c) ||
|
||||
c == "\u30fc"; /* chouonpu */
|
||||
c === '\u30fc'; /* chouonpu */
|
||||
function parse(word) {
|
||||
const res = [];
|
||||
while (word.length > 0) {
|
||||
const c = word.charAt(0);
|
||||
if (isKana(c)) {
|
||||
const [text, rest] = span(word, isKana);
|
||||
res.push({ type: "kana", text });
|
||||
res.push({ type: 'kana', text });
|
||||
word = rest;
|
||||
} else if (isKanji(c)) {
|
||||
const [text, rest] = span(word, isKanji);
|
||||
res.push({ type: "kanji", text });
|
||||
res.push({ type: 'kanji', text });
|
||||
word = rest;
|
||||
} else return null;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
let fallback = () => [{ text: word, furigana: reading }];
|
||||
let parts = parse(word);
|
||||
if (parts == null) return fallback();
|
||||
const fallback = () => [{ text: word, furigana: reading }];
|
||||
const parts = parse(word);
|
||||
if (!parts) return fallback();
|
||||
let parti = 0;
|
||||
let readingi = 0;
|
||||
let res = [];
|
||||
const res = [];
|
||||
let current = null;
|
||||
function backtrack() {
|
||||
parti--;
|
||||
@ -86,7 +86,7 @@ function distributeFurigana(word, reading) {
|
||||
switch (part.type) {
|
||||
case 'kana':
|
||||
if (reading.startsWith(wanakana.toHiragana(part.text), readingi)) {
|
||||
if (parti == parts.length - 1 && readingi != reading.length - part.text.length) {
|
||||
if (parti === parts.length - 1 && readingi !== reading.length - part.text.length) {
|
||||
backtrack();
|
||||
} else {
|
||||
readingi += part.text.length;
|
||||
@ -95,15 +95,15 @@ function distributeFurigana(word, reading) {
|
||||
}
|
||||
} else backtrack();
|
||||
break;
|
||||
case "kanji":
|
||||
current = current || "";
|
||||
if (parti == parts.length - 1) {
|
||||
case 'kanji':
|
||||
current = current || '';
|
||||
if (parti === parts.length - 1) {
|
||||
// last part, consume all
|
||||
current += reading.substring(readingi);
|
||||
} else {
|
||||
const nextText = parts[parti + 1].text;
|
||||
let end = reading.indexOf(nextText, readingi + 1); // consume at least one character
|
||||
if (end == -1) {
|
||||
const end = reading.indexOf(nextText, readingi + 1); // consume at least one character
|
||||
if (end === -1) {
|
||||
return fallback();
|
||||
}
|
||||
current += reading.substring(readingi, end);
|
||||
|
Loading…
Reference in New Issue
Block a user