@excofy/utils 1.0.1 → 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/dist/index.cjs +15 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +15 -0
- package/package.json +3 -1
- package/src/helpers/number.ts +31 -0
- package/src/helpers/slug.ts +50 -0
- package/src/helpers/validator.ts +18 -0
package/dist/index.cjs
CHANGED
|
@@ -311,6 +311,21 @@ function createValidator() {
|
|
|
311
311
|
}
|
|
312
312
|
return validator;
|
|
313
313
|
},
|
|
314
|
+
url(message) {
|
|
315
|
+
if (shouldSkipValidation()) {
|
|
316
|
+
return validator;
|
|
317
|
+
}
|
|
318
|
+
if (typeof current.value === "string") {
|
|
319
|
+
try {
|
|
320
|
+
new URL(current.value);
|
|
321
|
+
} catch {
|
|
322
|
+
current.pushError(message);
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
current.pushError(message);
|
|
326
|
+
}
|
|
327
|
+
return validator;
|
|
328
|
+
},
|
|
314
329
|
uuid(message) {
|
|
315
330
|
if (shouldSkipValidation()) {
|
|
316
331
|
return validator;
|
package/dist/index.d.cts
CHANGED
|
@@ -55,6 +55,7 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
55
55
|
slug(message: string): /*elided*/ any;
|
|
56
56
|
sanitize(): /*elided*/ any;
|
|
57
57
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
58
|
+
url(message: string): /*elided*/ any;
|
|
58
59
|
uuid(message: string): /*elided*/ any;
|
|
59
60
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
60
61
|
videoUrl: {
|
|
@@ -78,6 +79,7 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
78
79
|
slug(message: string): /*elided*/ any;
|
|
79
80
|
sanitize(): /*elided*/ any;
|
|
80
81
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
82
|
+
url(message: string): /*elided*/ any;
|
|
81
83
|
uuid(message: string): /*elided*/ any;
|
|
82
84
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
83
85
|
videoUrl: {
|
package/dist/index.d.ts
CHANGED
|
@@ -55,6 +55,7 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
55
55
|
slug(message: string): /*elided*/ any;
|
|
56
56
|
sanitize(): /*elided*/ any;
|
|
57
57
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
58
|
+
url(message: string): /*elided*/ any;
|
|
58
59
|
uuid(message: string): /*elided*/ any;
|
|
59
60
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
60
61
|
videoUrl: {
|
|
@@ -78,6 +79,7 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
78
79
|
slug(message: string): /*elided*/ any;
|
|
79
80
|
sanitize(): /*elided*/ any;
|
|
80
81
|
sanitizeHTML(tags?: AllowedTag[]): /*elided*/ any;
|
|
82
|
+
url(message: string): /*elided*/ any;
|
|
81
83
|
uuid(message: string): /*elided*/ any;
|
|
82
84
|
oneOf: (types: string[], message: string) => /*elided*/ any;
|
|
83
85
|
videoUrl: {
|
package/dist/index.js
CHANGED
|
@@ -284,6 +284,21 @@ function createValidator() {
|
|
|
284
284
|
}
|
|
285
285
|
return validator;
|
|
286
286
|
},
|
|
287
|
+
url(message) {
|
|
288
|
+
if (shouldSkipValidation()) {
|
|
289
|
+
return validator;
|
|
290
|
+
}
|
|
291
|
+
if (typeof current.value === "string") {
|
|
292
|
+
try {
|
|
293
|
+
new URL(current.value);
|
|
294
|
+
} catch {
|
|
295
|
+
current.pushError(message);
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
current.pushError(message);
|
|
299
|
+
}
|
|
300
|
+
return validator;
|
|
301
|
+
},
|
|
287
302
|
uuid(message) {
|
|
288
303
|
if (shouldSkipValidation()) {
|
|
289
304
|
return validator;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@excofy/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Biblioteca de utilitários para o Excofy",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -30,11 +30,13 @@
|
|
|
30
30
|
"homepage": "https://github.com/excofy/utils#readme",
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@biomejs/biome": "^1.9.4",
|
|
33
|
+
"@types/big.js": "^6.2.2",
|
|
33
34
|
"@types/node": "^24.0.3",
|
|
34
35
|
"tsup": "^8.5.0",
|
|
35
36
|
"typescript": "^5.8.3"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
39
|
+
"big.js": "^7.0.1",
|
|
38
40
|
"xss": "^1.0.15"
|
|
39
41
|
},
|
|
40
42
|
"publishConfig": {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a decimal number to an integer by multiplying it by 100.
|
|
5
|
+
* Commonly used to convert monetary values from dollars to cents.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} value - The decimal number to convert (e.g., 10.90).
|
|
8
|
+
* @returns {number} The integer value in cents (e.g., 1090).
|
|
9
|
+
*/
|
|
10
|
+
export const toCents = (value?: number): number => {
|
|
11
|
+
if (!value) {
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return new Big(value).mul(100).round(0).toNumber();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Converts an integer back to a decimal by dividing it by 100.
|
|
20
|
+
* Commonly used to convert monetary values from cents to dollars.
|
|
21
|
+
*
|
|
22
|
+
* @param {number} value - The integer value in cents (e.g., 1090).
|
|
23
|
+
* @returns {number} The decimal value (e.g., 10.90).
|
|
24
|
+
*/
|
|
25
|
+
export const toDecimal = (value?: number): number => {
|
|
26
|
+
if (!value) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return new Big(value).div(100).round(2).toNumber();
|
|
31
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a unique slug by appending a numeric suffix to the base slug if necessary.
|
|
3
|
+
*
|
|
4
|
+
* If the base slug is already used (either exactly or with a numeric suffix),
|
|
5
|
+
* the function finds the highest existing suffix and returns the next one in sequence.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* generateUniqueSlug('my-product', ['my-product', 'my-product-1', 'my-product-2'])
|
|
9
|
+
* // returns 'my-product-3'
|
|
10
|
+
*
|
|
11
|
+
* generateUniqueSlug('item', ['item-1', 'item-2'])
|
|
12
|
+
* // returns 'item'
|
|
13
|
+
*
|
|
14
|
+
* @param {string} baseSlug - The initial slug to use as a base.
|
|
15
|
+
* @param {string[]} existingSlugs - A list of existing slugs to avoid duplicates.
|
|
16
|
+
* @returns {string} A unique slug not present in the existingSlugs list.
|
|
17
|
+
*/
|
|
18
|
+
export const generateUniqueSlug = (
|
|
19
|
+
baseSlug: string,
|
|
20
|
+
existingSlugs: string[]
|
|
21
|
+
): string => {
|
|
22
|
+
const suffixes = existingSlugs
|
|
23
|
+
.map((slug) => {
|
|
24
|
+
const match = slug.match(new RegExp(`^${baseSlug}-(\\d+)$`));
|
|
25
|
+
return match ? Number(match[1]) : slug === baseSlug ? 0 : null;
|
|
26
|
+
})
|
|
27
|
+
.filter((num): num is number => num !== null);
|
|
28
|
+
|
|
29
|
+
const nextSuffix = suffixes.length > 0 ? Math.max(...suffixes) + 1 : 0;
|
|
30
|
+
|
|
31
|
+
return nextSuffix === 0 ? baseSlug : `${baseSlug}-${nextSuffix}`;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Removes a trailing numeric suffix from a slug, if present.
|
|
36
|
+
*
|
|
37
|
+
* This is useful for extracting the base part of a slug before checking
|
|
38
|
+
* or generating new unique variations.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* removeTrailingNumber('product-3') // returns 'product'
|
|
42
|
+
* removeTrailingNumber('item') // returns 'item'
|
|
43
|
+
*
|
|
44
|
+
* @param {string} slug - The slug from which to remove the trailing number.
|
|
45
|
+
* @returns {string} The slug without the trailing numeric suffix.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
export const removeTrailingNumber = (slug: string): string => {
|
|
49
|
+
return slug.replace(/-\d+$/, '');
|
|
50
|
+
};
|
package/src/helpers/validator.ts
CHANGED
|
@@ -292,6 +292,24 @@ export function createValidator<
|
|
|
292
292
|
return validator;
|
|
293
293
|
},
|
|
294
294
|
|
|
295
|
+
url(message: string) {
|
|
296
|
+
if (shouldSkipValidation()) {
|
|
297
|
+
return validator;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (typeof current.value === 'string') {
|
|
301
|
+
try {
|
|
302
|
+
new URL(current.value);
|
|
303
|
+
} catch {
|
|
304
|
+
current.pushError(message);
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
current.pushError(message);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return validator;
|
|
311
|
+
},
|
|
312
|
+
|
|
295
313
|
uuid(message: string) {
|
|
296
314
|
if (shouldSkipValidation()) {
|
|
297
315
|
return validator;
|