Skip to content

Commit 721d3ae

Browse files
authored
Merge pull request #36 from klmhyeonwoo/feature/utils-workspace-yeom
feat: formatPhoneNumber util function
2 parents e0f819c + 93b24f3 commit 721d3ae

5 files changed

Lines changed: 263 additions & 103 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A comprehensive collection of TypeScript utility functions for modern web develo
44

55
## Features
66

7-
- 🛠️ **Comprehensive**: String, object, cookie, number, validation, and common utilities
7+
- 🛠️ **Comprehensive**: String, object, cookie, number, validation, format, and common utilities
88
- 📦 **Tree-shakable**: Import only what you need
99
- 🔒 **Type-safe**: Full TypeScript support with type definitions
1010
-**Lightweight**: Minimal dependencies and optimized for performance
@@ -30,6 +30,7 @@ import {
3030
numberUtil,
3131
validationUtil,
3232
commonUtil,
33+
formatUtil,
3334
} from "kr-corekit";
3435

3536
// String utilities
@@ -69,6 +70,8 @@ await commonUtil.sleep(1000); // Pauses execution for 1 second
6970
// Cookie utilities
7071
cookieUtil.setCookie("theme", "dark");
7172
const theme = cookieUtil.getCookie("theme");
73+
// Format utilities
74+
const formattedPhone = formatUtil.formatPhoneNumber("01012345678"); // "010-1234-5678"
7275
```
7376

7477
## API Reference
@@ -90,6 +93,10 @@ const theme = cookieUtil.getCookie("theme");
9093
- `subtract(...numbers: number[]): number` - Calculates subtraction of numbers
9194
- `multiply(...numbers: number[]): number` - Calculates multiplication of numbers
9295

96+
### FormatUtil
97+
98+
- `formatPhoneNumber(phone: string): string` - Formats a phone number string to a standard format (e.g., "010-1234-5678")
99+
93100
### ValidationUtil
94101

95102
- `checkEmail(email: string): boolean` - Validates email format
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, expect, test } from "vitest";
2+
import formatPhoneNumber from ".";
3+
4+
describe("formatPhoneNumber", () => {
5+
describe("휴대폰 번호", () => {
6+
test("11자리 휴대폰 번호에 하이픈을 올바르게 추가한다 (010)", () => {
7+
expect(formatPhoneNumber("01012345678")).toBe("010-1234-5678");
8+
});
9+
10+
test("11자리 휴대폰 번호에 하이픈을 올바르게 추가한다 (011)", () => {
11+
expect(formatPhoneNumber("01198765432")).toBe("011-9876-5432");
12+
});
13+
14+
test("중간에 공백이나 하이픈이 있어도 숫자를 제외하고 올바르게 처리한다", () => {
15+
expect(formatPhoneNumber("010-1234 5678")).toBe("010-1234-5678");
16+
});
17+
});
18+
19+
describe("지역번호", () => {
20+
test("서울 지역번호(02) 9자리에 하이픈을 올바르게 추가한다", () => {
21+
expect(formatPhoneNumber("021234567")).toBe("02-123-4567");
22+
});
23+
24+
test("서울 지역번호(02) 10자리에 하이픈을 올바르게 추가한다", () => {
25+
expect(formatPhoneNumber("0212345678")).toBe("02-1234-5678");
26+
});
27+
28+
test("서울 외 지역번호(031) 10자리에 하이픈을 올바르게 추가한다", () => {
29+
expect(formatPhoneNumber("0311234567")).toBe("031-123-4567");
30+
});
31+
32+
test("서울 외 지역번호(054) 10자리에 하이픈을 올바르게 추가한다", () => {
33+
expect(formatPhoneNumber("0541234567")).toBe("054-123-4567");
34+
});
35+
});
36+
37+
describe("대표번호", () => {
38+
test("8자리 대표번호(1588)에 하이픈을 올바르게 추가한다", () => {
39+
expect(formatPhoneNumber("15881234")).toBe("1588-1234");
40+
});
41+
42+
test("8자리 대표번호(1670)에 하이픈을 올바르게 추가한다", () => {
43+
expect(formatPhoneNumber("16709876")).toBe("1670-9876");
44+
});
45+
});
46+
47+
describe("예외 케이스", () => {
48+
test("빈 문자열을 입력하면 빈 문자열을 반환한다", () => {
49+
expect(formatPhoneNumber("")).toBe("");
50+
});
51+
52+
test("null 또는 undefined를 입력하면 빈 문자열을 반환한다", () => {
53+
// @ts-ignore
54+
expect(formatPhoneNumber(null)).toBe("");
55+
// @ts-ignore
56+
expect(formatPhoneNumber(undefined)).toBe("");
57+
});
58+
59+
test("숫자가 아닌 문자가 포함된 경우, 해당 문자를 제거하고 포맷팅한다", () => {
60+
expect(formatPhoneNumber("010-abcd-1234-efg-5678")).toBe("010-1234-5678");
61+
});
62+
63+
test("어떤 형식에도 맞지 않는 짧은 길이라면 숫자만 반환한다", () => {
64+
expect(formatPhoneNumber("12345")).toBe("12345");
65+
});
66+
67+
test("어떤 형식에도 맞지 않는 긴 길이라면 숫자만 반환한다", () => {
68+
expect(formatPhoneNumber("0101234567890")).toBe("0101234567890");
69+
});
70+
});
71+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 전화번호 형식 규칙을 데이터로 정의합니다.
2+
const formatRules = [
3+
// 서울 지역번호 (02) - 9자리
4+
{
5+
prefix: "02",
6+
length: 9,
7+
format: /(\d{2})(\d{3})(\d{4})/,
8+
replacement: "$1-$2-$3",
9+
},
10+
// 서울 지역번호 (02) - 10자리
11+
{
12+
prefix: "02",
13+
length: 10,
14+
format: /(\d{2})(\d{4})(\d{4})/,
15+
replacement: "$1-$2-$3",
16+
},
17+
// 8자리 대표번호 (1588, 1670 등)
18+
{
19+
length: 8,
20+
format: /(\d{4})(\d{4})/,
21+
replacement: "$1-$2",
22+
},
23+
// 일반 지역번호 (031, 054 등)
24+
{
25+
length: 10,
26+
format: /(\d{3})(\d{3})(\d{4})/,
27+
replacement: "$1-$2-$3",
28+
},
29+
// 휴대폰 번호
30+
{
31+
length: 11,
32+
format: /(\d{3})(\d{4})(\d{4})/,
33+
replacement: "$1-$2-$3",
34+
},
35+
];
36+
37+
export default function formatPhoneNumber(phoneNumber: string): string {
38+
if (!phoneNumber) return "";
39+
40+
const digitsOnly = phoneNumber.replace(/\D/g, "");
41+
42+
const matched = formatRules.find(
43+
(rule) =>
44+
(!rule.prefix || digitsOnly.startsWith(rule.prefix)) &&
45+
digitsOnly.length === rule.length
46+
);
47+
48+
return matched
49+
? digitsOnly.replace(matched.format, matched.replacement)
50+
: digitsOnly;
51+
}

package/formatUtil/index.ts

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

0 commit comments

Comments
 (0)