@excofy/utils 1.0.2 → 1.0.3
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 +54 -2
- package/dist/index.d.cts +63 -1
- package/dist/index.d.ts +63 -1
- package/dist/index.js +47 -1
- package/package.json +1 -1
- package/src/index.ts +3 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,13 +17,23 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
21
31
|
var index_exports = {};
|
|
22
32
|
__export(index_exports, {
|
|
23
33
|
createValidator: () => createValidator,
|
|
24
|
-
htmlEntityDecode: () => htmlEntityDecode
|
|
34
|
+
htmlEntityDecode: () => htmlEntityDecode,
|
|
35
|
+
numberUtils: () => number_exports,
|
|
36
|
+
slugUtils: () => slug_exports
|
|
25
37
|
});
|
|
26
38
|
module.exports = __toCommonJS(index_exports);
|
|
27
39
|
|
|
@@ -467,8 +479,48 @@ function createValidator() {
|
|
|
467
479
|
}
|
|
468
480
|
};
|
|
469
481
|
}
|
|
482
|
+
|
|
483
|
+
// src/helpers/slug.ts
|
|
484
|
+
var slug_exports = {};
|
|
485
|
+
__export(slug_exports, {
|
|
486
|
+
generateUniqueSlug: () => generateUniqueSlug,
|
|
487
|
+
removeTrailingNumber: () => removeTrailingNumber
|
|
488
|
+
});
|
|
489
|
+
var generateUniqueSlug = (baseSlug, existingSlugs) => {
|
|
490
|
+
const suffixes = existingSlugs.map((slug) => {
|
|
491
|
+
const match = slug.match(new RegExp(`^${baseSlug}-(\\d+)$`));
|
|
492
|
+
return match ? Number(match[1]) : slug === baseSlug ? 0 : null;
|
|
493
|
+
}).filter((num) => num !== null);
|
|
494
|
+
const nextSuffix = suffixes.length > 0 ? Math.max(...suffixes) + 1 : 0;
|
|
495
|
+
return nextSuffix === 0 ? baseSlug : `${baseSlug}-${nextSuffix}`;
|
|
496
|
+
};
|
|
497
|
+
var removeTrailingNumber = (slug) => {
|
|
498
|
+
return slug.replace(/-\d+$/, "");
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// src/helpers/number.ts
|
|
502
|
+
var number_exports = {};
|
|
503
|
+
__export(number_exports, {
|
|
504
|
+
toCents: () => toCents,
|
|
505
|
+
toDecimal: () => toDecimal
|
|
506
|
+
});
|
|
507
|
+
var import_big = __toESM(require("big.js"), 1);
|
|
508
|
+
var toCents = (value) => {
|
|
509
|
+
if (!value) {
|
|
510
|
+
return 0;
|
|
511
|
+
}
|
|
512
|
+
return new import_big.default(value).mul(100).round(0).toNumber();
|
|
513
|
+
};
|
|
514
|
+
var toDecimal = (value) => {
|
|
515
|
+
if (!value) {
|
|
516
|
+
return 0;
|
|
517
|
+
}
|
|
518
|
+
return new import_big.default(value).div(100).round(2).toNumber();
|
|
519
|
+
};
|
|
470
520
|
// Annotate the CommonJS export names for ESM import in node:
|
|
471
521
|
0 && (module.exports = {
|
|
472
522
|
createValidator,
|
|
473
|
-
htmlEntityDecode
|
|
523
|
+
htmlEntityDecode,
|
|
524
|
+
numberUtils,
|
|
525
|
+
slugUtils
|
|
474
526
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -93,4 +93,66 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
93
93
|
};
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Generates a unique slug by appending a numeric suffix to the base slug if necessary.
|
|
98
|
+
*
|
|
99
|
+
* If the base slug is already used (either exactly or with a numeric suffix),
|
|
100
|
+
* the function finds the highest existing suffix and returns the next one in sequence.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* generateUniqueSlug('my-product', ['my-product', 'my-product-1', 'my-product-2'])
|
|
104
|
+
* // returns 'my-product-3'
|
|
105
|
+
*
|
|
106
|
+
* generateUniqueSlug('item', ['item-1', 'item-2'])
|
|
107
|
+
* // returns 'item'
|
|
108
|
+
*
|
|
109
|
+
* @param {string} baseSlug - The initial slug to use as a base.
|
|
110
|
+
* @param {string[]} existingSlugs - A list of existing slugs to avoid duplicates.
|
|
111
|
+
* @returns {string} A unique slug not present in the existingSlugs list.
|
|
112
|
+
*/
|
|
113
|
+
declare const generateUniqueSlug: (baseSlug: string, existingSlugs: string[]) => string;
|
|
114
|
+
/**
|
|
115
|
+
* Removes a trailing numeric suffix from a slug, if present.
|
|
116
|
+
*
|
|
117
|
+
* This is useful for extracting the base part of a slug before checking
|
|
118
|
+
* or generating new unique variations.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* removeTrailingNumber('product-3') // returns 'product'
|
|
122
|
+
* removeTrailingNumber('item') // returns 'item'
|
|
123
|
+
*
|
|
124
|
+
* @param {string} slug - The slug from which to remove the trailing number.
|
|
125
|
+
* @returns {string} The slug without the trailing numeric suffix.
|
|
126
|
+
*/
|
|
127
|
+
declare const removeTrailingNumber: (slug: string) => string;
|
|
128
|
+
|
|
129
|
+
declare const slug_generateUniqueSlug: typeof generateUniqueSlug;
|
|
130
|
+
declare const slug_removeTrailingNumber: typeof removeTrailingNumber;
|
|
131
|
+
declare namespace slug {
|
|
132
|
+
export { slug_generateUniqueSlug as generateUniqueSlug, slug_removeTrailingNumber as removeTrailingNumber };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Converts a decimal number to an integer by multiplying it by 100.
|
|
137
|
+
* Commonly used to convert monetary values from dollars to cents.
|
|
138
|
+
*
|
|
139
|
+
* @param {number} value - The decimal number to convert (e.g., 10.90).
|
|
140
|
+
* @returns {number} The integer value in cents (e.g., 1090).
|
|
141
|
+
*/
|
|
142
|
+
declare const toCents: (value?: number) => number;
|
|
143
|
+
/**
|
|
144
|
+
* Converts an integer back to a decimal by dividing it by 100.
|
|
145
|
+
* Commonly used to convert monetary values from cents to dollars.
|
|
146
|
+
*
|
|
147
|
+
* @param {number} value - The integer value in cents (e.g., 1090).
|
|
148
|
+
* @returns {number} The decimal value (e.g., 10.90).
|
|
149
|
+
*/
|
|
150
|
+
declare const toDecimal: (value?: number) => number;
|
|
151
|
+
|
|
152
|
+
declare const number_toCents: typeof toCents;
|
|
153
|
+
declare const number_toDecimal: typeof toDecimal;
|
|
154
|
+
declare namespace number {
|
|
155
|
+
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export { createValidator, htmlEntityDecode, number as numberUtils, slug as slugUtils };
|
package/dist/index.d.ts
CHANGED
|
@@ -93,4 +93,66 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
|
|
|
93
93
|
};
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Generates a unique slug by appending a numeric suffix to the base slug if necessary.
|
|
98
|
+
*
|
|
99
|
+
* If the base slug is already used (either exactly or with a numeric suffix),
|
|
100
|
+
* the function finds the highest existing suffix and returns the next one in sequence.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* generateUniqueSlug('my-product', ['my-product', 'my-product-1', 'my-product-2'])
|
|
104
|
+
* // returns 'my-product-3'
|
|
105
|
+
*
|
|
106
|
+
* generateUniqueSlug('item', ['item-1', 'item-2'])
|
|
107
|
+
* // returns 'item'
|
|
108
|
+
*
|
|
109
|
+
* @param {string} baseSlug - The initial slug to use as a base.
|
|
110
|
+
* @param {string[]} existingSlugs - A list of existing slugs to avoid duplicates.
|
|
111
|
+
* @returns {string} A unique slug not present in the existingSlugs list.
|
|
112
|
+
*/
|
|
113
|
+
declare const generateUniqueSlug: (baseSlug: string, existingSlugs: string[]) => string;
|
|
114
|
+
/**
|
|
115
|
+
* Removes a trailing numeric suffix from a slug, if present.
|
|
116
|
+
*
|
|
117
|
+
* This is useful for extracting the base part of a slug before checking
|
|
118
|
+
* or generating new unique variations.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* removeTrailingNumber('product-3') // returns 'product'
|
|
122
|
+
* removeTrailingNumber('item') // returns 'item'
|
|
123
|
+
*
|
|
124
|
+
* @param {string} slug - The slug from which to remove the trailing number.
|
|
125
|
+
* @returns {string} The slug without the trailing numeric suffix.
|
|
126
|
+
*/
|
|
127
|
+
declare const removeTrailingNumber: (slug: string) => string;
|
|
128
|
+
|
|
129
|
+
declare const slug_generateUniqueSlug: typeof generateUniqueSlug;
|
|
130
|
+
declare const slug_removeTrailingNumber: typeof removeTrailingNumber;
|
|
131
|
+
declare namespace slug {
|
|
132
|
+
export { slug_generateUniqueSlug as generateUniqueSlug, slug_removeTrailingNumber as removeTrailingNumber };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Converts a decimal number to an integer by multiplying it by 100.
|
|
137
|
+
* Commonly used to convert monetary values from dollars to cents.
|
|
138
|
+
*
|
|
139
|
+
* @param {number} value - The decimal number to convert (e.g., 10.90).
|
|
140
|
+
* @returns {number} The integer value in cents (e.g., 1090).
|
|
141
|
+
*/
|
|
142
|
+
declare const toCents: (value?: number) => number;
|
|
143
|
+
/**
|
|
144
|
+
* Converts an integer back to a decimal by dividing it by 100.
|
|
145
|
+
* Commonly used to convert monetary values from cents to dollars.
|
|
146
|
+
*
|
|
147
|
+
* @param {number} value - The integer value in cents (e.g., 1090).
|
|
148
|
+
* @returns {number} The decimal value (e.g., 10.90).
|
|
149
|
+
*/
|
|
150
|
+
declare const toDecimal: (value?: number) => number;
|
|
151
|
+
|
|
152
|
+
declare const number_toCents: typeof toCents;
|
|
153
|
+
declare const number_toDecimal: typeof toDecimal;
|
|
154
|
+
declare namespace number {
|
|
155
|
+
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export { createValidator, htmlEntityDecode, number as numberUtils, slug as slugUtils };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
1
7
|
// src/helpers/sanitize.ts
|
|
2
8
|
import { FilterXSS } from "xss";
|
|
3
9
|
var allTags = [
|
|
@@ -440,7 +446,47 @@ function createValidator() {
|
|
|
440
446
|
}
|
|
441
447
|
};
|
|
442
448
|
}
|
|
449
|
+
|
|
450
|
+
// src/helpers/slug.ts
|
|
451
|
+
var slug_exports = {};
|
|
452
|
+
__export(slug_exports, {
|
|
453
|
+
generateUniqueSlug: () => generateUniqueSlug,
|
|
454
|
+
removeTrailingNumber: () => removeTrailingNumber
|
|
455
|
+
});
|
|
456
|
+
var generateUniqueSlug = (baseSlug, existingSlugs) => {
|
|
457
|
+
const suffixes = existingSlugs.map((slug) => {
|
|
458
|
+
const match = slug.match(new RegExp(`^${baseSlug}-(\\d+)$`));
|
|
459
|
+
return match ? Number(match[1]) : slug === baseSlug ? 0 : null;
|
|
460
|
+
}).filter((num) => num !== null);
|
|
461
|
+
const nextSuffix = suffixes.length > 0 ? Math.max(...suffixes) + 1 : 0;
|
|
462
|
+
return nextSuffix === 0 ? baseSlug : `${baseSlug}-${nextSuffix}`;
|
|
463
|
+
};
|
|
464
|
+
var removeTrailingNumber = (slug) => {
|
|
465
|
+
return slug.replace(/-\d+$/, "");
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
// src/helpers/number.ts
|
|
469
|
+
var number_exports = {};
|
|
470
|
+
__export(number_exports, {
|
|
471
|
+
toCents: () => toCents,
|
|
472
|
+
toDecimal: () => toDecimal
|
|
473
|
+
});
|
|
474
|
+
import Big from "big.js";
|
|
475
|
+
var toCents = (value) => {
|
|
476
|
+
if (!value) {
|
|
477
|
+
return 0;
|
|
478
|
+
}
|
|
479
|
+
return new Big(value).mul(100).round(0).toNumber();
|
|
480
|
+
};
|
|
481
|
+
var toDecimal = (value) => {
|
|
482
|
+
if (!value) {
|
|
483
|
+
return 0;
|
|
484
|
+
}
|
|
485
|
+
return new Big(value).div(100).round(2).toNumber();
|
|
486
|
+
};
|
|
443
487
|
export {
|
|
444
488
|
createValidator,
|
|
445
|
-
htmlEntityDecode
|
|
489
|
+
htmlEntityDecode,
|
|
490
|
+
number_exports as numberUtils,
|
|
491
|
+
slug_exports as slugUtils
|
|
446
492
|
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createValidator } from './helpers/validator';
|
|
2
2
|
import { htmlEntityDecode } from './helpers/sanitize';
|
|
3
|
+
import * as slugUtils from './helpers/slug';
|
|
4
|
+
import * as numberUtils from './helpers/number';
|
|
3
5
|
|
|
4
|
-
export { createValidator, htmlEntityDecode };
|
|
6
|
+
export { createValidator, htmlEntityDecode, slugUtils, numberUtils };
|