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,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String Manipulation Utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 문자열을 지정된 길이로 자르고 말줄임표 추가
|
|
7
|
+
* @example truncate('Hello World', 5) // 'Hello...'
|
|
8
|
+
*/
|
|
9
|
+
export function truncate(str: string, length: number, suffix: string = '...'): string {
|
|
10
|
+
if (str.length <= length) return str;
|
|
11
|
+
return str.slice(0, length) + suffix;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 문자열 양쪽 공백 제거 및 연속된 공백을 하나로
|
|
16
|
+
* @example normalizeWhitespace(' hello world ') // 'hello world'
|
|
17
|
+
*/
|
|
18
|
+
export function normalizeWhitespace(str: string): string {
|
|
19
|
+
return str.trim().replace(/\s+/g, ' ');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* URL 친화적인 슬러그 생성
|
|
24
|
+
* @example slugify('Hello World!') // 'hello-world'
|
|
25
|
+
* @example slugify('Test123 World') // 'test123-world'
|
|
26
|
+
*/
|
|
27
|
+
export function slugify(str: string): string {
|
|
28
|
+
return str
|
|
29
|
+
.toLowerCase()
|
|
30
|
+
.trim()
|
|
31
|
+
.replace(/[^\w\s-]/g, '')
|
|
32
|
+
.replace(/[\s_]+/g, '-')
|
|
33
|
+
.replace(/-+/g, '-')
|
|
34
|
+
.replace(/^-+|-+$/g, '');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 랜덤 문자열 생성
|
|
39
|
+
* @param length - 생성할 문자열 길이
|
|
40
|
+
* @param charset - 사용할 문자 집합 ('alphanumeric', 'alpha', 'numeric', 'hex')
|
|
41
|
+
*/
|
|
42
|
+
export function randomString(
|
|
43
|
+
length: number,
|
|
44
|
+
charset: 'alphanumeric' | 'alpha' | 'numeric' | 'hex' = 'alphanumeric'
|
|
45
|
+
): string {
|
|
46
|
+
const charsets = {
|
|
47
|
+
alphanumeric: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
|
48
|
+
alpha: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
49
|
+
numeric: '0123456789',
|
|
50
|
+
hex: '0123456789abcdef',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const chars = charsets[charset];
|
|
54
|
+
let result = '';
|
|
55
|
+
|
|
56
|
+
for (let i = 0; i < length; i++) {
|
|
57
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 문자열 반복
|
|
65
|
+
* @example repeat('ab', 3) // 'ababab'
|
|
66
|
+
*/
|
|
67
|
+
export function repeat(str: string, count: number): string {
|
|
68
|
+
return str.repeat(count);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 문자열 뒤집기
|
|
73
|
+
* @example reverse('hello') // 'olleh'
|
|
74
|
+
*/
|
|
75
|
+
export function reverse(str: string): string {
|
|
76
|
+
return str.split('').reverse().join('');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 문자열에서 HTML 태그 제거
|
|
81
|
+
* @example stripHtml('<p>Hello <strong>World</strong></p>') // 'Hello World'
|
|
82
|
+
*/
|
|
83
|
+
export function stripHtml(str: string): string {
|
|
84
|
+
return str.replace(/<[^>]*>/g, '');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 문자열을 특정 길이로 패딩
|
|
89
|
+
* @example padStart('5', 3, '0') // '005'
|
|
90
|
+
*/
|
|
91
|
+
export function padStart(str: string, length: number, fillString: string = ' '): string {
|
|
92
|
+
return str.padStart(length, fillString);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 문자열을 특정 길이로 패딩 (오른쪽)
|
|
97
|
+
* @example padEnd('5', 3, '0') // '500'
|
|
98
|
+
*/
|
|
99
|
+
export function padEnd(str: string, length: number, fillString: string = ' '): string {
|
|
100
|
+
return str.padEnd(length, fillString);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 템플릿 문자열 치환
|
|
105
|
+
* @example template('Hello {name}!', { name: 'World' }) // 'Hello World!'
|
|
106
|
+
*/
|
|
107
|
+
export function template(str: string, values: Record<string, any>): string {
|
|
108
|
+
return str.replace(/\{(\w+)\}/g, (match, key) => {
|
|
109
|
+
return values[key] !== undefined ? String(values[key]) : match;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 마스킹 (특정 위치의 문자를 *로 변경)
|
|
115
|
+
* @example mask('1234567890', 3, 7) // '123****890'
|
|
116
|
+
*/
|
|
117
|
+
export function mask(str: string, start: number, end?: number, maskChar: string = '*'): string {
|
|
118
|
+
const endPos = end ?? str.length;
|
|
119
|
+
return (
|
|
120
|
+
str.slice(0, start) +
|
|
121
|
+
maskChar.repeat(endPos - start) +
|
|
122
|
+
str.slice(endPos)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String Validation Utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 빈 문자열 또는 공백만 있는지 확인
|
|
7
|
+
* @example isEmpty('') // true
|
|
8
|
+
* @example isEmpty(' ') // true
|
|
9
|
+
* @example isEmpty('hello') // false
|
|
10
|
+
*/
|
|
11
|
+
export function isEmpty(str: string): boolean {
|
|
12
|
+
return !str || str.trim().length === 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 문자열이 특정 문자로 시작하는지 확인
|
|
17
|
+
* @example startsWith('hello', 'he') // true
|
|
18
|
+
*/
|
|
19
|
+
export function startsWith(str: string, searchString: string): boolean {
|
|
20
|
+
return str.startsWith(searchString);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 문자열이 특정 문자로 끝나는지 확인
|
|
25
|
+
* @example endsWith('hello', 'lo') // true
|
|
26
|
+
*/
|
|
27
|
+
export function endsWith(str: string, searchString: string): boolean {
|
|
28
|
+
return str.endsWith(searchString);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 문자열에 특정 문자열이 포함되어 있는지 확인
|
|
33
|
+
* @example contains('hello world', 'world') // true
|
|
34
|
+
*/
|
|
35
|
+
export function contains(str: string, searchString: string): boolean {
|
|
36
|
+
return str.includes(searchString);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 알파벳만 포함하는지 확인
|
|
41
|
+
* @example isAlpha('hello') // true
|
|
42
|
+
* @example isAlpha('hello123') // false
|
|
43
|
+
*/
|
|
44
|
+
export function isAlpha(str: string): boolean {
|
|
45
|
+
return /^[a-zA-Z]+$/.test(str);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 숫자만 포함하는지 확인
|
|
50
|
+
* @example isNumeric('123') // true
|
|
51
|
+
* @example isNumeric('123.45') // false
|
|
52
|
+
*/
|
|
53
|
+
export function isNumeric(str: string): boolean {
|
|
54
|
+
return /^[0-9]+$/.test(str);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 알파벳과 숫자만 포함하는지 확인
|
|
59
|
+
* @example isAlphanumeric('hello123') // true
|
|
60
|
+
* @example isAlphanumeric('hello_123') // false
|
|
61
|
+
*/
|
|
62
|
+
export function isAlphanumeric(str: string): boolean {
|
|
63
|
+
return /^[a-zA-Z0-9]+$/.test(str);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 소문자만 포함하는지 확인
|
|
68
|
+
* @example isLowerCase('hello') // true
|
|
69
|
+
* @example isLowerCase('Hello') // false
|
|
70
|
+
*/
|
|
71
|
+
export function isLowerCase(str: string): boolean {
|
|
72
|
+
return str === str.toLowerCase() && str !== str.toUpperCase();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 대문자만 포함하는지 확인
|
|
77
|
+
* @example isUpperCase('HELLO') // true
|
|
78
|
+
* @example isUpperCase('Hello') // false
|
|
79
|
+
*/
|
|
80
|
+
export function isUpperCase(str: string): boolean {
|
|
81
|
+
return str === str.toUpperCase() && str !== str.toLowerCase();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* JSON 형식인지 확인
|
|
86
|
+
* @example isJSON('{"name":"John"}') // true
|
|
87
|
+
* @example isJSON('not json') // false
|
|
88
|
+
*/
|
|
89
|
+
export function isJSON(str: string): boolean {
|
|
90
|
+
try {
|
|
91
|
+
JSON.parse(str);
|
|
92
|
+
return true;
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 16진수 색상 코드인지 확인
|
|
100
|
+
* @example isHexColor('#fff') // true
|
|
101
|
+
* @example isHexColor('#ffffff') // true
|
|
102
|
+
* @example isHexColor('fff') // false
|
|
103
|
+
*/
|
|
104
|
+
export function isHexColor(str: string): boolean {
|
|
105
|
+
return /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(str);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* UUID 형식인지 확인
|
|
110
|
+
* @example isUUID('123e4567-e89b-12d3-a456-426614174000') // true
|
|
111
|
+
*/
|
|
112
|
+
export function isUUID(str: string): boolean {
|
|
113
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(str);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Base64 인코딩된 문자열인지 확인
|
|
118
|
+
* @example isBase64('SGVsbG8gV29ybGQ=') // true
|
|
119
|
+
*/
|
|
120
|
+
export function isBase64(str: string): boolean {
|
|
121
|
+
try {
|
|
122
|
+
return btoa(atob(str)) === str;
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
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
|
+
});
|