clear-af.js 1.0.0 → 1.0.2
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/README.md +90 -62
- package/README_FR.md +125 -0
- package/dist/index.d.ts +108 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +126 -1
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +78 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1633 -0
- package/docs/functions/camelify.html +7 -0
- package/docs/functions/capitalize.html +7 -0
- package/docs/functions/deepClone.html +7 -0
- package/docs/functions/isEmail.html +7 -0
- package/docs/functions/isEmpty.html +7 -0
- package/docs/functions/isType.html +8 -0
- package/docs/functions/isURL.html +7 -0
- package/docs/functions/kebabify.html +7 -0
- package/docs/functions/logHeader.html +6 -0
- package/docs/functions/logSeparator.html +5 -0
- package/docs/functions/noTwins.html +7 -0
- package/docs/functions/prettyDebug.html +7 -0
- package/docs/functions/prettyError.html +7 -0
- package/docs/functions/prettyInfo.html +7 -0
- package/docs/functions/prettySuccess.html +7 -0
- package/docs/functions/prettyWarn.html +7 -0
- package/docs/functions/snakify.html +7 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +87 -0
- package/docs/media/README_FR.md +125 -0
- package/docs/modules.html +69 -0
- package/package.json +4 -2
- package/src/index.ts +4 -251
- package/src/logging.ts +129 -0
- package/src/object_manipulation.ts +42 -0
- package/src/transformation.ts +89 -0
- package/src/validation.ts +79 -0
package/src/index.ts
CHANGED
|
@@ -7,254 +7,7 @@
|
|
|
7
7
|
Made by FroostDev | https://github.com/FroostDev - MIT Licence
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
function getDate(time: boolean = false): string {
|
|
16
|
-
return time ? `[${new Date().toLocaleString()}]` : '';
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/*
|
|
20
|
-
* =================
|
|
21
|
-
* Logging utilities
|
|
22
|
-
* =================
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Show a pretty error log message with optional date information
|
|
27
|
-
* @function prettyError
|
|
28
|
-
* @param err - Error message to display
|
|
29
|
-
* @param time - Include or not a timestamp
|
|
30
|
-
*/
|
|
31
|
-
function prettyError(err: string, time = false): void {
|
|
32
|
-
const date = getDate(time);
|
|
33
|
-
console.log(`\x1b[1;41m ✕ ${date} - ${err} \x1b[0m`);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Show a pretty warn log message with optional date information
|
|
38
|
-
* @function prettyWarn
|
|
39
|
-
* @param warn - Warning message to display
|
|
40
|
-
* @param time - Include or not a timestamp
|
|
41
|
-
*/
|
|
42
|
-
function prettyWarn(warn: string, time = false): void {
|
|
43
|
-
const date = getDate(time);
|
|
44
|
-
console.log(`\x1b[1;43m ⚠ ${date} - ${warn} \x1b[0m`);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Show a pretty success log message with optional date information
|
|
49
|
-
* @function prettySuccess
|
|
50
|
-
* @param success - Success message to display
|
|
51
|
-
* @param time - Include or not a timestamp
|
|
52
|
-
*/
|
|
53
|
-
function prettySuccess(success: string, time = false): void {
|
|
54
|
-
const date = getDate(time);
|
|
55
|
-
console.log(`\x1b[1;42m ✔ ${date} - ${success} \x1b[0m`);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Show a pretty information log message with optional date information
|
|
60
|
-
* @function prettyInfo
|
|
61
|
-
* @param info - Information message to display
|
|
62
|
-
* @param time - Include or not a timestamp
|
|
63
|
-
*/
|
|
64
|
-
function prettyInfo(info: string, time = false): void {
|
|
65
|
-
const date = getDate(time);
|
|
66
|
-
console.log(`\x1b[1;44m ℹ ${date} - ${info} \x1b[0m`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Show a pretty debug log message with optional date information
|
|
71
|
-
* @function prettyDebug
|
|
72
|
-
* @param debug - Debug message to display
|
|
73
|
-
* @param time - Include or not a timestamp
|
|
74
|
-
*/
|
|
75
|
-
function prettyDebug(debug: string, time = false): void {
|
|
76
|
-
const date = getDate(time);
|
|
77
|
-
console.log(`\x1b[1;97m ⚙ ${date} - ${debug} \x1b[0m`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Show a separator line in console
|
|
82
|
-
* @function logSeparator
|
|
83
|
-
*/
|
|
84
|
-
function logSeparator(): void {
|
|
85
|
-
console.log(`\x1b[1m${'═'.repeat(50)}\x1b[0m`);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Display a header with a title in the center
|
|
90
|
-
* @function logHeader
|
|
91
|
-
* @param title - The title of the header
|
|
92
|
-
*/
|
|
93
|
-
function logHeader(title: string): void {
|
|
94
|
-
console.log(`\x1b[1m╔${'═'.repeat(title.length + 2)}╗\x1b[0m`);
|
|
95
|
-
console.log(`\x1b[1m║ ${title} ║\x1b[0m`);
|
|
96
|
-
console.log(`\x1b[1m╚${'═'.repeat(title.length + 2)}╝\x1b[0m`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
*
|
|
101
|
-
* Validation utilities
|
|
102
|
-
*
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Check if any type of variable is empty
|
|
107
|
-
* @function isEmpty
|
|
108
|
-
* @param value - The variable to check
|
|
109
|
-
* @returns {boolean} True if the value is empty, false otherwise
|
|
110
|
-
*/
|
|
111
|
-
function isEmpty(value: unknown): boolean {
|
|
112
|
-
if (value === null || value === undefined) return true;
|
|
113
|
-
if (typeof value === "string") return value.trim().length === 0;
|
|
114
|
-
if (Array.isArray(value)) return value.length === 0;
|
|
115
|
-
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Check if the variable is of the chosen type
|
|
121
|
-
* @function isType
|
|
122
|
-
* @param value - The variable to check
|
|
123
|
-
* @param type - The type you want
|
|
124
|
-
* @returns {boolean} True if the value is of the chosen type, false otherwise
|
|
125
|
-
*/
|
|
126
|
-
function isType(value: unknown, type: string): boolean {
|
|
127
|
-
return typeof value === type;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Check if an email is valid
|
|
132
|
-
* @function isEmail
|
|
133
|
-
* @param email - The email to check
|
|
134
|
-
* @returns {boolean} True if the email is valid, false otherwise
|
|
135
|
-
*/
|
|
136
|
-
function isEmail(email: string): boolean {
|
|
137
|
-
const regex = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
138
|
-
return regex.test(email);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Check if a URL is valid
|
|
143
|
-
* @function isURL
|
|
144
|
-
* @param url - The URL to check
|
|
145
|
-
* @returns {boolean} True if the URL is valid, false otherwise
|
|
146
|
-
*/
|
|
147
|
-
function isURL(url: string): boolean {
|
|
148
|
-
try {
|
|
149
|
-
new URL(url);
|
|
150
|
-
return true;
|
|
151
|
-
} catch (error) {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
158
|
-
* Object manipulation
|
|
159
|
-
*
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Create a deep clone of any object or array
|
|
164
|
-
* @function deepClone
|
|
165
|
-
* @param obj - The object or array to clone
|
|
166
|
-
* @returns A deep clone of the object
|
|
167
|
-
*/
|
|
168
|
-
function deepClone<T>(obj: T): T {
|
|
169
|
-
if (obj === null || typeof obj !== "object") return obj;
|
|
170
|
-
|
|
171
|
-
if (Array.isArray(obj)) return obj.map(item => deepClone(item)) as T;
|
|
172
|
-
|
|
173
|
-
const cloned = {} as T;
|
|
174
|
-
for (const key in obj) {
|
|
175
|
-
if (obj.hasOwnProperty(key)) cloned[key] = deepClone(obj[key]);
|
|
176
|
-
}
|
|
177
|
-
return cloned;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Remove duplicate values from an array
|
|
182
|
-
* @function noTwins
|
|
183
|
-
* @param arr - The array to remove duplicates from
|
|
184
|
-
* @returns {unknown[]} A new array with unique values only
|
|
185
|
-
*/
|
|
186
|
-
function noTwins(arr: unknown[]): unknown[] {
|
|
187
|
-
return Array.from(new Set(arr));
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
*
|
|
192
|
-
* Transforamtion utilites
|
|
193
|
-
*
|
|
194
|
-
*/
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Transform a string to camelCase format
|
|
198
|
-
* @function camelify
|
|
199
|
-
* @param str - The string to transform
|
|
200
|
-
* @returns {string} The string in camelCase format
|
|
201
|
-
*/
|
|
202
|
-
function camelify(str: string): string {
|
|
203
|
-
let camelCased: string = "";
|
|
204
|
-
if (!isEmpty(str)) {
|
|
205
|
-
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
206
|
-
let toCamelify = cleaned.split(" ");
|
|
207
|
-
|
|
208
|
-
camelCased = toCamelify.map((word, index) => {
|
|
209
|
-
if (index === 0) return word.toLowerCase();
|
|
210
|
-
return word[0].toUpperCase() + word.slice(1).toLowerCase();
|
|
211
|
-
})
|
|
212
|
-
.join('');
|
|
213
|
-
}
|
|
214
|
-
return camelCased;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Transform a string to kebab-case format
|
|
219
|
-
* @function kebabify
|
|
220
|
-
* @param str - The string to transform
|
|
221
|
-
* @returns {string} The string in kebab-case format
|
|
222
|
-
*/
|
|
223
|
-
function kebabify(str: string): string {
|
|
224
|
-
let kebabised: string = "";
|
|
225
|
-
if (!isEmpty(str)) {
|
|
226
|
-
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
227
|
-
let toKebabify = cleaned.split(" ");
|
|
228
|
-
kebabised = toKebabify.map(word => word.toLowerCase())
|
|
229
|
-
.join("-");
|
|
230
|
-
}
|
|
231
|
-
return kebabised;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Transform a string to snake_case format
|
|
236
|
-
* @function snakify
|
|
237
|
-
* @param str - The string to transform
|
|
238
|
-
* @returns {string} The string in snake_case format
|
|
239
|
-
*/
|
|
240
|
-
function snakify(str: string): string {
|
|
241
|
-
let snaked: string = "";
|
|
242
|
-
if (!isEmpty(str)) {
|
|
243
|
-
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
244
|
-
let toSnakify = cleaned.split(" ");
|
|
245
|
-
snaked = toSnakify.map(word => word.toLowerCase())
|
|
246
|
-
.join("_");
|
|
247
|
-
}
|
|
248
|
-
return snaked;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Capitalize the first character of a string
|
|
253
|
-
* @function capitalize
|
|
254
|
-
* @param str - The string to capitalize
|
|
255
|
-
* @returns {string} The string with the first character in uppercase
|
|
256
|
-
*/
|
|
257
|
-
function capitalize(str: string): string {
|
|
258
|
-
if (isEmpty(str)) return str;
|
|
259
|
-
return str[0].toUpperCase() + str.slice(1);
|
|
260
|
-
}
|
|
10
|
+
export * from './logging';
|
|
11
|
+
export * from './validation';
|
|
12
|
+
export * from './transformation';
|
|
13
|
+
export * from './object_manipulation';
|
package/src/logging.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper function to get formatted date
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
function getDate(time: boolean = false): string {
|
|
6
|
+
return time ? `[${new Date().toLocaleString()}]` : '';
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Show a pretty error log message with optional date information
|
|
11
|
+
* @category Logging
|
|
12
|
+
* @function prettyError
|
|
13
|
+
* @param err - Error message to display
|
|
14
|
+
* @param time - Include or not a timestamp
|
|
15
|
+
* @example
|
|
16
|
+
* prettyError("Database connection failed");
|
|
17
|
+
* // Output: ✕ - Database connection failed (in red)
|
|
18
|
+
*
|
|
19
|
+
* prettyError("Critical error", true);
|
|
20
|
+
* // Output: ✕ [30/03/2026 10:30:45] - Critical error (in red with timestamp)
|
|
21
|
+
*/
|
|
22
|
+
function prettyError(err: string, time = false): void {
|
|
23
|
+
const date = getDate(time);
|
|
24
|
+
console.log(`\x1b[1;41m ✕ ${date} - ${err} \x1b[0m`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Show a pretty warn log message with optional date information
|
|
29
|
+
* @category Logging
|
|
30
|
+
* @function prettyWarn
|
|
31
|
+
* @param warn - Warning message to display
|
|
32
|
+
* @param time - Include or not a timestamp
|
|
33
|
+
* @example
|
|
34
|
+
* prettyWarn("Deprecated function used");
|
|
35
|
+
* // Output: ⚠ - Deprecated function used (in orange)
|
|
36
|
+
*
|
|
37
|
+
* prettyWarn("Low memory", true);
|
|
38
|
+
* // Output: ⚠ [30/03/2026 10:30:45] - Low memory (in orange with timestamp)
|
|
39
|
+
*/
|
|
40
|
+
function prettyWarn(warn: string, time = false): void {
|
|
41
|
+
const date = getDate(time);
|
|
42
|
+
console.log(`\x1b[1;43m ⚠ ${date} - ${warn} \x1b[0m`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Show a pretty success log message with optional date information
|
|
47
|
+
* @category Logging
|
|
48
|
+
* @function prettySuccess
|
|
49
|
+
* @param success - Success message to display
|
|
50
|
+
* @param time - Include or not a timestamp
|
|
51
|
+
* @example
|
|
52
|
+
* prettySuccess("User created successfully");
|
|
53
|
+
* // Output: ✔ - User created successfully (in green)
|
|
54
|
+
*
|
|
55
|
+
* prettySuccess("Data saved", true);
|
|
56
|
+
* // Output: ✔ [30/03/2026 10:30:45] - Data saved (in green with timestamp)
|
|
57
|
+
*/
|
|
58
|
+
function prettySuccess(success: string, time = false): void {
|
|
59
|
+
const date = getDate(time);
|
|
60
|
+
console.log(`\x1b[1;42m ✔ ${date} - ${success} \x1b[0m`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Show a pretty information log message with optional date information
|
|
65
|
+
* @category Logging
|
|
66
|
+
* @function prettyInfo
|
|
67
|
+
* @param info - Information message to display
|
|
68
|
+
* @param time - Include or not a timestamp
|
|
69
|
+
* @example
|
|
70
|
+
* prettyInfo("Server is running on port 3000");
|
|
71
|
+
* // Output: ℹ - Server is running on port 3000 (in blue)
|
|
72
|
+
*
|
|
73
|
+
* prettyInfo("Cache cleared", true);
|
|
74
|
+
* // Output: ℹ [30/03/2026 10:30:45] - Cache cleared (in blue with timestamp)
|
|
75
|
+
*/
|
|
76
|
+
function prettyInfo(info: string, time = false): void {
|
|
77
|
+
const date = getDate(time);
|
|
78
|
+
console.log(`\x1b[1;44m ℹ ${date} - ${info} \x1b[0m`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Show a pretty debug log message with optional date information
|
|
83
|
+
* @category Logging
|
|
84
|
+
* @function prettyDebug
|
|
85
|
+
* @param debug - Debug message to display
|
|
86
|
+
* @param time - Include or not a timestamp
|
|
87
|
+
* @example
|
|
88
|
+
* prettyDebug("Variable x = 42");
|
|
89
|
+
* // Output: ⚙ - Variable x = 42 (in white)
|
|
90
|
+
*
|
|
91
|
+
* prettyDebug("Function call trace", true);
|
|
92
|
+
* // Output: ⚙ [30/03/2026 10:30:45] - Function call trace (in white with timestamp)
|
|
93
|
+
*/
|
|
94
|
+
function prettyDebug(debug: string, time = false): void {
|
|
95
|
+
const date = getDate(time);
|
|
96
|
+
console.log(`\x1b[1;97m ⚙ ${date} - ${debug} \x1b[0m`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Show a separator line in console
|
|
101
|
+
* @category Logging
|
|
102
|
+
* @function logSeparator
|
|
103
|
+
* @example
|
|
104
|
+
* logSeparator();
|
|
105
|
+
* // Output: ══════════════════════════════════════════════════
|
|
106
|
+
*/
|
|
107
|
+
function logSeparator(): void {
|
|
108
|
+
console.log(`\x1b[1m${'═'.repeat(50)}\x1b[0m`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Display a header with a title in the center
|
|
113
|
+
* @category Logging
|
|
114
|
+
* @function logHeader
|
|
115
|
+
* @param title - The title of the header
|
|
116
|
+
* @example
|
|
117
|
+
* logHeader("Welcome");
|
|
118
|
+
* // Output:
|
|
119
|
+
* // ╔═════════╗
|
|
120
|
+
* // ║ Welcome ║
|
|
121
|
+
* // ╚═════════╝
|
|
122
|
+
*/
|
|
123
|
+
function logHeader(title: string): void {
|
|
124
|
+
console.log(`\x1b[1m╔${'═'.repeat(title.length + 2)}╗\x1b[0m`);
|
|
125
|
+
console.log(`\x1b[1m║ ${title} ║\x1b[0m`);
|
|
126
|
+
console.log(`\x1b[1m╚${'═'.repeat(title.length + 2)}╝\x1b[0m`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export {prettyError, prettyWarn, prettySuccess, prettyInfo, prettyDebug, logSeparator, logHeader};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a deep clone of any object or array
|
|
3
|
+
* @category Object Manipulation
|
|
4
|
+
* @function deepClone
|
|
5
|
+
* @param obj - The object or array to clone
|
|
6
|
+
* @returns A deep clone of the object
|
|
7
|
+
* @example
|
|
8
|
+
* const user = { name: "Alice", skills: ["JS", "TS"] };
|
|
9
|
+
* const clone = deepClone(user);
|
|
10
|
+
* clone.name = "Bob";
|
|
11
|
+
* clone.skills[0] = "Python";
|
|
12
|
+
* console.log(user.name); // "Alice" (unchanged)
|
|
13
|
+
* console.log(user.skills[0]); // "JS" (unchanged)
|
|
14
|
+
*/
|
|
15
|
+
function deepClone<T>(obj: T): T {
|
|
16
|
+
if (obj === null || typeof obj !== "object") return obj;
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(obj)) return obj.map(item => deepClone(item)) as T;
|
|
19
|
+
|
|
20
|
+
const cloned = {} as T;
|
|
21
|
+
for (const key in obj) {
|
|
22
|
+
if (obj.hasOwnProperty(key)) cloned[key] = deepClone(obj[key]);
|
|
23
|
+
}
|
|
24
|
+
return cloned;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Remove duplicate values from an array
|
|
29
|
+
* @category Object Manipulation
|
|
30
|
+
* @function noTwins
|
|
31
|
+
* @param arr - The array to remove duplicates from
|
|
32
|
+
* @returns {unknown[]} A new array with unique values only
|
|
33
|
+
* @example
|
|
34
|
+
* noTwins([1, 2, 2, 3, 3, 3]); // [1, 2, 3]
|
|
35
|
+
* noTwins(["a", "b", "a", "c"]); // ["a", "b", "c"]
|
|
36
|
+
* noTwins([1, "1", 1, "1"]); // [1, "1"]
|
|
37
|
+
*/
|
|
38
|
+
function noTwins(arr: unknown[]): unknown[] {
|
|
39
|
+
return Array.from(new Set(arr));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export {deepClone, noTwins};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { isEmpty } from './validation';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transform a string to camelCase format
|
|
5
|
+
* @category Transformation
|
|
6
|
+
* @function camelify
|
|
7
|
+
* @param str - The string to transform
|
|
8
|
+
* @returns {string} The string in camelCase format
|
|
9
|
+
* @example
|
|
10
|
+
* camelify("hello world"); // "helloWorld"
|
|
11
|
+
* camelify("C'est un test"); // "cestUnTest"
|
|
12
|
+
* camelify("foo bar baz"); // "fooBarBaz"
|
|
13
|
+
*/
|
|
14
|
+
function camelify(str: string): string {
|
|
15
|
+
let camelCased: string = "";
|
|
16
|
+
if (!isEmpty(str)) {
|
|
17
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
18
|
+
let toCamelify = cleaned.split(" ");
|
|
19
|
+
|
|
20
|
+
camelCased = toCamelify.map((word, index) => {
|
|
21
|
+
if (index === 0) return word.toLowerCase();
|
|
22
|
+
return word[0].toUpperCase() + word.slice(1).toLowerCase();
|
|
23
|
+
})
|
|
24
|
+
.join('');
|
|
25
|
+
}
|
|
26
|
+
return camelCased;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Transform a string to kebab-case format
|
|
31
|
+
* @category Transformation
|
|
32
|
+
* @function kebabify
|
|
33
|
+
* @param str - The string to transform
|
|
34
|
+
* @returns {string} The string in kebab-case format
|
|
35
|
+
* @example
|
|
36
|
+
* kebabify("hello world"); // "hello-world"
|
|
37
|
+
* kebabify("C'est un test"); // "ceststun-test"
|
|
38
|
+
* kebabify("foo bar baz"); // "foo-bar-baz"
|
|
39
|
+
*/
|
|
40
|
+
function kebabify(str: string): string {
|
|
41
|
+
let kebabised: string = "";
|
|
42
|
+
if (!isEmpty(str)) {
|
|
43
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
44
|
+
let toKebabify = cleaned.split(" ");
|
|
45
|
+
kebabised = toKebabify.map(word => word.toLowerCase())
|
|
46
|
+
.join("-");
|
|
47
|
+
}
|
|
48
|
+
return kebabised;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Transform a string to snake_case format
|
|
53
|
+
* @category Transformation
|
|
54
|
+
* @function snakify
|
|
55
|
+
* @param str - The string to transform
|
|
56
|
+
* @returns {string} The string in snake_case format
|
|
57
|
+
* @example
|
|
58
|
+
* snakify("hello world"); // "hello_world"
|
|
59
|
+
* snakify("C'est un test"); // "ceststun_test"
|
|
60
|
+
* snakify("foo bar baz"); // "foo_bar_baz"
|
|
61
|
+
*/
|
|
62
|
+
function snakify(str: string): string {
|
|
63
|
+
let snaked: string = "";
|
|
64
|
+
if (!isEmpty(str)) {
|
|
65
|
+
let cleaned = str.replace(/[^a-zA-Z0-9\s]/g, "");
|
|
66
|
+
let toSnakify = cleaned.split(" ");
|
|
67
|
+
snaked = toSnakify.map(word => word.toLowerCase())
|
|
68
|
+
.join("_");
|
|
69
|
+
}
|
|
70
|
+
return snaked;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Capitalize the first character of a string
|
|
75
|
+
* @category Transformation
|
|
76
|
+
* @function capitalize
|
|
77
|
+
* @param str - The string to capitalize
|
|
78
|
+
* @returns {string} The string with the first character in uppercase
|
|
79
|
+
* @example
|
|
80
|
+
* capitalize("hello"); // "Hello"
|
|
81
|
+
* capitalize("hello world"); // "Hello world"
|
|
82
|
+
* capitalize(""); // ""
|
|
83
|
+
*/
|
|
84
|
+
function capitalize(str: string): string {
|
|
85
|
+
if (isEmpty(str)) return str;
|
|
86
|
+
return str[0].toUpperCase() + str.slice(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export {camelify, kebabify, snakify, capitalize};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if any type of variable is empty
|
|
3
|
+
* @category Validation
|
|
4
|
+
* @function isEmpty
|
|
5
|
+
* @param value - The variable to check
|
|
6
|
+
* @returns {boolean} True if the value is empty, false otherwise
|
|
7
|
+
* @example
|
|
8
|
+
* isEmpty(""); // true
|
|
9
|
+
* isEmpty(" "); // true
|
|
10
|
+
* isEmpty(null); // true
|
|
11
|
+
* isEmpty([]); // true
|
|
12
|
+
* isEmpty({}); // true
|
|
13
|
+
* isEmpty("hello"); // false
|
|
14
|
+
* isEmpty([1, 2]); // false
|
|
15
|
+
*/
|
|
16
|
+
function isEmpty(value: unknown): boolean {
|
|
17
|
+
if (value === null || value === undefined) return true;
|
|
18
|
+
if (typeof value === "string") return value.trim().length === 0;
|
|
19
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
20
|
+
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check if the variable is of the chosen type
|
|
26
|
+
* @category Validation
|
|
27
|
+
* @function isType
|
|
28
|
+
* @param value - The variable to check
|
|
29
|
+
* @param type - The type you want
|
|
30
|
+
* @returns {boolean} True if the value is of the chosen type, false otherwise
|
|
31
|
+
* @example
|
|
32
|
+
* isType("hello", "string"); // true
|
|
33
|
+
* isType(42, "number"); // true
|
|
34
|
+
* isType([], "object"); // true
|
|
35
|
+
* isType("42", "number"); // false
|
|
36
|
+
*/
|
|
37
|
+
function isType(value: unknown, type: string): boolean {
|
|
38
|
+
return typeof value === type;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Check if an email is valid
|
|
43
|
+
* @category Validation
|
|
44
|
+
* @function isEmail
|
|
45
|
+
* @param email - The email to check
|
|
46
|
+
* @returns {boolean} True if the email is valid, false otherwise
|
|
47
|
+
* @example
|
|
48
|
+
* isEmail("alice@example.com"); // true
|
|
49
|
+
* isEmail("bob.smith@company.co.uk"); // true
|
|
50
|
+
* isEmail("invalid@.com"); // false
|
|
51
|
+
* isEmail("no-at-sign.com"); // false
|
|
52
|
+
*/
|
|
53
|
+
function isEmail(email: string): boolean {
|
|
54
|
+
const regex = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
55
|
+
return regex.test(email);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Check if a URL is valid
|
|
60
|
+
* @category Validation
|
|
61
|
+
* @function isURL
|
|
62
|
+
* @param url - The URL to check
|
|
63
|
+
* @returns {boolean} True if the URL is valid, false otherwise
|
|
64
|
+
* @example
|
|
65
|
+
* isURL("https://www.example.com"); // true
|
|
66
|
+
* isURL("http://example.com/path"); // true
|
|
67
|
+
* isURL("www.example.com"); // false (missing protocol)
|
|
68
|
+
* isURL("not a url"); // false
|
|
69
|
+
*/
|
|
70
|
+
function isURL(url: string): boolean {
|
|
71
|
+
try {
|
|
72
|
+
new URL(url);
|
|
73
|
+
return true;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export {isEmpty, isType, isEmail, isURL};
|