Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed Add Kanji #19

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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('あ')
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