Skip to content

Commit

Permalink
chore: numberToHangul 테스트 커버리지 보강 (#298)
Browse files Browse the repository at this point in the history
* chore: numberToHangul 테스트 커버리지 보강

* 불필요한 로그 제거
  • Loading branch information
po4tion authored Nov 28, 2024
1 parent 4599e57 commit bc2f19b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/numberToHangul/numberToHangul.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ describe('numberToHangul', () => {
expect(numberToHangul(1_234)).toBe('천이백삼십사');
expect(numberToHangul(9_999)).toBe('구천구백구십구');
});

test('유효하지 않은 숫자에 대한 오류 처리', () => {
expect(() => numberToHangul(-1)).toThrow('유효한 0 이상의 정수를 입력해주세요.');
expect(() => numberToHangul(-12345)).toThrow('유효한 0 이상의 정수를 입력해주세요.');
expect(() => numberToHangul(NaN)).toThrow('유효한 0 이상의 정수를 입력해주세요.');
expect(() => numberToHangul(Infinity)).toThrow('유효한 0 이상의 정수를 입력해주세요.');
});
});
9 changes: 4 additions & 5 deletions src/numberToHangul/numberToHangul.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { HANGUL_CARDINAL, HANGUL_DIGITS, HANGUL_NUMBERS } from '@/_internal/constants';

export function numberToHangul(input: number, options?: { spacing?: boolean }): string {
if (!Number.isFinite(input) || Number.isNaN(input) || !Number.isInteger(input) || input < 0) {
throw new Error('유효한 0 이상의 정수를 입력해주세요.');
}

if (input === 0) {
return '영';
}
Expand Down Expand Up @@ -29,10 +33,6 @@ export function numberToHangul(input: number, options?: { spacing?: boolean }):
}

function numberToKoreanUpToThousand(num: number): string {
if (num < 0 || num > 9999) {
throw new Error('0 이상 9999 이하의 숫자만 입력 가능합니다.');
}

const koreanDigits = num
.toString()
.split('')
Expand All @@ -42,5 +42,4 @@ function numberToKoreanUpToThousand(num: number): string {
.join('');

return koreanDigits.replace(/일천/, '천').replace(/일백/, '백').replace(/일십/, '십') || '';

}

0 comments on commit bc2f19b

Please sign in to comment.