@germondai/ts-utils 0.0.1 → 0.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/dist/module.js +87 -14
- package/dist/runtime/convertor.d.ts +8 -0
- package/dist/runtime/errors.d.ts +7 -3
- package/dist/runtime/regex.d.ts +0 -11
- package/dist/runtime/size.d.ts +23 -0
- package/dist/runtime/time.d.ts +12 -0
- package/dist/runtime/type.d.ts +14 -0
- package/package.json +1 -1
package/dist/module.js
CHANGED
|
@@ -61,14 +61,29 @@ var escapeHTML = (str) => {
|
|
|
61
61
|
};
|
|
62
62
|
return str.replace(/[&<>"']/g, (char) => escapeMap[char] || char);
|
|
63
63
|
};
|
|
64
|
+
var unescapeHTML = (str) => {
|
|
65
|
+
const unescapeMap = {
|
|
66
|
+
"&": "&",
|
|
67
|
+
"<": "<",
|
|
68
|
+
">": ">",
|
|
69
|
+
""": '"',
|
|
70
|
+
"'": "'"
|
|
71
|
+
};
|
|
72
|
+
return str.replace(/&[a-zA-Z0-9#]+;/g, (entity) => unescapeMap[entity] || entity);
|
|
73
|
+
};
|
|
64
74
|
// src/runtime/data.ts
|
|
65
75
|
var clone = (data) => JSON.parse(JSON.stringify(data));
|
|
66
76
|
// src/runtime/errors.ts
|
|
67
|
-
var catchError = (promise, errorsToCatch) =>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
77
|
+
var catchError = async (promise, errorsToCatch) => {
|
|
78
|
+
try {
|
|
79
|
+
const data = await promise();
|
|
80
|
+
return { data, ok: true };
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (errorsToCatch === undefined || errorsToCatch.some((e) => error instanceof e))
|
|
83
|
+
return { error, ok: false };
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
72
87
|
// src/runtime/math.ts
|
|
73
88
|
var rand = (n, m = 0) => Math.floor(Math.random() * (m - n + 1)) + n;
|
|
74
89
|
var percentage = (value, maxValue, decimalPlaces = 2) => {
|
|
@@ -97,14 +112,6 @@ var isCreditCard = (value) => /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:0
|
|
|
97
112
|
var isDomain = (value) => /^(?!:\/\/)([a-zA-Z0-9-_]{1,63}\.)+[a-zA-Z]{2,63}$/.test(value);
|
|
98
113
|
var isPostalCode = (value) => /^[A-Za-z0-9\s-]{3,10}$/.test(value);
|
|
99
114
|
var isISODate = (value) => /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/.test(value);
|
|
100
|
-
var isJson = (value) => {
|
|
101
|
-
try {
|
|
102
|
-
JSON.parse(value);
|
|
103
|
-
return true;
|
|
104
|
-
} catch {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
115
|
var isBase64 = (value) => /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value);
|
|
109
116
|
var isSlug = (value) => /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value);
|
|
110
117
|
// src/runtime/size.ts
|
|
@@ -118,6 +125,29 @@ var formatBytes = (bytes, decimals = 2) => {
|
|
|
118
125
|
const size = parseFloat((bytes / Math.pow(k, i)).toFixed(dm));
|
|
119
126
|
return `${size} ${sizes[i]}`;
|
|
120
127
|
};
|
|
128
|
+
var toBytes = (size) => {
|
|
129
|
+
const match = size.trim().match(/^(\d+\.?\d*)\s*(B|KB|MB|GB|TB)$/i);
|
|
130
|
+
if (!match)
|
|
131
|
+
return;
|
|
132
|
+
const value = Number(match[1]);
|
|
133
|
+
const unit = match[2]?.toUpperCase();
|
|
134
|
+
if (isNaN(value) || value < 0)
|
|
135
|
+
return;
|
|
136
|
+
switch (unit) {
|
|
137
|
+
case "B":
|
|
138
|
+
return value;
|
|
139
|
+
case "KB":
|
|
140
|
+
return value * 1024;
|
|
141
|
+
case "MB":
|
|
142
|
+
return value * 1024 * 1024;
|
|
143
|
+
case "GB":
|
|
144
|
+
return value * 1024 * 1024 * 1024;
|
|
145
|
+
case "TB":
|
|
146
|
+
return value * 1024 * 1024 * 1024 * 1024;
|
|
147
|
+
default:
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
121
151
|
// src/runtime/time.ts
|
|
122
152
|
var formatTime = (seconds) => {
|
|
123
153
|
const hours = Math.floor(seconds / 3600);
|
|
@@ -144,6 +174,36 @@ var formatDuration = (ms) => {
|
|
|
144
174
|
return `${min}m ${sec % 60}s`;
|
|
145
175
|
return `${sec}s`;
|
|
146
176
|
};
|
|
177
|
+
var toSeconds = (time) => {
|
|
178
|
+
time = time?.trim();
|
|
179
|
+
if (!time)
|
|
180
|
+
return;
|
|
181
|
+
if (/^\d+$/.test(time)) {
|
|
182
|
+
const seconds2 = Number(time);
|
|
183
|
+
if (isNaN(seconds2))
|
|
184
|
+
return;
|
|
185
|
+
return seconds2;
|
|
186
|
+
}
|
|
187
|
+
if (time.includes(":")) {
|
|
188
|
+
const parts = time.split(":").map(Number);
|
|
189
|
+
if (parts.some(isNaN))
|
|
190
|
+
return;
|
|
191
|
+
if (parts.length === 2)
|
|
192
|
+
return (parts[0] || 0) * 60 + (parts[1] || 0);
|
|
193
|
+
else if (parts.length === 3)
|
|
194
|
+
return (parts[0] || 0) * 3600 + (parts[1] || 0) * 60 + (parts[2] || 0);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const match = time.match(/(?:(\d+)h)?\s*(?:(\d+)m)?\s*(?:(\d+)s)?/i);
|
|
198
|
+
if (!match || !match.some((v, i) => i > 0 && v !== undefined))
|
|
199
|
+
return;
|
|
200
|
+
const hours = Number(match[1] || "0");
|
|
201
|
+
const minutes = Number(match[2] || "0");
|
|
202
|
+
const seconds = Number(match[3] || "0");
|
|
203
|
+
if (isNaN(hours) || isNaN(minutes) || isNaN(seconds))
|
|
204
|
+
return;
|
|
205
|
+
return hours * 3600 + minutes * 60 + seconds;
|
|
206
|
+
};
|
|
147
207
|
// src/runtime/type.ts
|
|
148
208
|
var isPrimitive = (value) => value === null || value === undefined || typeof value !== "object" && typeof value !== "function";
|
|
149
209
|
var isObject = (value) => value !== null && typeof value === "object" && value.constructor === Object;
|
|
@@ -157,6 +217,16 @@ var isEmpty = (value) => {
|
|
|
157
217
|
return Object.keys(value).length === 0;
|
|
158
218
|
return false;
|
|
159
219
|
};
|
|
220
|
+
var isJSON = (value) => {
|
|
221
|
+
if (typeof value !== "string")
|
|
222
|
+
return false;
|
|
223
|
+
try {
|
|
224
|
+
JSON.parse(value);
|
|
225
|
+
return true;
|
|
226
|
+
} catch {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
160
230
|
// src/runtime/url.ts
|
|
161
231
|
var getQueryParams = (urlString) => {
|
|
162
232
|
const url = new URL(urlString);
|
|
@@ -192,15 +262,18 @@ var buildUrl = (baseUrl, path, queryParams) => {
|
|
|
192
262
|
export {
|
|
193
263
|
updateQueryParam,
|
|
194
264
|
uniqueArray,
|
|
265
|
+
unescapeHTML,
|
|
195
266
|
truncate,
|
|
196
267
|
toggleCase,
|
|
197
268
|
toTitleCase,
|
|
198
269
|
toSnakeCase,
|
|
199
270
|
toSentenceCase,
|
|
271
|
+
toSeconds,
|
|
200
272
|
toPascalCase,
|
|
201
273
|
toKebabCase,
|
|
202
274
|
toConstantCase,
|
|
203
275
|
toCamelCase,
|
|
276
|
+
toBytes,
|
|
204
277
|
stripTags,
|
|
205
278
|
slugify,
|
|
206
279
|
sleep,
|
|
@@ -216,7 +289,7 @@ export {
|
|
|
216
289
|
isPhoneNumber,
|
|
217
290
|
isObject,
|
|
218
291
|
isMacAddress,
|
|
219
|
-
|
|
292
|
+
isJSON,
|
|
220
293
|
isISODate,
|
|
221
294
|
isIPv6,
|
|
222
295
|
isIPv4,
|
|
@@ -104,3 +104,11 @@ export declare const stripTags: (html: string) => string;
|
|
|
104
104
|
* @returns The HTML-escaped string.
|
|
105
105
|
*/
|
|
106
106
|
export declare const escapeHTML: (str: string) => string;
|
|
107
|
+
/**
|
|
108
|
+
* Unescapes HTML entities in a string.
|
|
109
|
+
* Converts HTML entities like `&`, `<`, `>`, `"`, and `'` back to their original characters.
|
|
110
|
+
*
|
|
111
|
+
* @param str - The input string to unescape.
|
|
112
|
+
* @returns The unescaped string.
|
|
113
|
+
*/
|
|
114
|
+
export declare const unescapeHTML: (str: string) => string;
|
package/dist/runtime/errors.d.ts
CHANGED
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
* @returns A promise resolving to an object with either the data or an error.
|
|
12
12
|
*/
|
|
13
13
|
export declare const catchError: <T, E extends new (message?: string) => Error>(promise: () => Promise<T>, errorsToCatch?: E[]) => Promise<{
|
|
14
|
-
data
|
|
15
|
-
ok:
|
|
16
|
-
error?:
|
|
14
|
+
data: T;
|
|
15
|
+
ok: true;
|
|
16
|
+
error?: never;
|
|
17
|
+
} | {
|
|
18
|
+
data?: never;
|
|
19
|
+
ok: false;
|
|
20
|
+
error: InstanceType<E>;
|
|
17
21
|
}>;
|
package/dist/runtime/regex.d.ts
CHANGED
|
@@ -145,17 +145,6 @@ export declare const isPostalCode: (value: string) => boolean;
|
|
|
145
145
|
* isISODate('2024-13-01'); // false
|
|
146
146
|
*/
|
|
147
147
|
export declare const isISODate: (value: string) => boolean;
|
|
148
|
-
/**
|
|
149
|
-
* Checks if a given string is a valid JSON string.
|
|
150
|
-
*
|
|
151
|
-
* @param value - The string to validate.
|
|
152
|
-
* @returns True if the string is valid JSON, false otherwise.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* isJson('{"name": "John"}'); // true
|
|
156
|
-
* isJson('{invalid}'); // false
|
|
157
|
-
*/
|
|
158
|
-
export declare const isJson: (value: string) => boolean;
|
|
159
148
|
/**
|
|
160
149
|
* Checks if a given string is a valid Base64-encoded string.
|
|
161
150
|
*
|
package/dist/runtime/size.d.ts
CHANGED
|
@@ -8,3 +8,26 @@
|
|
|
8
8
|
* @returns A string representing the file size (e.g., "1.23 MB").
|
|
9
9
|
*/
|
|
10
10
|
export declare const formatBytes: (bytes: number, decimals?: number) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Converts a human-readable size string into its equivalent size in bytes.
|
|
13
|
+
*
|
|
14
|
+
* @param size - The size string to convert. It should be in the format of a number
|
|
15
|
+
* followed by a unit (e.g., "10 KB", "5.5 MB", "1 GB"). Supported units are:
|
|
16
|
+
* - B (bytes)
|
|
17
|
+
* - KB (kilobytes)
|
|
18
|
+
* - MB (megabytes)
|
|
19
|
+
* - GB (gigabytes)
|
|
20
|
+
* - TB (terabytes)
|
|
21
|
+
*
|
|
22
|
+
* @returns The size in bytes as a number, or `undefined` if the input format is invalid
|
|
23
|
+
* or the value is negative.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* toBytes("10 KB") // Returns 10240
|
|
28
|
+
* toBytes("5.5 MB") // Returns 5767168
|
|
29
|
+
* toBytes("1 GB") // Returns 1073741824
|
|
30
|
+
* toBytes("invalid") // Returns undefined
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const toBytes: (size: string) => number | undefined;
|
package/dist/runtime/time.d.ts
CHANGED
|
@@ -16,3 +16,15 @@ export declare const formatTime: (seconds: number) => string;
|
|
|
16
16
|
* @returns A formatted string (e.g., "2h 15m 30s").
|
|
17
17
|
*/
|
|
18
18
|
export declare const formatDuration: (ms: number) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a time string into seconds.
|
|
21
|
+
*
|
|
22
|
+
* Supports various formats:
|
|
23
|
+
* - Direct seconds (e.g., '2947')
|
|
24
|
+
* - MM:SS or HH:MM:SS
|
|
25
|
+
* - 'Xh Ym Zs' format (e.g., '2h 15m 30s')
|
|
26
|
+
*
|
|
27
|
+
* @param time - The time string to convert.
|
|
28
|
+
* @returns The total number of seconds, or undefined if the format is invalid.
|
|
29
|
+
*/
|
|
30
|
+
export declare const toSeconds: (time: string) => number | undefined;
|
package/dist/runtime/type.d.ts
CHANGED
|
@@ -34,3 +34,17 @@ export declare const isArray: <T>(value: unknown) => value is T[];
|
|
|
34
34
|
* @returns True if the value is empty, false otherwise.
|
|
35
35
|
*/
|
|
36
36
|
export declare const isEmpty: (value: unknown) => boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Determines whether a value is valid JSON.
|
|
39
|
+
*
|
|
40
|
+
* This function checks if a value can be parsed as JSON.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The value to check.
|
|
43
|
+
* @returns True if the value is valid JSON, false otherwise.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* isJson('{"name": "John"}'); // true
|
|
47
|
+
* isJson('{invalid}'); // false
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
export declare const isJSON: (value: string) => boolean;
|