From 74df800c0be29db1bb7a87f334012276c73a0a6d Mon Sep 17 00:00:00 2001 From: MarvNC Date: Thu, 11 Jul 2024 15:26:22 -0700 Subject: [PATCH] More TS updates --- src/constants.ts | 2 +- src/{convertWikipedia.ts => index.ts} | 14 +++++++------- src/parse/parseLine.ts | 10 +++++----- src/parse/readingParse.test.ts | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) rename src/{convertWikipedia.ts => index.ts} (93%) diff --git a/src/constants.ts b/src/constants.ts index f126b01..24399b0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,4 @@ -export const languagesAllowed = { +export const languages = { ja: 'JA', zh: 'ZH', }; diff --git a/src/convertWikipedia.ts b/src/index.ts similarity index 93% rename from src/convertWikipedia.ts rename to src/index.ts index f18d504..d2b9c0d 100644 --- a/src/convertWikipedia.ts +++ b/src/index.ts @@ -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, @@ -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 = { @@ -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 diff --git a/src/parse/parseLine.ts b/src/parse/parseLine.ts index be42579..6fdeee7 100644 --- a/src/parse/parseLine.ts +++ b/src/parse/parseLine.ts @@ -1,5 +1,5 @@ import { pinyin } from 'pinyin-pro'; -import { languagesAllowed } from '../constants'; +import { languages } from '../constants'; import { getReadingFromDefinition } from './readingParse'; type ParsedLine = { @@ -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); @@ -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, ''); } diff --git a/src/parse/readingParse.test.ts b/src/parse/readingParse.test.ts index acfa26f..f18c96f 100644 --- a/src/parse/readingParse.test.ts +++ b/src/parse/readingParse.test.ts @@ -2,10 +2,10 @@ 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; @@ -13,7 +13,7 @@ const testCases: { }[]; }[] = [ { - lang: languagesAllowed.ja, + lang: languages.ja, cases: [ { line: ` "みぞおちとは、人間の腹の上方中央にある窪んだ部位のこと。鳩尾(きゅうび、みぞおち)、水月(すいげつ)、心窩(しんか、しんわ)とも呼ばれる。みぞおちの内部背中側には腹腔神経叢(ふっくうしんけいそう、英:celiac plexus, solar plexus. 独:solarplexus)という (en:nerve plexus) がある。"@ja .`, @@ -95,7 +95,7 @@ const testCases: { ], }, { - lang: languagesAllowed.zh, + lang: languages.zh, cases: [ { line: ` "《!HERO》(直译“!英雄”)是一部有关耶稣的。这部歌剧基于“如果耶稣出生在宾夕法尼亚州伯利恒将会怎样?”这个问题。2003年首次巡演后,《!HERO》也通过DVD和CD发行。此歌剧也被写成一部小说三部曲和一系列漫画。"@zh .`,