From 235052b78def618edb7f23d7fac859050aa45d06 Mon Sep 17 00:00:00 2001 From: Tykok Date: Tue, 21 Feb 2023 15:04:47 +0100 Subject: [PATCH] 2.0.0 --- package-lock.json | 4 +- package.json | 2 +- src/classes/Cedict.ts | 43 +++++++------- src/format-cedict.ts | 84 ++++++++++++++-------------- src/index.ts | 8 +-- src/module.d.ts | 4 +- src/test/dictionary-function.test.ts | 58 +++++++++---------- src/types/ChineseWord.ts | 12 ++-- 8 files changed, 106 insertions(+), 109 deletions(-) diff --git a/package-lock.json b/package-lock.json index f75ac44..7af1220 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tykok/cedict-dictionary", - "version": "1.5.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tykok/cedict-dictionary", - "version": "1.5.0", + "version": "2.0.0", "license": "MIT", "dependencies": { "extract-zip": "^2.0.1", diff --git a/package.json b/package.json index f09b135..8323c04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tykok/cedict-dictionary", - "version": "1.5.0", + "version": "2.0.0", "description": "The Cedict dictionary get and format the cedict to a JSON file", "main": "lib/index.js", "files": [ diff --git a/src/classes/Cedict.ts b/src/classes/Cedict.ts index abff2dd..a4806c4 100644 --- a/src/classes/Cedict.ts +++ b/src/classes/Cedict.ts @@ -1,33 +1,30 @@ -import cedict from '../../data/cedict.json' -import ChineseWord from '../types/ChineseWord' +import cedict from '../../data/cedict.json'; +import ChineseWord from '../types/ChineseWord'; class Cedict { - - static getCedict (): ChineseWord[] { - return cedict + static getCedict(): ChineseWord[] { + return cedict; } - - static getByTraditional (character: string): ChineseWord | undefined { - return Cedict.getCedict().find((word) => word.traditional === character) - } - - static getBySimplified (character: string): ChineseWord | undefined { - return Cedict.getCedict().find((word) => word.simplified === character) + + static getByTraditional(character: string): ChineseWord | undefined { + return Cedict.getCedict().find((word) => word.traditional === character); } - - static getByPinyin (character: string): ChineseWord | undefined { - return Cedict.getCedict().find((word) => word.pinyin === character) + + static getBySimplified(character: string): ChineseWord | undefined { + return Cedict.getCedict().find((word) => word.simplified === character); } - - static getByEnglish (sentence: string): ChineseWord[] | undefined { - return Cedict.getCedict().filter((word) => word.english.includes(sentence)) + + static getByPinyin(character: string): ChineseWord | undefined { + return Cedict.getCedict().find((word) => word.pinyin === character); } - - static allOccurenceOfTraditional (traditional: string): ChineseWord[] | undefined { - return Cedict.getCedict().filter((word) => word.traditional.includes(traditional)) + + static getByEnglish(sentence: string): ChineseWord[] | undefined { + return Cedict.getCedict().filter((word) => word.english.includes(sentence)); } - + static allOccurenceOfTraditional(traditional: string): ChineseWord[] | undefined { + return Cedict.getCedict().filter((word) => word.traditional.includes(traditional)); + } } -export default Cedict \ No newline at end of file +export default Cedict; diff --git a/src/format-cedict.ts b/src/format-cedict.ts index 9e8f2c3..3276c42 100644 --- a/src/format-cedict.ts +++ b/src/format-cedict.ts @@ -1,74 +1,74 @@ -import extract from 'extract-zip' -import * as fs from 'fs' -import * as superagent from 'superagent' -import ChineseWord from './types/ChineseWord' +import extract from 'extract-zip'; +import * as fs from 'fs'; +import * as superagent from 'superagent'; +import ChineseWord from './types/ChineseWord'; -const urlOfZip = 'https://www.mdbg.net/chinese/export/cedict/cedict_1_0_ts_utf-8_mdbg.zip' -const pathOfData = 'data' -const pathOfCedict = `${pathOfData}/cedict_ts.u8` -const pathOfZip = `${pathOfData}/cedict_1_0_ts_utf-8_mdbg.zip` -const pathOfJSON = `${pathOfData}/cedict.json` +const urlOfZip = 'https://www.mdbg.net/chinese/export/cedict/cedict_1_0_ts_utf-8_mdbg.zip'; +const pathOfData = 'data'; +const pathOfCedict = `${pathOfData}/cedict_ts.u8`; +const pathOfZip = `${pathOfData}/cedict_1_0_ts_utf-8_mdbg.zip`; +const pathOfJSON = `${pathOfData}/cedict.json`; /** * Used to parse the cedict file given in the zip */ const parseLine = (line: string): ChineseWord | null => { - if (!line || line === '' || line.startsWith('#')) return null - const splitedLine = line.split(/\/(.*)/s) - if (splitedLine.length <= 0) return null + if (!line || line === '' || line.startsWith('#')) return null; + const splitedLine = line.split(/\/(.*)/s); + if (splitedLine.length <= 0) return null; try { - const english = splitedLine[1] - const charAndPinyin = splitedLine[0].split('[') - const characters = charAndPinyin[0].split(' ') - const traditional = characters[0] - const simplified = characters[1] - let pinyin = charAndPinyin[1] - pinyin = pinyin.split(' ')[0] as unknown as string - pinyin = pinyin.split(']')[0] as unknown as string + const english = splitedLine[1]; + const charAndPinyin = splitedLine[0].split('['); + const characters = charAndPinyin[0].split(' '); + const traditional = characters[0]; + const simplified = characters[1]; + let pinyin = charAndPinyin[1]; + pinyin = pinyin.split(' ')[0] as unknown as string; + pinyin = pinyin.split(']')[0] as unknown as string; - return { traditional, simplified, pinyin, english } + return { traditional, simplified, pinyin, english }; } catch (e: unknown) { - return null + return null; } -} +}; const readFile = (): string[] => { - const file = fs.readFileSync(pathOfCedict, 'utf8') - return file.split('\n') -} + const file = fs.readFileSync(pathOfCedict, 'utf8'); + return file.split('\n'); +}; const parsedArray = (): ChineseWord[] => { - const listOfChineseWord: ChineseWord[] = [] + const listOfChineseWord: ChineseWord[] = []; for (const line of readFile()) { - const word = parseLine(line) + const word = parseLine(line); if (word !== null) { - listOfChineseWord.push(word) + listOfChineseWord.push(word); } } - return listOfChineseWord -} + return listOfChineseWord; +}; const serializeAndSave = (object: any) => { - const strignify = JSON.stringify(object) - fs.writeFileSync(pathOfJSON, strignify) -} + const strignify = JSON.stringify(object); + fs.writeFileSync(pathOfJSON, strignify); +}; const downloadUnzipAndFormat = async () => { return superagent .get(urlOfZip) .on('error', () => { - return null + return null; }) .pipe(fs.createWriteStream(pathOfZip)) .on('finish', async () => { - await extract(`${process.cwd()}/${pathOfZip}`, { dir: `${process.cwd()}/${pathOfData}/` }) - serializeAndSave(parsedArray()) - }) -} + await extract(`${process.cwd()}/${pathOfZip}`, { dir: `${process.cwd()}/${pathOfData}/` }); + serializeAndSave(parsedArray()); + }); +}; const main = async () => { - downloadUnzipAndFormat() -} + downloadUnzipAndFormat(); +}; -main() +main(); diff --git a/src/index.ts b/src/index.ts index 1036661..85e9697 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -import ChineseWord from './types/ChineseWord' -import Cedict from './classes/Cedict' +import ChineseWord from './types/ChineseWord'; +import Cedict from './classes/Cedict'; -export { ChineseWord } +export { ChineseWord }; -export default Cedict +export default Cedict; diff --git a/src/module.d.ts b/src/module.d.ts index f0fc30d..f423444 100644 --- a/src/module.d.ts +++ b/src/module.d.ts @@ -1,4 +1,4 @@ declare module '*cedict.json' { - const value: any - export default value + const value: any; + export default value; } diff --git a/src/test/dictionary-function.test.ts b/src/test/dictionary-function.test.ts index a4f8507..bdc12cc 100644 --- a/src/test/dictionary-function.test.ts +++ b/src/test/dictionary-function.test.ts @@ -1,41 +1,41 @@ -import ChineseWord from '../types/ChineseWord' -import Cedict from '../classes/Cedict' +import ChineseWord from '../types/ChineseWord'; +import Cedict from '../classes/Cedict'; describe('Check property return of dictionary function', () => { it('getByTraditional should have ChineseWord property', () => { - const expected = Cedict.getByTraditional('一') + const expected = Cedict.getByTraditional('一'); - expect(expected).toHaveProperty('traditional') - expect(expected).toHaveProperty('simplified') - expect(expected).toHaveProperty('pinyin') - expect(expected).toHaveProperty('english') - }) + expect(expected).toHaveProperty('traditional'); + expect(expected).toHaveProperty('simplified'); + expect(expected).toHaveProperty('pinyin'); + expect(expected).toHaveProperty('english'); + }); it('get should have ChineseWord property', () => { - const expected = Cedict.getBySimplified('一') + const expected = Cedict.getBySimplified('一'); - expect(expected).toHaveProperty('traditional') - expect(expected).toHaveProperty('simplified') - expect(expected).toHaveProperty('pinyin') - expect(expected).toHaveProperty('english') - }) + expect(expected).toHaveProperty('traditional'); + expect(expected).toHaveProperty('simplified'); + expect(expected).toHaveProperty('pinyin'); + expect(expected).toHaveProperty('english'); + }); it('getByEnglsih should have ChineseWord property', () => { - const englishArray = Cedict.getByEnglish('one') as ChineseWord[] - const expected = englishArray[0] + const englishArray = Cedict.getByEnglish('one') as ChineseWord[]; + const expected = englishArray[0]; - expect(expected).toHaveProperty('traditional') - expect(expected).toHaveProperty('simplified') - expect(expected).toHaveProperty('pinyin') - expect(expected).toHaveProperty('english') - }) + expect(expected).toHaveProperty('traditional'); + expect(expected).toHaveProperty('simplified'); + expect(expected).toHaveProperty('pinyin'); + expect(expected).toHaveProperty('english'); + }); it('getByPinyin should have ChineseWord property', () => { - const expected = Cedict.getByPinyin('yi1') as ChineseWord - - expect(expected).toHaveProperty('traditional') - expect(expected).toHaveProperty('simplified') - expect(expected).toHaveProperty('pinyin') - expect(expected).toHaveProperty('english') - }) -}) + const expected = Cedict.getByPinyin('yi1') as ChineseWord; + + expect(expected).toHaveProperty('traditional'); + expect(expected).toHaveProperty('simplified'); + expect(expected).toHaveProperty('pinyin'); + expect(expected).toHaveProperty('english'); + }); +}); diff --git a/src/types/ChineseWord.ts b/src/types/ChineseWord.ts index be0d4ce..8b0779e 100644 --- a/src/types/ChineseWord.ts +++ b/src/types/ChineseWord.ts @@ -5,22 +5,22 @@ type ChineseWord = { /** * Chinese traditional character */ - traditional: string + traditional: string; /** * Chinese simplified character */ - simplified: string + simplified: string; /** * Pinyin is the official transcription of Mandarin Chinese */ - pinyin: string + pinyin: string; /** * English translation of the chinese word */ - english: string -} + english: string; +}; -export default ChineseWord +export default ChineseWord;