Skip to content

Commit b7e12ac

Browse files
authored
feat: Add format number utility (#70)
* feat: add formatNumberWithCommas utility function and tests * feat: add formatNumberWithCommas description to README
1 parent bda7575 commit b7e12ac

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ storage.set("data", { key: "value" });
166166
### FormatUtil
167167

168168
- `formatPhoneNumber(phone: string): string` - Formats a phone number string to a standard format (e.g., "010-1234-5678")
169+
- `formatNumberWithCommas(value: number | string | null | undefined): string` - Converts numbers or strings to comma-separated format (e.g., "1,234,567"). Returns empty string for null/undefined values.
169170

170171
### ValidationUtil
171172

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expect, test } from "vitest";
2+
import formatNumberWithCommas from ".";
3+
4+
describe("formatNumberWithCommas", () => {
5+
test("네 자리 정수를 올바르게 포맷팅해야 합니다.", () => {
6+
expect(formatNumberWithCommas(1234)).toBe("1,234");
7+
});
8+
9+
test("세 자리 정수는 콤마 없이 그대로 반환해야 합니다.", () => {
10+
expect(formatNumberWithCommas(999)).toBe("999");
11+
});
12+
13+
test('숫자 0을 올바르게 "0"으로 반환해야 합니다.', () => {
14+
expect(formatNumberWithCommas(0)).toBe("0");
15+
});
16+
17+
test("숫자 입력 시 3자리마다 콤마가 포함된 문자열을 반환한다", () => {
18+
expect(formatNumberWithCommas(1234567)).toBe("1,234,567");
19+
expect(formatNumberWithCommas(-12345.67)).toBe("-12,345.67");
20+
});
21+
22+
test("null 입력 시 빈 문자열을 반환해야 합니다.", () => {
23+
expect(formatNumberWithCommas(null)).toBe("");
24+
});
25+
26+
test("undefined 입력 시 빈 문자열을 반환해야 합니다.", () => {
27+
expect(formatNumberWithCommas(undefined)).toBe("");
28+
});
29+
30+
test("빈 문자열 입력 시 빈 문자열을 반환해야 합니다.", () => {
31+
expect(formatNumberWithCommas("")).toBe("");
32+
});
33+
34+
test("숫자로 변환할 수 없는 문자열은 그대로 반환해야 합니다.", () => {
35+
expect(formatNumberWithCommas("안녕하세요")).toBe("안녕하세요");
36+
});
37+
38+
test("이미 콤마가 포함된 문자열은 (NaN으로 처리되어) 그대로 반환해야 합니다.", () => {
39+
expect(formatNumberWithCommas("12,345")).toBe("12,345");
40+
});
41+
42+
test('NaN 값을 직접 입력하면 "NaN" 문자열을 반환해야 합니다.', () => {
43+
expect(formatNumberWithCommas(NaN)).toBe("NaN");
44+
});
45+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 숫자나 문자열을 3자리마다 콤마(,)가 포함된 문자열로 변환합니다.
3+
* null, undefined, 또는 유효하지 않은 값이 들어오면 빈 문자열을 반환합니다.
4+
*
5+
* @param value - 콤마를 추가할 숫자 또는 문자열
6+
* @returns 콤마가 추가된 문자열
7+
*
8+
* @example
9+
* formatNumberWithCommas(1234567); // "1,234,567"
10+
* formatNumberWithCommas(-12345.67); // "-12,345.67"
11+
* formatNumberWithCommas("12345"); // "12,345"
12+
*/
13+
export default function formatNumberWithCommas(
14+
value: number | string | null | undefined
15+
): string {
16+
if (value === null || value === undefined || value === "") {
17+
return "";
18+
}
19+
20+
const num = Number(value);
21+
22+
if (isNaN(num)) {
23+
return String(value);
24+
}
25+
26+
return num.toLocaleString("en-US");
27+
}

package/formatUtil/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as formatPhoneNumber } from "./formatPhoneNumber";
2+
export { default as formatNumberWithCommas } from "./formatNumberWithCommas";

0 commit comments

Comments
 (0)