Skip to content

Commit

Permalink
More TS updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jul 11, 2024
1 parent 11064af commit 74df800
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const languagesAllowed = {
export const languages = {
ja: 'JA',
zh: 'ZH',
};
14 changes: 7 additions & 7 deletions src/convertWikipedia.ts → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import readline from 'readline';
import { Dictionary, TermEntry } from 'yomichan-dict-builder';

import { parseLine } from './parse/parseLine';
import { languagesAllowed } from './constants';
import { languages } from './constants';
import { getVersion } from './util/getVersion';
import type {
StructuredContent,
Expand Down Expand Up @@ -116,9 +116,9 @@ function processLine(line: string, dict: Dictionary, lang: string) {
// Read more
const articleLink = `https://${lang.toLowerCase()}.wikipedia.org/wiki/${termSlug}`;
const readTheRest =
lang === languagesAllowed.ja
lang === languages.ja
? '続きを読む'
: lang === languagesAllowed.zh
: lang === languages.zh
? '查看更多'
: 'Read more';
const linkSC: StructuredContentNode = {
Expand Down Expand Up @@ -155,17 +155,17 @@ function processLine(line: string, dict: Dictionary, lang: string) {
function readArgs() {
// Read arguments: node convertWikipedia.js [language] [date of dump]
const langInput =
process.argv[2].toLowerCase() as keyof typeof languagesAllowed;
process.argv[2].toLowerCase() as keyof typeof languages;
// Assert language is valid
if (!languagesAllowed[langInput]) {
if (!languages[langInput]) {
throw new Error(
`Language ${langInput} is not allowed. Allowed languages: ${Object.keys(
languagesAllowed
languages
).join(', ')}`
);
}

const lang = languagesAllowed[langInput];
const lang = languages[langInput];

const dateInput = process.argv[3];
// Assert date is valid in format YYYY-MM-DD
Expand Down
10 changes: 5 additions & 5 deletions src/parse/parseLine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pinyin } from 'pinyin-pro';
import { languagesAllowed } from '../constants';
import { languages } from '../constants';
import { getReadingFromDefinition } from './readingParse';

type ParsedLine = {
Expand All @@ -13,12 +13,12 @@ type ParsedLine = {
/**
*
* @param {string} line
* @param {typeof languagesAllowed[keyof typeof languagesAllowed]} lang
* @param {typeof languages[keyof typeof languages]} lang
* @returns {ParsedLine}
*/
function parseLine(
line: string,
lang: (typeof languagesAllowed)[keyof typeof languagesAllowed]
lang: (typeof languages)[keyof typeof languages]
): ParsedLine {
// remove last 6 characters
line = line.slice(0, -6);
Expand All @@ -41,9 +41,9 @@ function parseLine(
term = term.replace(/_/g, ' ');

let reading = '';
if (lang === languagesAllowed.ja) {
if (lang === languages.ja) {
reading = getReadingFromDefinition(definition, term);
} else if (lang === languagesAllowed.zh) {
} else if (lang === languages.zh) {
reading = pinyin(term, { mode: 'surname' });
reading = reading.replace(/ /g, '');
}
Expand Down
8 changes: 4 additions & 4 deletions src/parse/readingParse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import test from 'ava';

import { parseLine } from './parseLine.js';

import { languagesAllowed } from '../constants.js';
import { languages } from '../constants.js';

const testCases: {
lang: (typeof languagesAllowed)[keyof typeof languagesAllowed];
lang: (typeof languages)[keyof typeof languages];
cases: {
line: string;
term: string;
expectedReading: string;
}[];
}[] = [
{
lang: languagesAllowed.ja,
lang: languages.ja,
cases: [
{
line: `<http://ja.dbpedia.org/resource/みぞおち> <http://www.w3.org/2000/01/rdf-schema#comment> "みぞおちとは、人間の腹の上方中央にある窪んだ部位のこと。鳩尾(きゅうび、みぞおち)、水月(すいげつ)、心窩(しんか、しんわ)とも呼ばれる。みぞおちの内部背中側には腹腔神経叢(ふっくうしんけいそう、英:celiac plexus, solar plexus. 独:solarplexus)という (en:nerve plexus) がある。"@ja .`,
Expand Down Expand Up @@ -95,7 +95,7 @@ const testCases: {
],
},
{
lang: languagesAllowed.zh,
lang: languages.zh,
cases: [
{
line: `<http://zh.dbpedia.org/resource/!HERO> <http://www.w3.org/2000/01/rdf-schema#comment> "《!HERO》(直译“!英雄”)是一部有关耶稣的。这部歌剧基于“如果耶稣出生在宾夕法尼亚州伯利恒将会怎样?”这个问题。2003年首次巡演后,《!HERO》也通过DVD和CD发行。此歌剧也被写成一部小说三部曲和一系列漫画。"@zh .`,
Expand Down

0 comments on commit 74df800

Please sign in to comment.