From bb354587d9294e540cc658720b0317d4226e0a12 Mon Sep 17 00:00:00 2001 From: Jungwoo LEE Date: Sat, 13 Jul 2024 00:04:00 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20amountToHangul=EC=9D=B4=20=EC=84=A0?= =?UTF-8?q?=ED=96=89=200=EC=9D=B4=20=EC=9E=88=EB=8A=94=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4=EC=9D=84=20=EC=9E=98=EB=AA=BB=20=EB=B3=80?= =?UTF-8?q?=ED=99=98=ED=95=98=EB=8A=94=20=EC=98=A4=EB=A5=98=EB=A5=BC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=ED=95=A9=EB=8B=88=EB=8B=A4.=20(#168)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 호환성을 위해 배열의 마지막 요소 가져오는 부분 수정 * fix: 선행 0도 처리 가능하도록 amountToHangul 수정 * test: 선행 0 처리 테스트 코드 추가 * fix: 변수 네이밍 수정 * Create curvy-bats-nail.md --------- Co-authored-by: 박찬혁 --- .changeset/curvy-bats-nail.md | 5 +++++ src/amountToHangul.spec.ts | 12 +++++++++--- src/amountToHangul.ts | 8 +++++--- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/curvy-bats-nail.md diff --git a/.changeset/curvy-bats-nail.md b/.changeset/curvy-bats-nail.md new file mode 100644 index 00000000..f1031f66 --- /dev/null +++ b/.changeset/curvy-bats-nail.md @@ -0,0 +1,5 @@ +--- +"es-hangul": patch +--- + +fix: amountToHangul이 선행 0이 있는 문자열을 잘못 변환하는 오류를 수정합니다. diff --git a/src/amountToHangul.spec.ts b/src/amountToHangul.spec.ts index f7c47c24..eae4c5ad 100644 --- a/src/amountToHangul.spec.ts +++ b/src/amountToHangul.spec.ts @@ -10,10 +10,10 @@ describe('amountToHangul', () => { }); it('숫자로 된 금액이 80글자를 넘을 시 에러 발생', () => { - const longNumberString = '1'.repeat(81); + const longNumberString = '1'.repeat(81); expect(() => amountToHangul(longNumberString)).toThrowError(`convert range exceeded : ${longNumberString}`); - }) - + }); + it('숫자 외 문자를 무시하여 반환', () => { expect(amountToHangul('120,030원')).toEqual('일십이만삼십'); }); @@ -31,4 +31,10 @@ describe('amountToHangul', () => { expect(amountToHangul(100000100)).toEqual('일억백'); expect(amountToHangul(392.24)).toEqual('삼백구십이점이사'); }); + + it('선행 0이 있는 경우 처리', () => { + expect(amountToHangul('01023')).toEqual('일천이십삼'); + expect(amountToHangul('001023')).toEqual('일천이십삼'); + expect(amountToHangul('0001023')).toEqual('일천이십삼'); + }); }); diff --git a/src/amountToHangul.ts b/src/amountToHangul.ts index 3d70f9d5..ca43b335 100644 --- a/src/amountToHangul.ts +++ b/src/amountToHangul.ts @@ -26,19 +26,21 @@ export const HANGUL_NUMBERS_FOR_DECIMAL = ['영', '일', '이', '삼', '사', ' export const HANGUL_CARDINAL = ['', '십', '백', '천']; export function amountToHangul(amount: string | number) { - const [integerPart, tempDecimalPart] = String(amount) + const [rawIntegerPart, rawDecimalPart] = String(amount) .replace(/[^\d.]+/g, '') .split('.'); + const integerPart = rawIntegerPart !== '0' ? rawIntegerPart.replace(/^0+/, '') : rawIntegerPart; + if (integerPart.length > HANGUL_DIGITS_MAX) { throw new Error(`convert range exceeded : ${amount}`); } - const decimalPart = tempDecimalPart?.replace(/0+$/, ''); + const decimalPart = rawDecimalPart?.replace(/0+$/, ''); const result = []; let pronunDigits = true; - if(integerPart === '0' || (integerPart === '' && tempDecimalPart)) { + if (integerPart === '0' || (integerPart === '' && rawDecimalPart)) { result.push(HANGUL_NUMBERS_FOR_DECIMAL[0]); } else { for (let i = 0; i < integerPart.length - 1; i++) {