From 26fbcfcce56cb1aedcfbe3bd16bd68cd42adaf80 Mon Sep 17 00:00:00 2001 From: Lukince Date: Thu, 28 Oct 2021 18:09:56 +0900 Subject: [PATCH] clean up source tree --- mod.ts | 1 + src/assemblers/assemble.ts | 19 --------- src/entities/assembler.ts | 4 +- src/entities/converter.ts | 26 ++++++++++++ src/entities/hangul.ts | 42 +++++++++++-------- .../disassemble.ts => modules/assemble.ts} | 18 ++++++++ test/hangul.ts | 9 ++-- 7 files changed, 76 insertions(+), 43 deletions(-) delete mode 100644 src/assemblers/assemble.ts create mode 100644 src/entities/converter.ts rename src/{assemblers/disassemble.ts => modules/assemble.ts} (64%) diff --git a/mod.ts b/mod.ts index 78dbacc..2b97200 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,5 @@ export { Hangul } from "./src/entities/hangul.ts" +export { Converter } from "./src/entities/converter.ts" export { Assembler } from "./src/entities/assembler.ts" export { InteractLists, ComplexInteractionLists } from "./src/entities/interactList.ts" export { HangulBuilder } from "./src/entities/hangulBuilder.ts" diff --git a/src/assemblers/assemble.ts b/src/assemblers/assemble.ts deleted file mode 100644 index c533922..0000000 --- a/src/assemblers/assemble.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { InteractLists } from "../entities/interactList.ts" -import { toKorean } from "../modules/convert.ts" - -export function assemble(str: string): string { - const c = str.split(''); - let r = []; - - for (let i = 0; i < c.length; i++) { - const ko = InteractLists.allInteractions.map(v => v[0]); - const en = InteractLists.allInteractions.map(v => v[1]); - r.push( - ko.includes(c[i]) ? - en[ko.indexOf(c[i])] : - c[i] - ); - } - - return toKorean(r.reduce((p, c) => p + c)); -} \ No newline at end of file diff --git a/src/entities/assembler.ts b/src/entities/assembler.ts index a7c29a0..c6c30b8 100644 --- a/src/entities/assembler.ts +++ b/src/entities/assembler.ts @@ -1,6 +1,4 @@ -import { assemble } from "../assemblers/assemble.ts" -import { disassemble } from "../assemblers/disassemble.ts" -import {} from "../assemblers/disassemble.ts" +import { assemble, disassemble } from "../modules/assemble.ts" /** * Provide hangul assemble and disassemble function. diff --git a/src/entities/converter.ts b/src/entities/converter.ts new file mode 100644 index 0000000..142ae0d --- /dev/null +++ b/src/entities/converter.ts @@ -0,0 +1,26 @@ +import { toKorean } from "../modules/convert.ts" +import { disassemble } from "../modules/assemble.ts" +import { InteractLists } from "./interactList.ts" + +export class Converter { + static toKorean(eng: string): string { + return toKorean(eng); + } + + static toEnglish(kor: string): string { + const c = disassemble(kor); + let r = []; + + for (let i = 0; i < c.length; i++) { + const ko = InteractLists.allInteractions.map(v => v[0]); + const en = InteractLists.allInteractions.map(v => v[1]); + r.push( + ko.includes(c[i]) ? + en[ko.indexOf(c[i])] : + c[i] + ); + } + + return r.reduce((p, c) => p + c); + } +} \ No newline at end of file diff --git a/src/entities/hangul.ts b/src/entities/hangul.ts index b6a3927..1a44c1d 100644 --- a/src/entities/hangul.ts +++ b/src/entities/hangul.ts @@ -1,11 +1,10 @@ -import { toKorean } from "../modules/convert.ts" import { Assembler } from "../entities/assembler.ts" -import { InteractLists } from "./interactList.ts" import { HangulBuilder } from "./hangulBuilder.ts" import { StringType, getType } from "./stringType.ts" import { random, RandomOption } from "../modules/random.ts" +import { Converter } from "./converter.ts"; function insert(origin: string, ind: number, value: string): string { return [origin.slice(0, ind), value, origin.slice(ind)].join(''); @@ -92,37 +91,44 @@ export class Hangul { return Hangul.getType(this.content); } + /** + * Convert specific value to Korean. + * @example Hangul.engToKor(new Hangul('dks')) // '안' + * @param str The value that want to convert + * @returns Hangul object that contains korean string + */ + static engToKor(str: Hangul): Hangul /** * Convert specific value to Korean. * @example Hangul.engToKor('dks') // '안' * @param str The value that want to convert * @returns Korean string */ - static engToKor(str: string): string { - return toKorean(str); + static engToKor(str: string): string + static engToKor(str: string | Hangul): string | Hangul { + const s: string = Converter.toKorean(str instanceof Hangul ? str.content : str); + + return str instanceof Hangul ? new Hangul(s) : s; } + /** + * Convert specific value to English. + * @example Hangul.korToEng(new Hangul('안')) // 'dks' + * @param str The value that want to convert + * @returns Hangul object that contains english string + */ + static korToEng(str: Hangul): Hangul /** * Convert specific value to English. * @example Hangul.korToEng('안') // 'dks' * @param str The value that want to convert * @returns English string */ - static korToEng(str: string): string { - const c = Assembler.disassemble(str); - let r = []; - - for (let i = 0; i < c.length; i++) { - const ko = InteractLists.allInteractions.map(v => v[0]); - const en = InteractLists.allInteractions.map(v => v[1]); - r.push( - ko.includes(c[i]) ? - en[ko.indexOf(c[i])] : - c[i] - ); - } + static korToEng(str: string): string + static korToEng(str: string | Hangul): string | Hangul { + const s: string = Converter.toEnglish(str instanceof Hangul ? str.content : str); - return r.reduce((p, c) => p + c); + return str instanceof Hangul ? new Hangul(s) : s; } /** diff --git a/src/assemblers/disassemble.ts b/src/modules/assemble.ts similarity index 64% rename from src/assemblers/disassemble.ts rename to src/modules/assemble.ts index 35625e3..3bdf7cf 100644 --- a/src/assemblers/disassemble.ts +++ b/src/modules/assemble.ts @@ -1,4 +1,22 @@ import { InteractLists } from "../entities/interactList.ts" +import { toKorean } from "./convert.ts" + +export function assemble(str: string): string { + const c = str.split(''); + let r = []; + + for (let i = 0; i < c.length; i++) { + const ko = InteractLists.allInteractions.map(v => v[0]); + const en = InteractLists.allInteractions.map(v => v[1]); + r.push( + ko.includes(c[i]) ? + en[ko.indexOf(c[i])] : + c[i] + ); + } + + return toKorean(r.reduce((p, c) => p + c)); +} export function disassemble(str: string): string[] { const ch: string[] = str.split(''); diff --git a/test/hangul.ts b/test/hangul.ts index 2c030b3..4f62f85 100644 --- a/test/hangul.ts +++ b/test/hangul.ts @@ -2,12 +2,12 @@ import { Hangul } from "../mod.ts" -const han = new Hangul("테스트"); +const han: Hangul = new Hangul("테스트"); console.log(han.toString()); han.append("!"); -han.insert(0, "Insert "); +han.insert(0, "insert "); console.log(han.toString()); @@ -15,4 +15,7 @@ console.log(Hangul.engToKor("dkssud tprP!")); console.log(Hangul.engToKor("dkf tn djqtsms answk cjfl : 32암$%@ asakdno")); console.log(Hangul.korToEng("안녕 세계!")); -console.log(Hangul.korToEng("알 수 없는 문자 처리 : 32dka$%@ asakdno")); \ No newline at end of file +console.log(Hangul.korToEng("알 수 없는 문자 처리 : 32dka$%@ asakdno")); + +console.log(Hangul.korToEng(han)); +console.log(Hangul.engToKor(new Hangul("xptmxm"))); \ No newline at end of file