goodchuck-utils 1.2.0 → 1.4.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.
Files changed (66) hide show
  1. package/dist/form/__tests__/formatter.test.d.ts +2 -0
  2. package/dist/form/__tests__/formatter.test.d.ts.map +1 -0
  3. package/dist/form/__tests__/formatter.test.js +74 -0
  4. package/dist/form/__tests__/helpers.test.d.ts +2 -0
  5. package/dist/form/__tests__/helpers.test.d.ts.map +1 -0
  6. package/dist/form/__tests__/helpers.test.js +42 -0
  7. package/dist/form/__tests__/validation.test.d.ts +2 -0
  8. package/dist/form/__tests__/validation.test.d.ts.map +1 -0
  9. package/dist/form/__tests__/validation.test.js +67 -0
  10. package/dist/form/formatter.d.ts +34 -0
  11. package/dist/form/formatter.d.ts.map +1 -0
  12. package/dist/form/formatter.js +76 -0
  13. package/dist/form/helpers.d.ts +16 -0
  14. package/dist/form/helpers.d.ts.map +1 -0
  15. package/dist/form/helpers.js +34 -0
  16. package/dist/form/index.d.ts +9 -0
  17. package/dist/form/index.d.ts.map +1 -0
  18. package/dist/form/index.js +11 -0
  19. package/dist/form/validation.d.ts +33 -0
  20. package/dist/form/validation.d.ts.map +1 -0
  21. package/dist/form/validation.js +56 -0
  22. package/dist/hooks/index.d.ts +11 -0
  23. package/dist/hooks/index.d.ts.map +1 -0
  24. package/dist/hooks/index.js +12 -0
  25. package/dist/hooks/useClickOutside.d.ts +49 -0
  26. package/dist/hooks/useClickOutside.d.ts.map +1 -0
  27. package/dist/hooks/useClickOutside.js +94 -0
  28. package/dist/hooks/useLocalStorage.d.ts +19 -0
  29. package/dist/hooks/useLocalStorage.d.ts.map +1 -0
  30. package/dist/hooks/useLocalStorage.js +91 -0
  31. package/dist/hooks/useMediaQuery.d.ts +56 -0
  32. package/dist/hooks/useMediaQuery.d.ts.map +1 -0
  33. package/dist/hooks/useMediaQuery.js +104 -0
  34. package/dist/hooks/useSessionStorage.d.ts +19 -0
  35. package/dist/hooks/useSessionStorage.d.ts.map +1 -0
  36. package/dist/hooks/useSessionStorage.js +85 -0
  37. package/dist/index.d.ts +2 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +7 -2
  40. package/dist/string/__tests__/case.test.d.ts +2 -0
  41. package/dist/string/__tests__/case.test.d.ts.map +1 -0
  42. package/dist/string/__tests__/case.test.js +61 -0
  43. package/dist/string/__tests__/manipulation.test.d.ts +2 -0
  44. package/dist/string/__tests__/manipulation.test.d.ts.map +1 -0
  45. package/dist/string/__tests__/manipulation.test.js +109 -0
  46. package/dist/string/__tests__/validation.test.d.ts +2 -0
  47. package/dist/string/__tests__/validation.test.d.ts.map +1 -0
  48. package/dist/string/__tests__/validation.test.js +101 -0
  49. package/dist/string/case.d.ts +42 -0
  50. package/dist/string/case.d.ts.map +1 -0
  51. package/dist/string/case.js +71 -0
  52. package/dist/string/index.d.ts +9 -0
  53. package/dist/string/index.d.ts.map +1 -0
  54. package/dist/string/index.js +11 -0
  55. package/dist/string/manipulation.d.ts +61 -0
  56. package/dist/string/manipulation.d.ts.map +1 -0
  57. package/dist/string/manipulation.js +106 -0
  58. package/dist/string/validation.d.ts +79 -0
  59. package/dist/string/validation.d.ts.map +1 -0
  60. package/dist/string/validation.js +115 -0
  61. package/package.json +22 -7
  62. package/src/date/index.test.ts +0 -206
  63. package/src/date/index.ts +0 -123
  64. package/src/index.ts +0 -11
  65. package/tsconfig.json +0 -18
  66. package/vitest.config.ts +0 -13
@@ -0,0 +1,115 @@
1
+ /**
2
+ * String Validation Utilities
3
+ */
4
+ /**
5
+ * 빈 문자열 또는 공백만 있는지 확인
6
+ * @example isEmpty('') // true
7
+ * @example isEmpty(' ') // true
8
+ * @example isEmpty('hello') // false
9
+ */
10
+ export function isEmpty(str) {
11
+ return !str || str.trim().length === 0;
12
+ }
13
+ /**
14
+ * 문자열이 특정 문자로 시작하는지 확인
15
+ * @example startsWith('hello', 'he') // true
16
+ */
17
+ export function startsWith(str, searchString) {
18
+ return str.startsWith(searchString);
19
+ }
20
+ /**
21
+ * 문자열이 특정 문자로 끝나는지 확인
22
+ * @example endsWith('hello', 'lo') // true
23
+ */
24
+ export function endsWith(str, searchString) {
25
+ return str.endsWith(searchString);
26
+ }
27
+ /**
28
+ * 문자열에 특정 문자열이 포함되어 있는지 확인
29
+ * @example contains('hello world', 'world') // true
30
+ */
31
+ export function contains(str, searchString) {
32
+ return str.includes(searchString);
33
+ }
34
+ /**
35
+ * 알파벳만 포함하는지 확인
36
+ * @example isAlpha('hello') // true
37
+ * @example isAlpha('hello123') // false
38
+ */
39
+ export function isAlpha(str) {
40
+ return /^[a-zA-Z]+$/.test(str);
41
+ }
42
+ /**
43
+ * 숫자만 포함하는지 확인
44
+ * @example isNumeric('123') // true
45
+ * @example isNumeric('123.45') // false
46
+ */
47
+ export function isNumeric(str) {
48
+ return /^[0-9]+$/.test(str);
49
+ }
50
+ /**
51
+ * 알파벳과 숫자만 포함하는지 확인
52
+ * @example isAlphanumeric('hello123') // true
53
+ * @example isAlphanumeric('hello_123') // false
54
+ */
55
+ export function isAlphanumeric(str) {
56
+ return /^[a-zA-Z0-9]+$/.test(str);
57
+ }
58
+ /**
59
+ * 소문자만 포함하는지 확인
60
+ * @example isLowerCase('hello') // true
61
+ * @example isLowerCase('Hello') // false
62
+ */
63
+ export function isLowerCase(str) {
64
+ return str === str.toLowerCase() && str !== str.toUpperCase();
65
+ }
66
+ /**
67
+ * 대문자만 포함하는지 확인
68
+ * @example isUpperCase('HELLO') // true
69
+ * @example isUpperCase('Hello') // false
70
+ */
71
+ export function isUpperCase(str) {
72
+ return str === str.toUpperCase() && str !== str.toLowerCase();
73
+ }
74
+ /**
75
+ * JSON 형식인지 확인
76
+ * @example isJSON('{"name":"John"}') // true
77
+ * @example isJSON('not json') // false
78
+ */
79
+ export function isJSON(str) {
80
+ try {
81
+ JSON.parse(str);
82
+ return true;
83
+ }
84
+ catch {
85
+ return false;
86
+ }
87
+ }
88
+ /**
89
+ * 16진수 색상 코드인지 확인
90
+ * @example isHexColor('#fff') // true
91
+ * @example isHexColor('#ffffff') // true
92
+ * @example isHexColor('fff') // false
93
+ */
94
+ export function isHexColor(str) {
95
+ return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(str);
96
+ }
97
+ /**
98
+ * UUID 형식인지 확인
99
+ * @example isUUID('123e4567-e89b-12d3-a456-426614174000') // true
100
+ */
101
+ export function isUUID(str) {
102
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(str);
103
+ }
104
+ /**
105
+ * Base64 인코딩된 문자열인지 확인
106
+ * @example isBase64('SGVsbG8gV29ybGQ=') // true
107
+ */
108
+ export function isBase64(str) {
109
+ try {
110
+ return btoa(atob(str)) === str;
111
+ }
112
+ catch {
113
+ return false;
114
+ }
115
+ }
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "goodchuck-utils",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
6
10
  "scripts": {
7
11
  "prepare": "npm run build:tsc",
8
12
  "prepublishOnly": "npm run test:run && npm run build:tsc",
@@ -18,6 +22,11 @@
18
22
  "types": "./dist/index.d.ts",
19
23
  "import": "./dist/index.js",
20
24
  "require": "./src/index.js"
25
+ },
26
+ "./hooks": {
27
+ "types": "./dist/hooks/index.d.ts",
28
+ "import": "./dist/hooks/index.js",
29
+ "require": "./src/hooks/index.js"
21
30
  }
22
31
  },
23
32
  "keywords": [],
@@ -25,15 +34,21 @@
25
34
  "license": "ISC",
26
35
  "description": "",
27
36
  "dependencies": {
28
- "@types/node": "^25.0.6",
29
- "dayjs": "^1.11.19",
30
- "typescript": "^5.9.3"
37
+ "dayjs": "^1.11.19"
31
38
  },
32
- "include": [
33
- "src/**/*.ts"
34
- ],
35
39
  "devDependencies": {
40
+ "@types/node": "^25.0.6",
41
+ "typescript": "^5.9.3",
42
+ "@types/react": "^19.2.8",
36
43
  "@vitest/ui": "^4.0.16",
37
44
  "vitest": "^4.0.16"
45
+ },
46
+ "peerDependencies": {
47
+ "react": ">=16.8.0"
48
+ },
49
+ "peerDependenciesMeta": {
50
+ "react": {
51
+ "optional": true
52
+ }
38
53
  }
39
54
  }
@@ -1,206 +0,0 @@
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
- });
package/src/date/index.ts DELETED
@@ -1,123 +0,0 @@
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 DELETED
@@ -1,11 +0,0 @@
1
- // Date utilities
2
- export * from './date';
3
-
4
- // String utilities (placeholder for future)
5
- // export * from './string';
6
-
7
- // Array utilities (placeholder for future)
8
- // export * from './array';
9
-
10
- // Object utilities (placeholder for future)
11
- // export * from './object';
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
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"]
18
- }
package/vitest.config.ts DELETED
@@ -1,13 +0,0 @@
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
- });