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

feat: 영문 알파벳을 qwerty 자판에 매칭되는 한글로 변환하는 함수 추가 #68

Merged
merged 11 commits into from
Apr 23, 2024
5 changes: 5 additions & 0 deletions .changeset/slimy-kiwis-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

feat: 영문 알파벳을 qwerty 자판에 매칭되는 한글로 변환하는 함수 추가
58 changes: 58 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,61 @@ export const HANGUL_CHARACTERS_BY_LAST_INDEX = (
'ㅎ',
] as const
).map(consonant => DISASSEMBLED_CONSONANTS_BY_CONSONANT[consonant]);

/**
* qwerty 키보드 자판의 대소문자를 구분한 영어 알파벳을 한글 음소와 맵핑한 객체
*/
export const QWERTY_KEYBOARD_MAP = {
q: 'ㅂ',
Q: 'ㅃ',
w: 'ㅈ',
W: 'ㅉ',
e: 'ㄷ',
E: 'ㄸ',
r: 'ㄱ',
R: 'ㄲ',
t: 'ㅅ',
T: 'ㅆ',
y: 'ㅛ',
Y: 'ㅛ',
u: 'ㅕ',
U: 'ㅕ',
i: 'ㅑ',
I: 'ㅑ',
o: 'ㅐ',
O: 'ㅒ',
p: 'ㅔ',
P: 'ㅖ',
a: 'ㅁ',
A: 'ㅁ',
s: 'ㄴ',
S: 'ㄴ',
d: 'ㅇ',
D: 'ㅇ',
f: 'ㄹ',
F: 'ㄹ',
g: 'ㅎ',
G: 'ㅎ',
h: 'ㅗ',
H: 'ㅗ',
j: 'ㅓ',
J: 'ㅓ',
k: 'ㅏ',
K: 'ㅏ',
l: 'ㅣ',
L: 'ㅣ',
z: 'ㅋ',
Z: 'ㅋ',
x: 'ㅌ',
X: 'ㅌ',
c: 'ㅊ',
C: 'ㅊ',
v: 'ㅍ',
V: 'ㅍ',
b: 'ㅠ',
B: 'ㅠ',
n: 'ㅜ',
N: 'ㅜ',
m: 'ㅡ',
M: 'ㅡ',
} as const;
62 changes: 62 additions & 0 deletions src/convertQwertyToHangulAlphabet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, it } from 'vitest';
import { convertQwertyToHangul, convertQwertyToHangulAlphabet } from './convertQwertyToHangulAlphabet';

describe('convertQwertyToHangulAlphabet', () => {
it('영어 알파벳을 한글 음소로 바꾼다.', () => {
expect(convertQwertyToHangulAlphabet('abc')).toBe('ㅁㅠㅊ');
});

it('쌍/자모음에 대응하지 않는 영어 알파벳을 한글 음소로 바꾼다.', () => {
expect(convertQwertyToHangulAlphabet('ABC')).toBe('ㅁㅠㅊ');
});

it('영어 알파벳은 한글 음소로 바꾸고, 한글 음절은 유지한다.', () => {
expect(convertQwertyToHangulAlphabet('vm론트')).toBe('ㅍㅡ론트');
});

it('분해된 한글 음소는 유지한다.', () => {
expect(convertQwertyToHangulAlphabet('ㅍㅡㄹㅗㄴㅌㅡ')).toBe('ㅍㅡㄹㅗㄴㅌㅡ');
});

it('영어 알파벳이 아닌 입력은 유지한다.', () => {
expect(convertQwertyToHangulAlphabet('4월/20dlf!')).toBe('4월/20ㅇㅣㄹ!');
});

it('영문 대문자는 쌍자/모음으로 바꾼다.', () => {
expect(convertQwertyToHangulAlphabet('RㅏㄱEㅜrl')).toBe('ㄲㅏㄱㄸㅜㄱㅣ');
expect(convertQwertyToHangulAlphabet('ㅇPdml')).toBe('ㅇㅖㅇㅡㅣ');
});

it('빈 문자열은 빈 문자열을 반환한다.', () => {
expect(convertQwertyToHangulAlphabet('')).toBe('');
});
});

describe('convertQwertyToHangul', () => {
it('영어 알파벳을 한글로 합성한다.', () => {
expect(convertQwertyToHangul('abc')).toBe('뮻');
expect(convertQwertyToHangul('vmfhsxmdpsem')).toBe('프론트엔드');
});

it('쌍/자모음에 대응하지 않는 영어 알파벳을 한글로 합성한다.', () => {
expect(convertQwertyToHangul('ABC')).toBe('뮻');
expect(convertQwertyToHangul('VMFHSXM')).toBe('프론트');
});

it('영어 알파벳은 한글로 합성하고, 한글 음절은 유지한다.', () => {
expect(convertQwertyToHangul('vm론트')).toBe('프론트');
});

it('인자가 한글 음소면 한글로 합성한다.', () => {
expect(convertQwertyToHangul('ㅍㅡㄹㅗㄴㅌㅡ')).toBe('프론트');
});

it('영문 대문자는 쌍자/모음에 대응하며 한글로 합성한다.', () => {
expect(convertQwertyToHangul('RㅏㄱEㅜrl')).toBe('깍뚜기');
expect(convertQwertyToHangul('ㅇPdml')).toBe('예의');
});

it('빈 문자열은 빈 문자열을 반환한다.', () => {
expect(convertQwertyToHangul('')).toBe('');
});
});
31 changes: 31 additions & 0 deletions src/convertQwertyToHangulAlphabet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assembleHangul } from './assemble';
import { QWERTY_KEYBOARD_MAP } from './constants';
import { hasProperty } from './utils';

/**
* @name convertQwertyToHangulAlphabet
* @description
* 영어 알파벳을 qwerty 자판과 매칭되는 한글 음소로 변환합니다.
* @param word 한글 음소로 변환하고자 하는 영문
* @returns 영어 알파벳이 포함되지 않은 한글 음소, 음절, 기타 문자로 이루어진 문자열
*/
export function convertQwertyToHangulAlphabet(word: string): string {
return word
.split('')
.map(inputText => (hasProperty(QWERTY_KEYBOARD_MAP, inputText) ? QWERTY_KEYBOARD_MAP[inputText] : inputText))
.join('');
}

/**
* @name convertQwertyToHangul
* @description
* 영어 알파벳을 qwerty 자판과 매칭과는 한글 문자와 문장으로 변환합니다.
* @param word 한글 문장으로 변환하고자 하는 영문
* @returns qwerty 영어 알파벳을 변환하여 한글 규칙에 맞게 합성한 문자열
*/
export function convertQwertyToHangul(word: string): string {
if (!word) {
return '';
}
return assembleHangul([...convertQwertyToHangulAlphabet(word).split('')]);
}