goodchuck-utils 1.1.0 → 1.2.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/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -18
- package/package.json +13 -3
- package/src/date/index.test.ts +206 -0
- package/src/date/index.ts +123 -0
- package/src/index.ts +8 -14
- 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
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
declare function multiply(a: number, b: number): number;
|
|
4
|
-
declare function divide(a: number, b: number): number;
|
|
5
|
-
export { add, subtract, multiply, divide };
|
|
1
|
+
export * from './date';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,QAAQ,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
function subtract(a, b) {
|
|
11
|
-
return a - b;
|
|
12
|
-
}
|
|
13
|
-
function multiply(a, b) {
|
|
14
|
-
return a * b;
|
|
15
|
-
}
|
|
16
|
-
function divide(a, b) {
|
|
17
|
-
return a / b;
|
|
18
|
-
}
|
|
1
|
+
// Date utilities
|
|
2
|
+
export * from './date';
|
|
3
|
+
// String utilities (placeholder for future)
|
|
4
|
+
// export * from './string';
|
|
5
|
+
// Array utilities (placeholder for future)
|
|
6
|
+
// export * from './array';
|
|
7
|
+
// Object utilities (placeholder for future)
|
|
8
|
+
// export * from './object';
|
package/package.json
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "goodchuck-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prepare": "npm run build:tsc",
|
|
8
|
+
"prepublishOnly": "npm run test:run && npm run build:tsc",
|
|
8
9
|
"build": "npm run build",
|
|
9
|
-
"build:tsc": "tsc"
|
|
10
|
+
"build:tsc": "tsc",
|
|
11
|
+
"test": "vitest",
|
|
12
|
+
"test:ui": "vitest --ui",
|
|
13
|
+
"test:run": "vitest run",
|
|
14
|
+
"test:coverage": "vitest run --coverage"
|
|
10
15
|
},
|
|
11
16
|
"exports": {
|
|
12
17
|
".": {
|
|
@@ -21,9 +26,14 @@
|
|
|
21
26
|
"description": "",
|
|
22
27
|
"dependencies": {
|
|
23
28
|
"@types/node": "^25.0.6",
|
|
29
|
+
"dayjs": "^1.11.19",
|
|
24
30
|
"typescript": "^5.9.3"
|
|
25
31
|
},
|
|
26
32
|
"include": [
|
|
27
33
|
"src/**/*.ts"
|
|
28
|
-
]
|
|
34
|
+
],
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@vitest/ui": "^4.0.16",
|
|
37
|
+
"vitest": "^4.0.16"
|
|
38
|
+
}
|
|
29
39
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
formatDate,
|
|
4
|
+
now,
|
|
5
|
+
fromNow,
|
|
6
|
+
diffDate,
|
|
7
|
+
addDate,
|
|
8
|
+
subtractDate,
|
|
9
|
+
isBefore,
|
|
10
|
+
isAfter,
|
|
11
|
+
isSame,
|
|
12
|
+
isValidDate,
|
|
13
|
+
toTimezone,
|
|
14
|
+
} from './index';
|
|
15
|
+
|
|
16
|
+
describe('Date Utils', () => {
|
|
17
|
+
describe('formatDate', () => {
|
|
18
|
+
it('should format date with default format', () => {
|
|
19
|
+
const date = new Date('2024-01-15 10:30:45');
|
|
20
|
+
const result = formatDate(date);
|
|
21
|
+
expect(result).toBe('2024-01-15 10:30:45');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should format date with custom format', () => {
|
|
25
|
+
const date = new Date('2024-01-15');
|
|
26
|
+
const result = formatDate(date, 'YYYY년 MM월 DD일');
|
|
27
|
+
expect(result).toBe('2024년 01월 15일');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should format string date', () => {
|
|
31
|
+
const result = formatDate('2024-01-15', 'YYYY-MM-DD');
|
|
32
|
+
expect(result).toBe('2024-01-15');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should format timestamp', () => {
|
|
36
|
+
const timestamp = new Date('2024-01-15').getTime();
|
|
37
|
+
const result = formatDate(timestamp, 'YYYY-MM-DD');
|
|
38
|
+
expect(result).toBe('2024-01-15');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('now', () => {
|
|
43
|
+
it('should return formatted current date when format is provided', () => {
|
|
44
|
+
const result = now('YYYY-MM-DD');
|
|
45
|
+
expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should return Date object when no format is provided', () => {
|
|
49
|
+
const result = now();
|
|
50
|
+
expect(result).toBeInstanceOf(Date);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('fromNow', () => {
|
|
55
|
+
it('should return relative time in Korean', () => {
|
|
56
|
+
const yesterday = new Date();
|
|
57
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
58
|
+
const result = fromNow(yesterday, 'ko');
|
|
59
|
+
expect(result).toContain('전');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should return relative time in English', () => {
|
|
63
|
+
const yesterday = new Date();
|
|
64
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
65
|
+
const result = fromNow(yesterday, 'en');
|
|
66
|
+
expect(result).toContain('ago');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('diffDate', () => {
|
|
71
|
+
it('should calculate difference in days', () => {
|
|
72
|
+
const date1 = new Date('2024-01-20');
|
|
73
|
+
const date2 = new Date('2024-01-15');
|
|
74
|
+
const result = diffDate(date1, date2, 'day');
|
|
75
|
+
expect(result).toBe(5);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should calculate difference in hours', () => {
|
|
79
|
+
const date1 = new Date('2024-01-15 15:00:00');
|
|
80
|
+
const date2 = new Date('2024-01-15 12:00:00');
|
|
81
|
+
const result = diffDate(date1, date2, 'hour');
|
|
82
|
+
expect(result).toBe(3);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should handle negative differences', () => {
|
|
86
|
+
const date1 = new Date('2024-01-15');
|
|
87
|
+
const date2 = new Date('2024-01-20');
|
|
88
|
+
const result = diffDate(date1, date2, 'day');
|
|
89
|
+
expect(result).toBe(-5);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('addDate', () => {
|
|
94
|
+
it('should add days to date', () => {
|
|
95
|
+
const date = new Date('2024-01-15');
|
|
96
|
+
const result = addDate(date, 5, 'day');
|
|
97
|
+
expect(formatDate(result, 'YYYY-MM-DD')).toBe('2024-01-20');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should add hours to date', () => {
|
|
101
|
+
const date = new Date('2024-01-15 10:00:00');
|
|
102
|
+
const result = addDate(date, 3, 'hour');
|
|
103
|
+
expect(formatDate(result, 'YYYY-MM-DD HH:mm:ss')).toBe('2024-01-15 13:00:00');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should add months to date', () => {
|
|
107
|
+
const date = new Date('2024-01-15');
|
|
108
|
+
const result = addDate(date, 2, 'month');
|
|
109
|
+
expect(formatDate(result, 'YYYY-MM-DD')).toBe('2024-03-15');
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('subtractDate', () => {
|
|
114
|
+
it('should subtract days from date', () => {
|
|
115
|
+
const date = new Date('2024-01-20');
|
|
116
|
+
const result = subtractDate(date, 5, 'day');
|
|
117
|
+
expect(formatDate(result, 'YYYY-MM-DD')).toBe('2024-01-15');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should subtract hours from date', () => {
|
|
121
|
+
const date = new Date('2024-01-15 13:00:00');
|
|
122
|
+
const result = subtractDate(date, 3, 'hour');
|
|
123
|
+
expect(formatDate(result, 'YYYY-MM-DD HH:mm:ss')).toBe('2024-01-15 10:00:00');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('isBefore', () => {
|
|
128
|
+
it('should return true if first date is before second', () => {
|
|
129
|
+
const date1 = new Date('2024-01-15');
|
|
130
|
+
const date2 = new Date('2024-01-20');
|
|
131
|
+
expect(isBefore(date1, date2)).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('should return false if first date is after second', () => {
|
|
135
|
+
const date1 = new Date('2024-01-20');
|
|
136
|
+
const date2 = new Date('2024-01-15');
|
|
137
|
+
expect(isBefore(date1, date2)).toBe(false);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('isAfter', () => {
|
|
142
|
+
it('should return true if first date is after second', () => {
|
|
143
|
+
const date1 = new Date('2024-01-20');
|
|
144
|
+
const date2 = new Date('2024-01-15');
|
|
145
|
+
expect(isAfter(date1, date2)).toBe(true);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should return false if first date is before second', () => {
|
|
149
|
+
const date1 = new Date('2024-01-15');
|
|
150
|
+
const date2 = new Date('2024-01-20');
|
|
151
|
+
expect(isAfter(date1, date2)).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('isSame', () => {
|
|
156
|
+
it('should return true for exact same dates', () => {
|
|
157
|
+
const date1 = new Date('2024-01-15 10:30:45');
|
|
158
|
+
const date2 = new Date('2024-01-15 10:30:45');
|
|
159
|
+
expect(isSame(date1, date2)).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should return true for same day (ignoring time)', () => {
|
|
163
|
+
const date1 = new Date('2024-01-15 10:00:00');
|
|
164
|
+
const date2 = new Date('2024-01-15 15:00:00');
|
|
165
|
+
expect(isSame(date1, date2, 'day')).toBe(true);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('should return false for different days', () => {
|
|
169
|
+
const date1 = new Date('2024-01-15');
|
|
170
|
+
const date2 = new Date('2024-01-16');
|
|
171
|
+
expect(isSame(date1, date2, 'day')).toBe(false);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('isValidDate', () => {
|
|
176
|
+
it('should return true for valid dates', () => {
|
|
177
|
+
expect(isValidDate(new Date('2024-01-15'))).toBe(true);
|
|
178
|
+
expect(isValidDate('2024-01-15')).toBe(true);
|
|
179
|
+
expect(isValidDate(1705276800000)).toBe(true);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should return false for invalid dates', () => {
|
|
183
|
+
expect(isValidDate('invalid-date')).toBe(false);
|
|
184
|
+
expect(isValidDate(NaN)).toBe(false);
|
|
185
|
+
expect(isValidDate(null)).toBe(false);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('toTimezone', () => {
|
|
190
|
+
it('should convert to specific timezone', () => {
|
|
191
|
+
const date = new Date('2024-01-15T00:00:00Z');
|
|
192
|
+
const result = toTimezone(date, 'Asia/Seoul');
|
|
193
|
+
expect(result).toBeInstanceOf(Date);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should handle different timezones', () => {
|
|
197
|
+
const date = new Date('2024-01-15T12:00:00Z');
|
|
198
|
+
const nyTime = toTimezone(date, 'America/New_York');
|
|
199
|
+
const seoulTime = toTimezone(date, 'Asia/Seoul');
|
|
200
|
+
|
|
201
|
+
expect(nyTime).toBeInstanceOf(Date);
|
|
202
|
+
expect(seoulTime).toBeInstanceOf(Date);
|
|
203
|
+
expect(nyTime.getTime()).toBe(seoulTime.getTime());
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
|
|
7
|
+
dayjs.extend(utc);
|
|
8
|
+
dayjs.extend(timezone);
|
|
9
|
+
dayjs.extend(relativeTime);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 날짜를 지정된 포맷으로 변환
|
|
13
|
+
* @param date - 변환할 날짜 (Date, string, number)
|
|
14
|
+
* @param format - 포맷 문자열 (기본값: 'YYYY-MM-DD HH:mm:ss')
|
|
15
|
+
*/
|
|
16
|
+
export function formatDate(
|
|
17
|
+
date: Date | string | number = new Date(),
|
|
18
|
+
format: string = 'YYYY-MM-DD HH:mm:ss'
|
|
19
|
+
): string {
|
|
20
|
+
return dayjs(date).format(format);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 현재 날짜/시간 반환
|
|
25
|
+
* @param format - 포맷 문자열 (선택)
|
|
26
|
+
*/
|
|
27
|
+
export function now(format?: string): string | Date {
|
|
28
|
+
const current = dayjs();
|
|
29
|
+
return format ? current.format(format) : current.toDate();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 상대 시간 반환 (예: "3시간 전")
|
|
34
|
+
* @param date - 기준 날짜
|
|
35
|
+
* @param locale - 로케일 (기본값: 'ko')
|
|
36
|
+
*/
|
|
37
|
+
export function fromNow(date: Date | string | number, locale: string = 'ko'): string {
|
|
38
|
+
return dayjs(date).locale(locale).fromNow();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 두 날짜 사이의 차이 계산
|
|
43
|
+
* @param date1 - 첫 번째 날짜
|
|
44
|
+
* @param date2 - 두 번째 날짜
|
|
45
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
46
|
+
*/
|
|
47
|
+
export function diffDate(
|
|
48
|
+
date1: Date | string | number,
|
|
49
|
+
date2: Date | string | number,
|
|
50
|
+
unit: dayjs.OpUnitType = 'day'
|
|
51
|
+
): number {
|
|
52
|
+
return dayjs(date1).diff(dayjs(date2), unit);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 날짜에 시간 더하기
|
|
57
|
+
* @param date - 기준 날짜
|
|
58
|
+
* @param value - 더할 값
|
|
59
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
60
|
+
*/
|
|
61
|
+
export function addDate(
|
|
62
|
+
date: Date | string | number,
|
|
63
|
+
value: number,
|
|
64
|
+
unit: dayjs.ManipulateType = 'day'
|
|
65
|
+
): Date {
|
|
66
|
+
return dayjs(date).add(value, unit).toDate();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 날짜에 시간 빼기
|
|
71
|
+
* @param date - 기준 날짜
|
|
72
|
+
* @param value - 뺄 값
|
|
73
|
+
* @param unit - 단위 ('day', 'hour', 'minute' 등)
|
|
74
|
+
*/
|
|
75
|
+
export function subtractDate(
|
|
76
|
+
date: Date | string | number,
|
|
77
|
+
value: number,
|
|
78
|
+
unit: dayjs.ManipulateType = 'day'
|
|
79
|
+
): Date {
|
|
80
|
+
return dayjs(date).subtract(value, unit).toDate();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 날짜가 특정 날짜보다 이전인지 확인
|
|
85
|
+
*/
|
|
86
|
+
export function isBefore(date1: Date | string | number, date2: Date | string | number): boolean {
|
|
87
|
+
return dayjs(date1).isBefore(dayjs(date2));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 날짜가 특정 날짜보다 이후인지 확인
|
|
92
|
+
*/
|
|
93
|
+
export function isAfter(date1: Date | string | number, date2: Date | string | number): boolean {
|
|
94
|
+
return dayjs(date1).isAfter(dayjs(date2));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 두 날짜가 같은지 확인
|
|
99
|
+
* @param unit - 비교 단위 (기본값: 'millisecond')
|
|
100
|
+
*/
|
|
101
|
+
export function isSame(
|
|
102
|
+
date1: Date | string | number,
|
|
103
|
+
date2: Date | string | number,
|
|
104
|
+
unit: dayjs.OpUnitType = 'millisecond'
|
|
105
|
+
): boolean {
|
|
106
|
+
return dayjs(date1).isSame(dayjs(date2), unit);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 날짜가 유효한지 확인
|
|
111
|
+
*/
|
|
112
|
+
export function isValidDate(date: any): boolean {
|
|
113
|
+
return dayjs(date).isValid();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 타임존 변환
|
|
118
|
+
* @param date - 변환할 날짜
|
|
119
|
+
* @param tz - 타임존 (예: 'Asia/Seoul')
|
|
120
|
+
*/
|
|
121
|
+
export function toTimezone(date: Date | string | number, tz: string): Date {
|
|
122
|
+
return dayjs(date).tz(tz).toDate();
|
|
123
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
// Date utilities
|
|
2
|
+
export * from './date';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
4
|
+
// String utilities (placeholder for future)
|
|
5
|
+
// export * from './string';
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
7
|
+
// Array utilities (placeholder for future)
|
|
8
|
+
// export * from './array';
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { add, subtract, multiply, divide };
|
|
10
|
+
// Object utilities (placeholder for future)
|
|
11
|
+
// export * from './object';
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "
|
|
5
|
-
"lib": [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020", "DOM"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
"rootDir": "src",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"moduleResolution": "node",
|
|
14
|
+
"resolveJsonModule": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*.ts"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
14
18
|
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
environment: 'node',
|
|
7
|
+
coverage: {
|
|
8
|
+
provider: 'v8',
|
|
9
|
+
reporter: ['text', 'html'],
|
|
10
|
+
exclude: ['node_modules/', 'dist/', '**/*.test.ts'],
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|