goodchuck-utils 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/date/index.d.ts +64 -0
- package/dist/date/index.d.ts.map +1 -0
- package/dist/date/index.js +92 -0
- package/dist/date/index.test.d.ts +2 -0
- package/dist/date/index.test.d.ts.map +1 -0
- package/dist/date/index.test.js +166 -0
- package/dist/form/__tests__/formatter.test.d.ts +2 -0
- package/dist/form/__tests__/formatter.test.d.ts.map +1 -0
- package/dist/form/__tests__/formatter.test.js +74 -0
- package/dist/form/__tests__/helpers.test.d.ts +2 -0
- package/dist/form/__tests__/helpers.test.d.ts.map +1 -0
- package/dist/form/__tests__/helpers.test.js +42 -0
- package/dist/form/__tests__/validation.test.d.ts +2 -0
- package/dist/form/__tests__/validation.test.d.ts.map +1 -0
- package/dist/form/__tests__/validation.test.js +67 -0
- package/dist/form/formatter.d.ts +34 -0
- package/dist/form/formatter.d.ts.map +1 -0
- package/dist/form/formatter.js +76 -0
- package/dist/form/helpers.d.ts +16 -0
- package/dist/form/helpers.d.ts.map +1 -0
- package/dist/form/helpers.js +34 -0
- package/dist/form/index.d.ts +9 -0
- package/dist/form/index.d.ts.map +1 -0
- package/dist/form/index.js +11 -0
- package/dist/form/validation.d.ts +33 -0
- package/dist/form/validation.d.ts.map +1 -0
- package/dist/form/validation.js +56 -0
- package/dist/hooks/index.d.ts +11 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +12 -0
- package/dist/hooks/useClickOutside.d.ts +49 -0
- package/dist/hooks/useClickOutside.d.ts.map +1 -0
- package/dist/hooks/useClickOutside.js +94 -0
- package/dist/hooks/useLocalStorage.d.ts +19 -0
- package/dist/hooks/useLocalStorage.d.ts.map +1 -0
- package/dist/hooks/useLocalStorage.js +91 -0
- package/dist/hooks/useMediaQuery.d.ts +56 -0
- package/dist/hooks/useMediaQuery.d.ts.map +1 -0
- package/dist/hooks/useMediaQuery.js +104 -0
- package/dist/hooks/useSessionStorage.d.ts +19 -0
- package/dist/hooks/useSessionStorage.d.ts.map +1 -0
- package/dist/hooks/useSessionStorage.js +85 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -18
- package/dist/string/__tests__/case.test.d.ts +2 -0
- package/dist/string/__tests__/case.test.d.ts.map +1 -0
- package/dist/string/__tests__/case.test.js +61 -0
- package/dist/string/__tests__/manipulation.test.d.ts +2 -0
- package/dist/string/__tests__/manipulation.test.d.ts.map +1 -0
- package/dist/string/__tests__/manipulation.test.js +109 -0
- package/dist/string/__tests__/validation.test.d.ts +2 -0
- package/dist/string/__tests__/validation.test.d.ts.map +1 -0
- package/dist/string/__tests__/validation.test.js +101 -0
- package/dist/string/case.d.ts +42 -0
- package/dist/string/case.d.ts.map +1 -0
- package/dist/string/case.js +71 -0
- package/dist/string/index.d.ts +9 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +11 -0
- package/dist/string/manipulation.d.ts +61 -0
- package/dist/string/manipulation.d.ts.map +1 -0
- package/dist/string/manipulation.js +106 -0
- package/dist/string/validation.d.ts +79 -0
- package/dist/string/validation.d.ts.map +1 -0
- package/dist/string/validation.js +115 -0
- package/package.json +27 -3
- package/src/date/index.test.ts +206 -0
- package/src/date/index.ts +123 -0
- package/src/form/__tests__/formatter.test.ts +97 -0
- package/src/form/__tests__/helpers.test.ts +53 -0
- package/src/form/__tests__/validation.test.ts +84 -0
- package/src/form/formatter.ts +85 -0
- package/src/form/helpers.ts +44 -0
- package/src/form/index.ts +14 -0
- package/src/form/validation.ts +72 -0
- package/src/hooks/index.ts +14 -0
- package/src/hooks/useClickOutside.ts +114 -0
- package/src/hooks/useLocalStorage.ts +112 -0
- package/src/hooks/useMediaQuery.ts +116 -0
- package/src/hooks/useSessionStorage.ts +106 -0
- package/src/index.ts +14 -13
- package/src/string/__tests__/case.test.ts +78 -0
- package/src/string/__tests__/manipulation.test.ts +142 -0
- package/src/string/__tests__/validation.test.ts +128 -0
- package/src/string/case.ts +76 -0
- package/src/string/index.ts +14 -0
- package/src/string/manipulation.ts +124 -0
- package/src/string/validation.ts +126 -0
- package/tsconfig.json +15 -11
- package/vitest.config.ts +13 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import 'dayjs/locale/ko';
|
|
3
|
+
/**
|
|
4
|
+
* 날짜를 지정된 포맷으로 변환
|
|
5
|
+
* @param date - 변환할 날짜 (Date, string, number)
|
|
6
|
+
* @param format - 포맷 문자열 (기본값: 'YYYY-MM-DD HH:mm:ss')
|
|
7
|
+
*/
|
|
8
|
+
export declare function formatDate(date?: Date | string | number, format?: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* 현재 날짜/시간 반환
|
|
11
|
+
* @param format - 포맷 문자열 (선택)
|
|
12
|
+
*/
|
|
13
|
+
export declare function now(format?: string): string | Date;
|
|
14
|
+
/**
|
|
15
|
+
* 상대 시간 반환 (예: "3시간 전")
|
|
16
|
+
* @param date - 기준 날짜
|
|
17
|
+
* @param locale - 로케일 (기본값: 'ko')
|
|
18
|
+
*/
|
|
19
|
+
export declare function fromNow(date: Date | string | number, locale?: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* 두 날짜 사이의 차이 계산
|
|
22
|
+
* @param date1 - 첫 번째 날짜
|
|
23
|
+
* @param date2 - 두 번째 날짜
|
|
24
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
25
|
+
*/
|
|
26
|
+
export declare function diffDate(date1: Date | string | number, date2: Date | string | number, unit?: dayjs.OpUnitType): number;
|
|
27
|
+
/**
|
|
28
|
+
* 날짜에 시간 더하기
|
|
29
|
+
* @param date - 기준 날짜
|
|
30
|
+
* @param value - 더할 값
|
|
31
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
32
|
+
*/
|
|
33
|
+
export declare function addDate(date: Date | string | number, value: number, unit?: dayjs.ManipulateType): Date;
|
|
34
|
+
/**
|
|
35
|
+
* 날짜에 시간 빼기
|
|
36
|
+
* @param date - 기준 날짜
|
|
37
|
+
* @param value - 뺄 값
|
|
38
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
39
|
+
*/
|
|
40
|
+
export declare function subtractDate(date: Date | string | number, value: number, unit?: dayjs.ManipulateType): Date;
|
|
41
|
+
/**
|
|
42
|
+
* 날짜가 특정 날짜보다 이전인지 확인
|
|
43
|
+
*/
|
|
44
|
+
export declare function isBefore(date1: Date | string | number, date2: Date | string | number): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 날짜가 특정 날짜보다 이후인지 확인
|
|
47
|
+
*/
|
|
48
|
+
export declare function isAfter(date1: Date | string | number, date2: Date | string | number): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* 두 날짜가 같은지 확인
|
|
51
|
+
* @param unit - 비교 단위 (기본값: 'millisecond')
|
|
52
|
+
*/
|
|
53
|
+
export declare function isSame(date1: Date | string | number, date2: Date | string | number, unit?: dayjs.OpUnitType): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 날짜가 유효한지 확인
|
|
56
|
+
*/
|
|
57
|
+
export declare function isValidDate(date: any): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* 타임존 변환
|
|
60
|
+
* @param date - 변환할 날짜
|
|
61
|
+
* @param tz - 타임존 (예: 'Asia/Seoul')
|
|
62
|
+
*/
|
|
63
|
+
export declare function toTimezone(date: Date | string | number, tz: string): Date;
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/date/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,OAAO,iBAAiB,CAAC;AAMzB;;;;GAIG;AACH,wBAAgB,UAAU,CACxB,IAAI,GAAE,IAAI,GAAG,MAAM,GAAG,MAAmB,EACzC,MAAM,GAAE,MAA8B,GACrC,MAAM,CAER;AAED;;;GAGG;AACH,wBAAgB,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGlD;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,MAAM,GAAE,MAAa,GAAG,MAAM,CAEnF;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC7B,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC7B,IAAI,GAAE,KAAK,CAAC,UAAkB,GAC7B,MAAM,CAER;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,KAAK,CAAC,cAAsB,GACjC,IAAI,CAEN;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,KAAK,CAAC,cAAsB,GACjC,IAAI,CAEN;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAE9F;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAE7F;AAED;;;GAGG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC7B,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC7B,IAAI,GAAE,KAAK,CAAC,UAA0B,GACrC,OAAO,CAET;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAE9C;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI,CAEzE"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import utc from 'dayjs/plugin/utc';
|
|
3
|
+
import timezone from 'dayjs/plugin/timezone';
|
|
4
|
+
import relativeTime from 'dayjs/plugin/relativeTime';
|
|
5
|
+
import 'dayjs/locale/ko';
|
|
6
|
+
dayjs.extend(utc);
|
|
7
|
+
dayjs.extend(timezone);
|
|
8
|
+
dayjs.extend(relativeTime);
|
|
9
|
+
/**
|
|
10
|
+
* 날짜를 지정된 포맷으로 변환
|
|
11
|
+
* @param date - 변환할 날짜 (Date, string, number)
|
|
12
|
+
* @param format - 포맷 문자열 (기본값: 'YYYY-MM-DD HH:mm:ss')
|
|
13
|
+
*/
|
|
14
|
+
export function formatDate(date = new Date(), format = 'YYYY-MM-DD HH:mm:ss') {
|
|
15
|
+
return dayjs(date).format(format);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 현재 날짜/시간 반환
|
|
19
|
+
* @param format - 포맷 문자열 (선택)
|
|
20
|
+
*/
|
|
21
|
+
export function now(format) {
|
|
22
|
+
const current = dayjs();
|
|
23
|
+
return format ? current.format(format) : current.toDate();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 상대 시간 반환 (예: "3시간 전")
|
|
27
|
+
* @param date - 기준 날짜
|
|
28
|
+
* @param locale - 로케일 (기본값: 'ko')
|
|
29
|
+
*/
|
|
30
|
+
export function fromNow(date, locale = 'ko') {
|
|
31
|
+
return dayjs(date).locale(locale).fromNow();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 두 날짜 사이의 차이 계산
|
|
35
|
+
* @param date1 - 첫 번째 날짜
|
|
36
|
+
* @param date2 - 두 번째 날짜
|
|
37
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
38
|
+
*/
|
|
39
|
+
export function diffDate(date1, date2, unit = 'day') {
|
|
40
|
+
return dayjs(date1).diff(dayjs(date2), unit);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 날짜에 시간 더하기
|
|
44
|
+
* @param date - 기준 날짜
|
|
45
|
+
* @param value - 더할 값
|
|
46
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
47
|
+
*/
|
|
48
|
+
export function addDate(date, value, unit = 'day') {
|
|
49
|
+
return dayjs(date).add(value, unit).toDate();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 날짜에 시간 빼기
|
|
53
|
+
* @param date - 기준 날짜
|
|
54
|
+
* @param value - 뺄 값
|
|
55
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
56
|
+
*/
|
|
57
|
+
export function subtractDate(date, value, unit = 'day') {
|
|
58
|
+
return dayjs(date).subtract(value, unit).toDate();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 날짜가 특정 날짜보다 이전인지 확인
|
|
62
|
+
*/
|
|
63
|
+
export function isBefore(date1, date2) {
|
|
64
|
+
return dayjs(date1).isBefore(dayjs(date2));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 날짜가 특정 날짜보다 이후인지 확인
|
|
68
|
+
*/
|
|
69
|
+
export function isAfter(date1, date2) {
|
|
70
|
+
return dayjs(date1).isAfter(dayjs(date2));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 두 날짜가 같은지 확인
|
|
74
|
+
* @param unit - 비교 단위 (기본값: 'millisecond')
|
|
75
|
+
*/
|
|
76
|
+
export function isSame(date1, date2, unit = 'millisecond') {
|
|
77
|
+
return dayjs(date1).isSame(dayjs(date2), unit);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 날짜가 유효한지 확인
|
|
81
|
+
*/
|
|
82
|
+
export function isValidDate(date) {
|
|
83
|
+
return dayjs(date).isValid();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 타임존 변환
|
|
87
|
+
* @param date - 변환할 날짜
|
|
88
|
+
* @param tz - 타임존 (예: 'Asia/Seoul')
|
|
89
|
+
*/
|
|
90
|
+
export function toTimezone(date, tz) {
|
|
91
|
+
return dayjs(date).tz(tz).toDate();
|
|
92
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/date/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { formatDate, now, fromNow, diffDate, addDate, subtractDate, isBefore, isAfter, isSame, isValidDate, toTimezone, } from './index';
|
|
3
|
+
describe('Date Utils', () => {
|
|
4
|
+
describe('formatDate', () => {
|
|
5
|
+
it('should format date with default format', () => {
|
|
6
|
+
const date = new Date('2024-01-15 10:30:45');
|
|
7
|
+
const result = formatDate(date);
|
|
8
|
+
expect(result).toBe('2024-01-15 10:30:45');
|
|
9
|
+
});
|
|
10
|
+
it('should format date with custom format', () => {
|
|
11
|
+
const date = new Date('2024-01-15');
|
|
12
|
+
const result = formatDate(date, 'YYYY년 MM월 DD일');
|
|
13
|
+
expect(result).toBe('2024년 01월 15일');
|
|
14
|
+
});
|
|
15
|
+
it('should format string date', () => {
|
|
16
|
+
const result = formatDate('2024-01-15', 'YYYY-MM-DD');
|
|
17
|
+
expect(result).toBe('2024-01-15');
|
|
18
|
+
});
|
|
19
|
+
it('should format timestamp', () => {
|
|
20
|
+
const timestamp = new Date('2024-01-15').getTime();
|
|
21
|
+
const result = formatDate(timestamp, 'YYYY-MM-DD');
|
|
22
|
+
expect(result).toBe('2024-01-15');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe('now', () => {
|
|
26
|
+
it('should return formatted current date when format is provided', () => {
|
|
27
|
+
const result = now('YYYY-MM-DD');
|
|
28
|
+
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
29
|
+
});
|
|
30
|
+
it('should return Date object when no format is provided', () => {
|
|
31
|
+
const result = now();
|
|
32
|
+
expect(result).toBeInstanceOf(Date);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('fromNow', () => {
|
|
36
|
+
it('should return relative time in Korean', () => {
|
|
37
|
+
const yesterday = new Date();
|
|
38
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
39
|
+
const result = fromNow(yesterday, 'ko');
|
|
40
|
+
expect(result).toContain('전');
|
|
41
|
+
});
|
|
42
|
+
it('should return relative time in English', () => {
|
|
43
|
+
const yesterday = new Date();
|
|
44
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
45
|
+
const result = fromNow(yesterday, 'en');
|
|
46
|
+
expect(result).toContain('ago');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
describe('diffDate', () => {
|
|
50
|
+
it('should calculate difference in days', () => {
|
|
51
|
+
const date1 = new Date('2024-01-20');
|
|
52
|
+
const date2 = new Date('2024-01-15');
|
|
53
|
+
const result = diffDate(date1, date2, 'day');
|
|
54
|
+
expect(result).toBe(5);
|
|
55
|
+
});
|
|
56
|
+
it('should calculate difference in hours', () => {
|
|
57
|
+
const date1 = new Date('2024-01-15 15:00:00');
|
|
58
|
+
const date2 = new Date('2024-01-15 12:00:00');
|
|
59
|
+
const result = diffDate(date1, date2, 'hour');
|
|
60
|
+
expect(result).toBe(3);
|
|
61
|
+
});
|
|
62
|
+
it('should handle negative differences', () => {
|
|
63
|
+
const date1 = new Date('2024-01-15');
|
|
64
|
+
const date2 = new Date('2024-01-20');
|
|
65
|
+
const result = diffDate(date1, date2, 'day');
|
|
66
|
+
expect(result).toBe(-5);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe('addDate', () => {
|
|
70
|
+
it('should add days to date', () => {
|
|
71
|
+
const date = new Date('2024-01-15');
|
|
72
|
+
const result = addDate(date, 5, 'day');
|
|
73
|
+
expect(formatDate(result, 'YYYY-MM-DD')).toBe('2024-01-20');
|
|
74
|
+
});
|
|
75
|
+
it('should add hours to date', () => {
|
|
76
|
+
const date = new Date('2024-01-15 10:00:00');
|
|
77
|
+
const result = addDate(date, 3, 'hour');
|
|
78
|
+
expect(formatDate(result, 'YYYY-MM-DD HH:mm:ss')).toBe('2024-01-15 13:00:00');
|
|
79
|
+
});
|
|
80
|
+
it('should add months to date', () => {
|
|
81
|
+
const date = new Date('2024-01-15');
|
|
82
|
+
const result = addDate(date, 2, 'month');
|
|
83
|
+
expect(formatDate(result, 'YYYY-MM-DD')).toBe('2024-03-15');
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe('subtractDate', () => {
|
|
87
|
+
it('should subtract days from date', () => {
|
|
88
|
+
const date = new Date('2024-01-20');
|
|
89
|
+
const result = subtractDate(date, 5, 'day');
|
|
90
|
+
expect(formatDate(result, 'YYYY-MM-DD')).toBe('2024-01-15');
|
|
91
|
+
});
|
|
92
|
+
it('should subtract hours from date', () => {
|
|
93
|
+
const date = new Date('2024-01-15 13:00:00');
|
|
94
|
+
const result = subtractDate(date, 3, 'hour');
|
|
95
|
+
expect(formatDate(result, 'YYYY-MM-DD HH:mm:ss')).toBe('2024-01-15 10:00:00');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
describe('isBefore', () => {
|
|
99
|
+
it('should return true if first date is before second', () => {
|
|
100
|
+
const date1 = new Date('2024-01-15');
|
|
101
|
+
const date2 = new Date('2024-01-20');
|
|
102
|
+
expect(isBefore(date1, date2)).toBe(true);
|
|
103
|
+
});
|
|
104
|
+
it('should return false if first date is after second', () => {
|
|
105
|
+
const date1 = new Date('2024-01-20');
|
|
106
|
+
const date2 = new Date('2024-01-15');
|
|
107
|
+
expect(isBefore(date1, date2)).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe('isAfter', () => {
|
|
111
|
+
it('should return true if first date is after second', () => {
|
|
112
|
+
const date1 = new Date('2024-01-20');
|
|
113
|
+
const date2 = new Date('2024-01-15');
|
|
114
|
+
expect(isAfter(date1, date2)).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
it('should return false if first date is before second', () => {
|
|
117
|
+
const date1 = new Date('2024-01-15');
|
|
118
|
+
const date2 = new Date('2024-01-20');
|
|
119
|
+
expect(isAfter(date1, date2)).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe('isSame', () => {
|
|
123
|
+
it('should return true for exact same dates', () => {
|
|
124
|
+
const date1 = new Date('2024-01-15 10:30:45');
|
|
125
|
+
const date2 = new Date('2024-01-15 10:30:45');
|
|
126
|
+
expect(isSame(date1, date2)).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
it('should return true for same day (ignoring time)', () => {
|
|
129
|
+
const date1 = new Date('2024-01-15 10:00:00');
|
|
130
|
+
const date2 = new Date('2024-01-15 15:00:00');
|
|
131
|
+
expect(isSame(date1, date2, 'day')).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
it('should return false for different days', () => {
|
|
134
|
+
const date1 = new Date('2024-01-15');
|
|
135
|
+
const date2 = new Date('2024-01-16');
|
|
136
|
+
expect(isSame(date1, date2, 'day')).toBe(false);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe('isValidDate', () => {
|
|
140
|
+
it('should return true for valid dates', () => {
|
|
141
|
+
expect(isValidDate(new Date('2024-01-15'))).toBe(true);
|
|
142
|
+
expect(isValidDate('2024-01-15')).toBe(true);
|
|
143
|
+
expect(isValidDate(1705276800000)).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
it('should return false for invalid dates', () => {
|
|
146
|
+
expect(isValidDate('invalid-date')).toBe(false);
|
|
147
|
+
expect(isValidDate(NaN)).toBe(false);
|
|
148
|
+
expect(isValidDate(null)).toBe(false);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
describe('toTimezone', () => {
|
|
152
|
+
it('should convert to specific timezone', () => {
|
|
153
|
+
const date = new Date('2024-01-15T00:00:00Z');
|
|
154
|
+
const result = toTimezone(date, 'Asia/Seoul');
|
|
155
|
+
expect(result).toBeInstanceOf(Date);
|
|
156
|
+
});
|
|
157
|
+
it('should handle different timezones', () => {
|
|
158
|
+
const date = new Date('2024-01-15T12:00:00Z');
|
|
159
|
+
const nyTime = toTimezone(date, 'America/New_York');
|
|
160
|
+
const seoulTime = toTimezone(date, 'Asia/Seoul');
|
|
161
|
+
expect(nyTime).toBeInstanceOf(Date);
|
|
162
|
+
expect(seoulTime).toBeInstanceOf(Date);
|
|
163
|
+
expect(nyTime.getTime()).toBe(seoulTime.getTime());
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.test.d.ts","sourceRoot":"","sources":["../../../src/form/__tests__/formatter.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { formatPhoneNumber, formatBusinessNumber, formatCreditCard, extractNumbers, formatCurrency, maskResidentNumber, maskCreditCard, } from '../formatter';
|
|
3
|
+
describe('Form Formatters', () => {
|
|
4
|
+
describe('formatPhoneNumber', () => {
|
|
5
|
+
it('should format phone numbers correctly', () => {
|
|
6
|
+
expect(formatPhoneNumber('01012345678')).toBe('010-1234-5678');
|
|
7
|
+
expect(formatPhoneNumber('0212345678')).toBe('021-2345-678');
|
|
8
|
+
expect(formatPhoneNumber('010')).toBe('010');
|
|
9
|
+
expect(formatPhoneNumber('0101234')).toBe('010-1234');
|
|
10
|
+
});
|
|
11
|
+
it('should handle already formatted numbers', () => {
|
|
12
|
+
expect(formatPhoneNumber('010-1234-5678')).toBe('010-1234-5678');
|
|
13
|
+
});
|
|
14
|
+
it('should limit to 11 digits', () => {
|
|
15
|
+
expect(formatPhoneNumber('010123456789999')).toBe('010-1234-5678');
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
describe('formatBusinessNumber', () => {
|
|
19
|
+
it('should format business numbers correctly', () => {
|
|
20
|
+
expect(formatBusinessNumber('1234567890')).toBe('123-45-67890');
|
|
21
|
+
expect(formatBusinessNumber('123')).toBe('123');
|
|
22
|
+
expect(formatBusinessNumber('12345')).toBe('123-45');
|
|
23
|
+
});
|
|
24
|
+
it('should limit to 10 digits', () => {
|
|
25
|
+
expect(formatBusinessNumber('12345678901234')).toBe('123-45-67890');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
describe('formatCreditCard', () => {
|
|
29
|
+
it('should format credit card numbers', () => {
|
|
30
|
+
expect(formatCreditCard('1234567890123456')).toBe('1234-5678-9012-3456');
|
|
31
|
+
expect(formatCreditCard('1234')).toBe('1234');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe('extractNumbers', () => {
|
|
35
|
+
it('should extract only numbers', () => {
|
|
36
|
+
expect(extractNumbers('abc123def456')).toBe('123456');
|
|
37
|
+
expect(extractNumbers('010-1234-5678')).toBe('01012345678');
|
|
38
|
+
expect(extractNumbers('no numbers')).toBe('');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe('formatCurrency', () => {
|
|
42
|
+
it('should format numbers with commas', () => {
|
|
43
|
+
expect(formatCurrency(1234567)).toBe('1,234,567원');
|
|
44
|
+
expect(formatCurrency(1000)).toBe('1,000원');
|
|
45
|
+
expect(formatCurrency(0)).toBe('0원');
|
|
46
|
+
});
|
|
47
|
+
it('should handle string input', () => {
|
|
48
|
+
expect(formatCurrency('1234567')).toBe('1,234,567원');
|
|
49
|
+
});
|
|
50
|
+
it('should allow custom currency', () => {
|
|
51
|
+
expect(formatCurrency(1000, '달러')).toBe('1,000달러');
|
|
52
|
+
});
|
|
53
|
+
it('should handle invalid input', () => {
|
|
54
|
+
expect(formatCurrency('invalid')).toBe('0원');
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('maskResidentNumber', () => {
|
|
58
|
+
it('should mask resident registration number', () => {
|
|
59
|
+
expect(maskResidentNumber('1234561234567')).toBe('123456-*******');
|
|
60
|
+
expect(maskResidentNumber('123456-1234567')).toBe('123456-*******');
|
|
61
|
+
});
|
|
62
|
+
it('should handle partial input', () => {
|
|
63
|
+
expect(maskResidentNumber('12345')).toBe('12345');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('maskCreditCard', () => {
|
|
67
|
+
it('should mask credit card number', () => {
|
|
68
|
+
expect(maskCreditCard('1234567890123456')).toBe('1234-****-****-3456');
|
|
69
|
+
});
|
|
70
|
+
it('should handle partial input', () => {
|
|
71
|
+
expect(maskCreditCard('1234567')).toBe('1234567');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.test.d.ts","sourceRoot":"","sources":["../../../src/form/__tests__/helpers.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { hasFormErrors, getChangedFields, removeEmptyValues, } from '../helpers';
|
|
3
|
+
describe('Form State Helpers', () => {
|
|
4
|
+
describe('hasFormErrors', () => {
|
|
5
|
+
it('should detect errors', () => {
|
|
6
|
+
expect(hasFormErrors({ email: 'error' })).toBe(true);
|
|
7
|
+
expect(hasFormErrors({})).toBe(false);
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
describe('getChangedFields', () => {
|
|
11
|
+
it('should return only changed fields', () => {
|
|
12
|
+
const original = { name: 'John', email: 'john@example.com', age: 30 };
|
|
13
|
+
const current = { name: 'Jane', email: 'john@example.com', age: 31 };
|
|
14
|
+
const changed = getChangedFields(original, current);
|
|
15
|
+
expect(changed).toEqual({ name: 'Jane', age: 31 });
|
|
16
|
+
});
|
|
17
|
+
it('should return empty object when nothing changed', () => {
|
|
18
|
+
const original = { name: 'John' };
|
|
19
|
+
const current = { name: 'John' };
|
|
20
|
+
expect(getChangedFields(original, current)).toEqual({});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
describe('removeEmptyValues', () => {
|
|
24
|
+
it('should remove null, undefined, and empty strings', () => {
|
|
25
|
+
const input = {
|
|
26
|
+
name: 'John',
|
|
27
|
+
email: '',
|
|
28
|
+
age: null,
|
|
29
|
+
phone: undefined,
|
|
30
|
+
address: 'Seoul',
|
|
31
|
+
};
|
|
32
|
+
expect(removeEmptyValues(input)).toEqual({
|
|
33
|
+
name: 'John',
|
|
34
|
+
address: 'Seoul',
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
it('should keep zero values', () => {
|
|
38
|
+
const input = { count: 0, active: false };
|
|
39
|
+
expect(removeEmptyValues(input)).toEqual({ count: 0, active: false });
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.test.d.ts","sourceRoot":"","sources":["../../../src/form/__tests__/validation.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { isEmail, isPhoneNumber, isUrl, isStrongPassword, isBusinessNumber, } from '../validation';
|
|
3
|
+
describe('Form Validation', () => {
|
|
4
|
+
describe('isEmail', () => {
|
|
5
|
+
it('should validate correct email formats', () => {
|
|
6
|
+
expect(isEmail('test@example.com')).toBe(true);
|
|
7
|
+
expect(isEmail('user.name@domain.co.kr')).toBe(true);
|
|
8
|
+
expect(isEmail('test+tag@example.com')).toBe(true);
|
|
9
|
+
});
|
|
10
|
+
it('should reject invalid email formats', () => {
|
|
11
|
+
expect(isEmail('invalid')).toBe(false);
|
|
12
|
+
expect(isEmail('test@')).toBe(false);
|
|
13
|
+
expect(isEmail('@example.com')).toBe(false);
|
|
14
|
+
expect(isEmail('test @example.com')).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
describe('isPhoneNumber', () => {
|
|
18
|
+
it('should validate Korean phone numbers', () => {
|
|
19
|
+
expect(isPhoneNumber('010-1234-5678')).toBe(true);
|
|
20
|
+
expect(isPhoneNumber('01012345678')).toBe(true);
|
|
21
|
+
expect(isPhoneNumber('02-1234-5678')).toBe(true);
|
|
22
|
+
expect(isPhoneNumber('031-123-4567')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
it('should reject invalid phone numbers', () => {
|
|
25
|
+
expect(isPhoneNumber('123-456-7890')).toBe(false);
|
|
26
|
+
expect(isPhoneNumber('010-12-3456')).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
describe('isUrl', () => {
|
|
30
|
+
it('should validate URLs', () => {
|
|
31
|
+
expect(isUrl('https://example.com')).toBe(true);
|
|
32
|
+
expect(isUrl('http://test.co.kr')).toBe(true);
|
|
33
|
+
expect(isUrl('https://example.com/path?query=1')).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
it('should reject invalid URLs', () => {
|
|
36
|
+
expect(isUrl('not-a-url')).toBe(false);
|
|
37
|
+
expect(isUrl('example.com')).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe('isStrongPassword', () => {
|
|
41
|
+
it('should validate strong passwords', () => {
|
|
42
|
+
expect(isStrongPassword('Password123!')).toBe(true);
|
|
43
|
+
expect(isStrongPassword('Str0ng#Pass')).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
it('should reject weak passwords', () => {
|
|
46
|
+
expect(isStrongPassword('short')).toBe(false);
|
|
47
|
+
expect(isStrongPassword('NoNumbers!')).toBe(false);
|
|
48
|
+
expect(isStrongPassword('nouppercas3!')).toBe(false);
|
|
49
|
+
expect(isStrongPassword('NOLOWERCASE1!')).toBe(false);
|
|
50
|
+
expect(isStrongPassword('NoSpecialChar1')).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
it('should respect custom options', () => {
|
|
53
|
+
expect(isStrongPassword('simple', { minLength: 6, requireUppercase: false, requireNumbers: false, requireSpecialChars: false })).toBe(true);
|
|
54
|
+
expect(isStrongPassword('PASSWORD', { requireLowercase: false, requireNumbers: false, requireSpecialChars: false })).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('isBusinessNumber', () => {
|
|
58
|
+
it('should validate 10-digit business numbers', () => {
|
|
59
|
+
expect(isBusinessNumber('1234567890')).toBe(true);
|
|
60
|
+
expect(isBusinessNumber('123-45-67890')).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
it('should reject invalid business numbers', () => {
|
|
63
|
+
expect(isBusinessNumber('123456789')).toBe(false);
|
|
64
|
+
expect(isBusinessNumber('12345678901')).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form Formatter Utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 전화번호 포맷팅 (010-1234-5678)
|
|
6
|
+
*/
|
|
7
|
+
export declare function formatPhoneNumber(value: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* 사업자등록번호 포맷팅 (123-45-67890)
|
|
10
|
+
*/
|
|
11
|
+
export declare function formatBusinessNumber(value: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* 신용카드 번호 포맷팅 (1234-5678-9012-3456)
|
|
14
|
+
*/
|
|
15
|
+
export declare function formatCreditCard(value: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* 숫자만 추출
|
|
18
|
+
*/
|
|
19
|
+
export declare function extractNumbers(value: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* 통화 포맷팅 (천 단위 콤마)
|
|
22
|
+
* @param value - 숫자 값
|
|
23
|
+
* @param currency - 통화 기호 (기본값: '원')
|
|
24
|
+
*/
|
|
25
|
+
export declare function formatCurrency(value: number | string, currency?: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* 주민등록번호 앞자리만 표시 (123456-*******)
|
|
28
|
+
*/
|
|
29
|
+
export declare function maskResidentNumber(value: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* 카드번호 마스킹 (1234-****-****-5678)
|
|
32
|
+
*/
|
|
33
|
+
export declare function maskCreditCard(value: string): string;
|
|
34
|
+
//# sourceMappingURL=formatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/form/formatter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAYvD;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAY1D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAItD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,GAAE,MAAY,GAAG,MAAM,CAKrF;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKxD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAKpD"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form Formatter Utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 전화번호 포맷팅 (010-1234-5678)
|
|
6
|
+
*/
|
|
7
|
+
export function formatPhoneNumber(value) {
|
|
8
|
+
const cleaned = value.replace(/[^0-9]/g, '');
|
|
9
|
+
if (cleaned.length <= 3)
|
|
10
|
+
return cleaned;
|
|
11
|
+
if (cleaned.length <= 7) {
|
|
12
|
+
return `${cleaned.slice(0, 3)}-${cleaned.slice(3)}`;
|
|
13
|
+
}
|
|
14
|
+
if (cleaned.length <= 11) {
|
|
15
|
+
return `${cleaned.slice(0, 3)}-${cleaned.slice(3, 7)}-${cleaned.slice(7)}`;
|
|
16
|
+
}
|
|
17
|
+
return `${cleaned.slice(0, 3)}-${cleaned.slice(3, 7)}-${cleaned.slice(7, 11)}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 사업자등록번호 포맷팅 (123-45-67890)
|
|
21
|
+
*/
|
|
22
|
+
export function formatBusinessNumber(value) {
|
|
23
|
+
const cleaned = value.replace(/[^0-9]/g, '');
|
|
24
|
+
if (cleaned.length <= 3)
|
|
25
|
+
return cleaned;
|
|
26
|
+
if (cleaned.length <= 5) {
|
|
27
|
+
return `${cleaned.slice(0, 3)}-${cleaned.slice(3)}`;
|
|
28
|
+
}
|
|
29
|
+
if (cleaned.length <= 10) {
|
|
30
|
+
return `${cleaned.slice(0, 3)}-${cleaned.slice(3, 5)}-${cleaned.slice(5)}`;
|
|
31
|
+
}
|
|
32
|
+
return `${cleaned.slice(0, 3)}-${cleaned.slice(3, 5)}-${cleaned.slice(5, 10)}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 신용카드 번호 포맷팅 (1234-5678-9012-3456)
|
|
36
|
+
*/
|
|
37
|
+
export function formatCreditCard(value) {
|
|
38
|
+
const cleaned = value.replace(/[^0-9]/g, '');
|
|
39
|
+
const groups = cleaned.match(/.{1,4}/g);
|
|
40
|
+
return groups ? groups.join('-') : cleaned;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 숫자만 추출
|
|
44
|
+
*/
|
|
45
|
+
export function extractNumbers(value) {
|
|
46
|
+
return value.replace(/[^0-9]/g, '');
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 통화 포맷팅 (천 단위 콤마)
|
|
50
|
+
* @param value - 숫자 값
|
|
51
|
+
* @param currency - 통화 기호 (기본값: '원')
|
|
52
|
+
*/
|
|
53
|
+
export function formatCurrency(value, currency = '원') {
|
|
54
|
+
const num = typeof value === 'string' ? parseFloat(value) : value;
|
|
55
|
+
if (isNaN(num))
|
|
56
|
+
return '0' + currency;
|
|
57
|
+
return num.toLocaleString('ko-KR') + currency;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 주민등록번호 앞자리만 표시 (123456-*******)
|
|
61
|
+
*/
|
|
62
|
+
export function maskResidentNumber(value) {
|
|
63
|
+
const cleaned = value.replace(/[^0-9]/g, '');
|
|
64
|
+
if (cleaned.length < 6)
|
|
65
|
+
return cleaned;
|
|
66
|
+
return `${cleaned.slice(0, 6)}-${'*'.repeat(7)}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 카드번호 마스킹 (1234-****-****-5678)
|
|
70
|
+
*/
|
|
71
|
+
export function maskCreditCard(value) {
|
|
72
|
+
const cleaned = value.replace(/[^0-9]/g, '');
|
|
73
|
+
if (cleaned.length < 12)
|
|
74
|
+
return value;
|
|
75
|
+
return `${cleaned.slice(0, 4)}-****-****-${cleaned.slice(-4)}`;
|
|
76
|
+
}
|