@excofy/utils 1.0.2 → 1.0.4

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 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,24 @@ 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,
37
+ stringUtils: () => stringUtils
25
38
  });
26
39
  module.exports = __toCommonJS(index_exports);
27
40
 
@@ -467,8 +480,54 @@ function createValidator() {
467
480
  }
468
481
  };
469
482
  }
483
+
484
+ // src/helpers/string.ts
485
+ var stringUtils = {
486
+ removeFileExtension: (fileName) => fileName.replace(/\.[^/.]+$/, "")
487
+ };
488
+
489
+ // src/helpers/slug.ts
490
+ var slug_exports = {};
491
+ __export(slug_exports, {
492
+ generateUniqueSlug: () => generateUniqueSlug,
493
+ removeTrailingNumber: () => removeTrailingNumber
494
+ });
495
+ var generateUniqueSlug = (baseSlug, existingSlugs) => {
496
+ const suffixes = existingSlugs.map((slug) => {
497
+ const match = slug.match(new RegExp(`^${baseSlug}-(\\d+)$`));
498
+ return match ? Number(match[1]) : slug === baseSlug ? 0 : null;
499
+ }).filter((num) => num !== null);
500
+ const nextSuffix = suffixes.length > 0 ? Math.max(...suffixes) + 1 : 0;
501
+ return nextSuffix === 0 ? baseSlug : `${baseSlug}-${nextSuffix}`;
502
+ };
503
+ var removeTrailingNumber = (slug) => {
504
+ return slug.replace(/-\d+$/, "");
505
+ };
506
+
507
+ // src/helpers/number.ts
508
+ var number_exports = {};
509
+ __export(number_exports, {
510
+ toCents: () => toCents,
511
+ toDecimal: () => toDecimal
512
+ });
513
+ var import_big = __toESM(require("big.js"), 1);
514
+ var toCents = (value) => {
515
+ if (!value) {
516
+ return 0;
517
+ }
518
+ return new import_big.default(value).mul(100).round(0).toNumber();
519
+ };
520
+ var toDecimal = (value) => {
521
+ if (!value) {
522
+ return 0;
523
+ }
524
+ return new import_big.default(value).div(100).round(2).toNumber();
525
+ };
470
526
  // Annotate the CommonJS export names for ESM import in node:
471
527
  0 && (module.exports = {
472
528
  createValidator,
473
- htmlEntityDecode
529
+ htmlEntityDecode,
530
+ numberUtils,
531
+ slugUtils,
532
+ stringUtils
474
533
  });
package/dist/index.d.cts CHANGED
@@ -93,4 +93,70 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
93
93
  };
94
94
  };
95
95
 
96
- export { createValidator, htmlEntityDecode };
96
+ declare const stringUtils: {
97
+ removeFileExtension: (fileName: string) => string;
98
+ };
99
+
100
+ /**
101
+ * Generates a unique slug by appending a numeric suffix to the base slug if necessary.
102
+ *
103
+ * If the base slug is already used (either exactly or with a numeric suffix),
104
+ * the function finds the highest existing suffix and returns the next one in sequence.
105
+ *
106
+ * @example
107
+ * generateUniqueSlug('my-product', ['my-product', 'my-product-1', 'my-product-2'])
108
+ * // returns 'my-product-3'
109
+ *
110
+ * generateUniqueSlug('item', ['item-1', 'item-2'])
111
+ * // returns 'item'
112
+ *
113
+ * @param {string} baseSlug - The initial slug to use as a base.
114
+ * @param {string[]} existingSlugs - A list of existing slugs to avoid duplicates.
115
+ * @returns {string} A unique slug not present in the existingSlugs list.
116
+ */
117
+ declare const generateUniqueSlug: (baseSlug: string, existingSlugs: string[]) => string;
118
+ /**
119
+ * Removes a trailing numeric suffix from a slug, if present.
120
+ *
121
+ * This is useful for extracting the base part of a slug before checking
122
+ * or generating new unique variations.
123
+ *
124
+ * @example
125
+ * removeTrailingNumber('product-3') // returns 'product'
126
+ * removeTrailingNumber('item') // returns 'item'
127
+ *
128
+ * @param {string} slug - The slug from which to remove the trailing number.
129
+ * @returns {string} The slug without the trailing numeric suffix.
130
+ */
131
+ declare const removeTrailingNumber: (slug: string) => string;
132
+
133
+ declare const slug_generateUniqueSlug: typeof generateUniqueSlug;
134
+ declare const slug_removeTrailingNumber: typeof removeTrailingNumber;
135
+ declare namespace slug {
136
+ export { slug_generateUniqueSlug as generateUniqueSlug, slug_removeTrailingNumber as removeTrailingNumber };
137
+ }
138
+
139
+ /**
140
+ * Converts a decimal number to an integer by multiplying it by 100.
141
+ * Commonly used to convert monetary values from dollars to cents.
142
+ *
143
+ * @param {number} value - The decimal number to convert (e.g., 10.90).
144
+ * @returns {number} The integer value in cents (e.g., 1090).
145
+ */
146
+ declare const toCents: (value?: number) => number;
147
+ /**
148
+ * Converts an integer back to a decimal by dividing it by 100.
149
+ * Commonly used to convert monetary values from cents to dollars.
150
+ *
151
+ * @param {number} value - The integer value in cents (e.g., 1090).
152
+ * @returns {number} The decimal value (e.g., 10.90).
153
+ */
154
+ declare const toDecimal: (value?: number) => number;
155
+
156
+ declare const number_toCents: typeof toCents;
157
+ declare const number_toDecimal: typeof toDecimal;
158
+ declare namespace number {
159
+ export { number_toCents as toCents, number_toDecimal as toDecimal };
160
+ }
161
+
162
+ export { createValidator, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
package/dist/index.d.ts CHANGED
@@ -93,4 +93,70 @@ declare function createValidator<TRaw extends Record<string, TInputValue>, TPars
93
93
  };
94
94
  };
95
95
 
96
- export { createValidator, htmlEntityDecode };
96
+ declare const stringUtils: {
97
+ removeFileExtension: (fileName: string) => string;
98
+ };
99
+
100
+ /**
101
+ * Generates a unique slug by appending a numeric suffix to the base slug if necessary.
102
+ *
103
+ * If the base slug is already used (either exactly or with a numeric suffix),
104
+ * the function finds the highest existing suffix and returns the next one in sequence.
105
+ *
106
+ * @example
107
+ * generateUniqueSlug('my-product', ['my-product', 'my-product-1', 'my-product-2'])
108
+ * // returns 'my-product-3'
109
+ *
110
+ * generateUniqueSlug('item', ['item-1', 'item-2'])
111
+ * // returns 'item'
112
+ *
113
+ * @param {string} baseSlug - The initial slug to use as a base.
114
+ * @param {string[]} existingSlugs - A list of existing slugs to avoid duplicates.
115
+ * @returns {string} A unique slug not present in the existingSlugs list.
116
+ */
117
+ declare const generateUniqueSlug: (baseSlug: string, existingSlugs: string[]) => string;
118
+ /**
119
+ * Removes a trailing numeric suffix from a slug, if present.
120
+ *
121
+ * This is useful for extracting the base part of a slug before checking
122
+ * or generating new unique variations.
123
+ *
124
+ * @example
125
+ * removeTrailingNumber('product-3') // returns 'product'
126
+ * removeTrailingNumber('item') // returns 'item'
127
+ *
128
+ * @param {string} slug - The slug from which to remove the trailing number.
129
+ * @returns {string} The slug without the trailing numeric suffix.
130
+ */
131
+ declare const removeTrailingNumber: (slug: string) => string;
132
+
133
+ declare const slug_generateUniqueSlug: typeof generateUniqueSlug;
134
+ declare const slug_removeTrailingNumber: typeof removeTrailingNumber;
135
+ declare namespace slug {
136
+ export { slug_generateUniqueSlug as generateUniqueSlug, slug_removeTrailingNumber as removeTrailingNumber };
137
+ }
138
+
139
+ /**
140
+ * Converts a decimal number to an integer by multiplying it by 100.
141
+ * Commonly used to convert monetary values from dollars to cents.
142
+ *
143
+ * @param {number} value - The decimal number to convert (e.g., 10.90).
144
+ * @returns {number} The integer value in cents (e.g., 1090).
145
+ */
146
+ declare const toCents: (value?: number) => number;
147
+ /**
148
+ * Converts an integer back to a decimal by dividing it by 100.
149
+ * Commonly used to convert monetary values from cents to dollars.
150
+ *
151
+ * @param {number} value - The integer value in cents (e.g., 1090).
152
+ * @returns {number} The decimal value (e.g., 10.90).
153
+ */
154
+ declare const toDecimal: (value?: number) => number;
155
+
156
+ declare const number_toCents: typeof toCents;
157
+ declare const number_toDecimal: typeof toDecimal;
158
+ declare namespace number {
159
+ export { number_toCents as toCents, number_toDecimal as toDecimal };
160
+ }
161
+
162
+ export { createValidator, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
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,53 @@ function createValidator() {
440
446
  }
441
447
  };
442
448
  }
449
+
450
+ // src/helpers/string.ts
451
+ var stringUtils = {
452
+ removeFileExtension: (fileName) => fileName.replace(/\.[^/.]+$/, "")
453
+ };
454
+
455
+ // src/helpers/slug.ts
456
+ var slug_exports = {};
457
+ __export(slug_exports, {
458
+ generateUniqueSlug: () => generateUniqueSlug,
459
+ removeTrailingNumber: () => removeTrailingNumber
460
+ });
461
+ var generateUniqueSlug = (baseSlug, existingSlugs) => {
462
+ const suffixes = existingSlugs.map((slug) => {
463
+ const match = slug.match(new RegExp(`^${baseSlug}-(\\d+)$`));
464
+ return match ? Number(match[1]) : slug === baseSlug ? 0 : null;
465
+ }).filter((num) => num !== null);
466
+ const nextSuffix = suffixes.length > 0 ? Math.max(...suffixes) + 1 : 0;
467
+ return nextSuffix === 0 ? baseSlug : `${baseSlug}-${nextSuffix}`;
468
+ };
469
+ var removeTrailingNumber = (slug) => {
470
+ return slug.replace(/-\d+$/, "");
471
+ };
472
+
473
+ // src/helpers/number.ts
474
+ var number_exports = {};
475
+ __export(number_exports, {
476
+ toCents: () => toCents,
477
+ toDecimal: () => toDecimal
478
+ });
479
+ import Big from "big.js";
480
+ var toCents = (value) => {
481
+ if (!value) {
482
+ return 0;
483
+ }
484
+ return new Big(value).mul(100).round(0).toNumber();
485
+ };
486
+ var toDecimal = (value) => {
487
+ if (!value) {
488
+ return 0;
489
+ }
490
+ return new Big(value).div(100).round(2).toNumber();
491
+ };
443
492
  export {
444
493
  createValidator,
445
- htmlEntityDecode
494
+ htmlEntityDecode,
495
+ number_exports as numberUtils,
496
+ slug_exports as slugUtils,
497
+ stringUtils
446
498
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -0,0 +1,4 @@
1
+ export const stringUtils = {
2
+ removeFileExtension: (fileName: string): string =>
3
+ fileName.replace(/\.[^/.]+$/, ''),
4
+ };
package/src/index.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  import { createValidator } from './helpers/validator';
2
2
  import { htmlEntityDecode } from './helpers/sanitize';
3
+ import { stringUtils } from './helpers/string';
4
+ import * as slugUtils from './helpers/slug';
5
+ import * as numberUtils from './helpers/number';
3
6
 
4
- export { createValidator, htmlEntityDecode };
7
+ export {
8
+ createValidator,
9
+ htmlEntityDecode,
10
+ slugUtils,
11
+ numberUtils,
12
+ stringUtils,
13
+ };