Skip to content

Commit

Permalink
Merge pull request #19 from MarvNC/typed-add-kanji
Browse files Browse the repository at this point in the history
Typed Add Kanji
  • Loading branch information
MarvNC authored Jul 7, 2024
2 parents 197afa6 + 0bc6df8 commit cfd3582
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('ア')
Expand Down
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down
28 changes: 26 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {
DictionaryKanjiBankV3,
KanjiInformation,
KanjiStats,
} from './types/yomitan/kanjibank';
import {
DictionaryKanjiMetaBankV3,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cfd3582

Please sign in to comment.