diff options
Diffstat (limited to 'api/japanese.ts')
-rw-r--r-- | api/japanese.ts | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/api/japanese.ts b/api/japanese.ts index 0396821..9319cdd 100644 --- a/api/japanese.ts +++ b/api/japanese.ts @@ -4,6 +4,26 @@ import "../util/string.ts"; import "../util/japanese.ts"; import "../util/array.ts"; +const formatters = { + "HTML": tokens => tokens.reduce((out, token) => { + if (token.ruby) out += `<ruby>${escape(token.writing)}<rt>${escape(token.reading)}</rt></ruby>`; + else out += token.writing; + return out; + }, ""), + "parenthesis": tokens => tokens.reduce((out, token) => { + if (token.ruby) out += `${token.writing}(${token.reading}) `; + else out += token.writing; + return out; + }, ""), + "refold-tools": tokens => tokens.reduce((out, token) => { + if (token.ruby) out += `[${token.writing}](${token.reading})`; + else out += token.writing; + return out; + }, ""), +} satisfies { [name: string]: (tokens: Array<JapaneseToken>) => string }; + +export type JapaneseFormatter = keyof typeof formatters; + /** @interface Piece */ interface JapaneseToken { /** @prop token writing (kanji/katakana/hiragana) */ @@ -22,24 +42,6 @@ export default class Japanese { public reading: string; private normalized: string; - private formatters = { - "HTML": tokens => tokens.reduce((out, token) => { - if (token.ruby) out += `<ruby>${escape(token.writing)}<rt>${escape(token.reading)}</rt></ruby>`; - else out += token.writing; - return out; - }, ""), - "parenthesis": tokens => tokens.reduce((out, token) => { - if (token.ruby) out += `${token.writing}(${token.reading}) `; - else out += token.writing; - return out; - }, ""), - "refold-tools": tokens => tokens.reduce((out, token) => { - if (token.ruby) out += `[${token.writing}](${token.reading})`; - else out += token.writing; - return out; - }, ""), - } satisfies Record<string, (tokens: Array<JapaneseToken>) => string>; - constructor(writing: string, reading: string) { this.writing = writing; this.reading = reading; @@ -47,8 +49,8 @@ export default class Japanese { } /** @summary format this as text with furigana */ - public furigana(format: keyof typeof this.formatters = "HTML"): string { - return this.formatters[format](this.tokenize()); + public furigana(format: JapaneseFormatter = "HTML"): string { + return formatters[format](this.tokenize()); } /** @@ -151,3 +153,6 @@ export default class Japanese { } } +export type test = keyof typeof Japanese.formatters; + +var gert: test = "HTML"; |