From 6ff55171a57db86d1ba709aa4051f769fd565650 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Sun, 10 Mar 2024 13:36:46 -0700 Subject: [PATCH 1/2] Typed add kanji --- examples/example.js | 14 ++++++++++++++ src/index.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/examples/example.js b/examples/example.js index d91197d..e2e10ec 100644 --- a/examples/example.js +++ b/examples/example.js @@ -113,6 +113,20 @@ const { { jis213: '2-04-06', skip: '1-3-8', strokes: '11', ucs: '35A8' }, ]); + dictionary.addKanji({ + kanji: '読', + kunyomi: 'あ', + onyomi: 'ア', + meanings: ['Asia'], + stats: { + strokes: '7', + grade: '8', + }, + }); + dictionary.addKanji({ + kanji: '詠', + }); + const kanjiEntry = new KanjiEntry('亜') .setKunyomi('あ') .setOnyomi('ア') diff --git a/src/index.ts b/src/index.ts index b777f72..bdb705c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ import { import { DictionaryKanjiBankV3, KanjiInformation, + KanjiStats, } from './types/yomitan/kanjibank'; import { DictionaryKanjiMetaBankV3, @@ -170,8 +171,31 @@ export class Dictionary { * Adds a kanji to the dictionary * @param kanji - The kanji to add */ - async addKanji(kanji: KanjiInformation) { - this.kanjiBank.push(kanji); + async addKanji( + kanji: + | KanjiInformation + | { + kanji: string; + onyomi?: string; + kunyomi?: string; + tags?: string; + meanings?: string[]; + stats?: KanjiStats; + }, + ) { + let array: KanjiInformation = Array.isArray(kanji) + ? kanji + : [ + kanji.kanji, + kanji.onyomi ?? '', + kanji.kunyomi ?? '', + kanji.tags ?? '', + kanji.meanings ?? [], + kanji.stats ?? {}, + ]; + // If kanji is KanjiInformation array + + this.kanjiBank.push(array); this.stats.kanjiCount++; if (this.kanjiBank.length >= this.options.termBankMaxSize) { await this.saveKanjiBank(); From 0bc6df81967f6f625f60b040e5896dbee552fb11 Mon Sep 17 00:00:00 2001 From: MarvNC Date: Sat, 6 Jul 2024 21:18:46 -0700 Subject: [PATCH 2/2] Add docs for typed add kanji --- readme.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/readme.md b/readme.md index f69d0ff..c480b11 100644 --- a/readme.md +++ b/readme.md @@ -119,6 +119,21 @@ await dictionary.addTerm(entry2); Kanji entries can be added using the `addKanji()` method: ```javascript +dictionary.addKanji({ + kanji: '読', + kunyomi: 'あ', + onyomi: 'ア', + meanings: ['Asia'], + stats: { + strokes: '7', + grade: '8', + }, +}); +dictionary.addKanji({ + kanji: '詠', +}); + +// Alternatively, create a KanjiEntry object first const { KanjiEntry } = require('yomichan-dict-builder'); const kanjiEntry = new KanjiEntry('亜') .setKunyomi('あ')