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/readme.md b/readme.md index ff2896d..a4ea89e 100644 --- a/readme.md +++ b/readme.md @@ -125,6 +125,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('あ') 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();