@excofy/utils 2.3.2 → 2.4.0
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 +32 -4
- package/dist/index.d.cts +20 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.js +32 -4
- package/package.json +1 -1
- package/src/helpers/number.ts +45 -0
- package/src/helpers/sanitize.ts +4 -0
- package/tsconfig.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -71,13 +71,13 @@ function isValidCNPJ(cnpj) {
|
|
|
71
71
|
function validCalc(x, numbers2) {
|
|
72
72
|
const slice = numbers2.slice(0, x);
|
|
73
73
|
let factor = x - 7;
|
|
74
|
-
let
|
|
74
|
+
let sum2 = 0;
|
|
75
75
|
for (let i = x; i >= 1; i--) {
|
|
76
76
|
const n = slice[x - i];
|
|
77
|
-
|
|
77
|
+
sum2 += n * factor--;
|
|
78
78
|
if (factor < 2) factor = 9;
|
|
79
79
|
}
|
|
80
|
-
const result = 11 -
|
|
80
|
+
const result = 11 - sum2 % 11;
|
|
81
81
|
return result > 9 ? 0 : result;
|
|
82
82
|
}
|
|
83
83
|
function matchNumbers(value = "") {
|
|
@@ -155,6 +155,9 @@ function sanitizeValue(value, allowedTags) {
|
|
|
155
155
|
return filter.process(value);
|
|
156
156
|
}
|
|
157
157
|
function htmlEntityDecode(str = "") {
|
|
158
|
+
if (!str || typeof str !== "string" || str.length === 0) {
|
|
159
|
+
return "";
|
|
160
|
+
}
|
|
158
161
|
return str.replace(/&#(\d+);/g, (_, code) => String.fromCharCode(code)).replace(
|
|
159
162
|
/&#x([0-9a-f]+);/gi,
|
|
160
163
|
(_, code) => String.fromCharCode(Number.parseInt(code, 16))
|
|
@@ -908,8 +911,11 @@ var removeTrailingNumber = (slug) => {
|
|
|
908
911
|
// src/helpers/number.ts
|
|
909
912
|
var number_exports = {};
|
|
910
913
|
__export(number_exports, {
|
|
914
|
+
divide: () => divide,
|
|
915
|
+
sum: () => sum,
|
|
911
916
|
toCents: () => toCents,
|
|
912
|
-
toDecimal: () => toDecimal
|
|
917
|
+
toDecimal: () => toDecimal,
|
|
918
|
+
truncateDecimal: () => truncateDecimal
|
|
913
919
|
});
|
|
914
920
|
var import_big = __toESM(require("big.js"), 1);
|
|
915
921
|
var toCents = (value) => {
|
|
@@ -924,6 +930,28 @@ var toDecimal = (value) => {
|
|
|
924
930
|
}
|
|
925
931
|
return new import_big.default(value).div(100).round(2).toNumber();
|
|
926
932
|
};
|
|
933
|
+
var truncateDecimal = (value, decimalPlaces) => {
|
|
934
|
+
if (!value) {
|
|
935
|
+
return 0;
|
|
936
|
+
}
|
|
937
|
+
const bigValue = new import_big.default(value);
|
|
938
|
+
const factor = new import_big.default(10).pow(decimalPlaces);
|
|
939
|
+
return bigValue.times(factor).round(0, import_big.default.roundDown).div(factor).toNumber();
|
|
940
|
+
};
|
|
941
|
+
var sum = (values) => {
|
|
942
|
+
return values.reduce((acc, value) => {
|
|
943
|
+
if (value) {
|
|
944
|
+
return acc.plus(value);
|
|
945
|
+
}
|
|
946
|
+
return acc;
|
|
947
|
+
}, new import_big.default(0)).toNumber();
|
|
948
|
+
};
|
|
949
|
+
var divide = (numerator, denominator) => {
|
|
950
|
+
if (denominator === 0) {
|
|
951
|
+
return 0;
|
|
952
|
+
}
|
|
953
|
+
return new import_big.default(numerator).div(denominator).toNumber();
|
|
954
|
+
};
|
|
927
955
|
// Annotate the CommonJS export names for ESM import in node:
|
|
928
956
|
0 && (module.exports = {
|
|
929
957
|
ExpiredTokenError,
|
package/dist/index.d.cts
CHANGED
|
@@ -195,11 +195,30 @@ declare const toCents: (value?: number) => number;
|
|
|
195
195
|
* @returns {number} The decimal value (e.g., 10.90).
|
|
196
196
|
*/
|
|
197
197
|
declare const toDecimal: (value?: number) => number;
|
|
198
|
+
/**
|
|
199
|
+
* Truncates a decimal number to a specified number of decimal places.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const truncated = truncateDecimal(10.98765, 2);
|
|
204
|
+
* console.log(truncated); // 10.98
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* @param {number} value - The decimal number to truncate.
|
|
208
|
+
* @param {number} decimalPlaces - The number of decimal places to keep.
|
|
209
|
+
* @returns {number} The truncated decimal number.
|
|
210
|
+
*/
|
|
211
|
+
declare const truncateDecimal: (value: number, decimalPlaces: number) => number;
|
|
212
|
+
declare const sum: (values: (number | undefined)[]) => number;
|
|
213
|
+
declare const divide: (numerator: number, denominator: number) => number;
|
|
198
214
|
|
|
215
|
+
declare const number_divide: typeof divide;
|
|
216
|
+
declare const number_sum: typeof sum;
|
|
199
217
|
declare const number_toCents: typeof toCents;
|
|
200
218
|
declare const number_toDecimal: typeof toDecimal;
|
|
219
|
+
declare const number_truncateDecimal: typeof truncateDecimal;
|
|
201
220
|
declare namespace number {
|
|
202
|
-
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
221
|
+
export { number_divide as divide, number_sum as sum, number_toCents as toCents, number_toDecimal as toDecimal, number_truncateDecimal as truncateDecimal };
|
|
203
222
|
}
|
|
204
223
|
|
|
205
224
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -195,11 +195,30 @@ declare const toCents: (value?: number) => number;
|
|
|
195
195
|
* @returns {number} The decimal value (e.g., 10.90).
|
|
196
196
|
*/
|
|
197
197
|
declare const toDecimal: (value?: number) => number;
|
|
198
|
+
/**
|
|
199
|
+
* Truncates a decimal number to a specified number of decimal places.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const truncated = truncateDecimal(10.98765, 2);
|
|
204
|
+
* console.log(truncated); // 10.98
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* @param {number} value - The decimal number to truncate.
|
|
208
|
+
* @param {number} decimalPlaces - The number of decimal places to keep.
|
|
209
|
+
* @returns {number} The truncated decimal number.
|
|
210
|
+
*/
|
|
211
|
+
declare const truncateDecimal: (value: number, decimalPlaces: number) => number;
|
|
212
|
+
declare const sum: (values: (number | undefined)[]) => number;
|
|
213
|
+
declare const divide: (numerator: number, denominator: number) => number;
|
|
198
214
|
|
|
215
|
+
declare const number_divide: typeof divide;
|
|
216
|
+
declare const number_sum: typeof sum;
|
|
199
217
|
declare const number_toCents: typeof toCents;
|
|
200
218
|
declare const number_toDecimal: typeof toDecimal;
|
|
219
|
+
declare const number_truncateDecimal: typeof truncateDecimal;
|
|
201
220
|
declare namespace number {
|
|
202
|
-
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
221
|
+
export { number_divide as divide, number_sum as sum, number_toCents as toCents, number_toDecimal as toDecimal, number_truncateDecimal as truncateDecimal };
|
|
203
222
|
}
|
|
204
223
|
|
|
205
224
|
/**
|
package/dist/index.js
CHANGED
|
@@ -33,13 +33,13 @@ function isValidCNPJ(cnpj) {
|
|
|
33
33
|
function validCalc(x, numbers2) {
|
|
34
34
|
const slice = numbers2.slice(0, x);
|
|
35
35
|
let factor = x - 7;
|
|
36
|
-
let
|
|
36
|
+
let sum2 = 0;
|
|
37
37
|
for (let i = x; i >= 1; i--) {
|
|
38
38
|
const n = slice[x - i];
|
|
39
|
-
|
|
39
|
+
sum2 += n * factor--;
|
|
40
40
|
if (factor < 2) factor = 9;
|
|
41
41
|
}
|
|
42
|
-
const result = 11 -
|
|
42
|
+
const result = 11 - sum2 % 11;
|
|
43
43
|
return result > 9 ? 0 : result;
|
|
44
44
|
}
|
|
45
45
|
function matchNumbers(value = "") {
|
|
@@ -117,6 +117,9 @@ function sanitizeValue(value, allowedTags) {
|
|
|
117
117
|
return filter.process(value);
|
|
118
118
|
}
|
|
119
119
|
function htmlEntityDecode(str = "") {
|
|
120
|
+
if (!str || typeof str !== "string" || str.length === 0) {
|
|
121
|
+
return "";
|
|
122
|
+
}
|
|
120
123
|
return str.replace(/&#(\d+);/g, (_, code) => String.fromCharCode(code)).replace(
|
|
121
124
|
/&#x([0-9a-f]+);/gi,
|
|
122
125
|
(_, code) => String.fromCharCode(Number.parseInt(code, 16))
|
|
@@ -870,8 +873,11 @@ var removeTrailingNumber = (slug) => {
|
|
|
870
873
|
// src/helpers/number.ts
|
|
871
874
|
var number_exports = {};
|
|
872
875
|
__export(number_exports, {
|
|
876
|
+
divide: () => divide,
|
|
877
|
+
sum: () => sum,
|
|
873
878
|
toCents: () => toCents,
|
|
874
|
-
toDecimal: () => toDecimal
|
|
879
|
+
toDecimal: () => toDecimal,
|
|
880
|
+
truncateDecimal: () => truncateDecimal
|
|
875
881
|
});
|
|
876
882
|
import Big from "big.js";
|
|
877
883
|
var toCents = (value) => {
|
|
@@ -886,6 +892,28 @@ var toDecimal = (value) => {
|
|
|
886
892
|
}
|
|
887
893
|
return new Big(value).div(100).round(2).toNumber();
|
|
888
894
|
};
|
|
895
|
+
var truncateDecimal = (value, decimalPlaces) => {
|
|
896
|
+
if (!value) {
|
|
897
|
+
return 0;
|
|
898
|
+
}
|
|
899
|
+
const bigValue = new Big(value);
|
|
900
|
+
const factor = new Big(10).pow(decimalPlaces);
|
|
901
|
+
return bigValue.times(factor).round(0, Big.roundDown).div(factor).toNumber();
|
|
902
|
+
};
|
|
903
|
+
var sum = (values) => {
|
|
904
|
+
return values.reduce((acc, value) => {
|
|
905
|
+
if (value) {
|
|
906
|
+
return acc.plus(value);
|
|
907
|
+
}
|
|
908
|
+
return acc;
|
|
909
|
+
}, new Big(0)).toNumber();
|
|
910
|
+
};
|
|
911
|
+
var divide = (numerator, denominator) => {
|
|
912
|
+
if (denominator === 0) {
|
|
913
|
+
return 0;
|
|
914
|
+
}
|
|
915
|
+
return new Big(numerator).div(denominator).toNumber();
|
|
916
|
+
};
|
|
889
917
|
export {
|
|
890
918
|
ExpiredTokenError,
|
|
891
919
|
InvalidTokenError,
|
package/package.json
CHANGED
package/src/helpers/number.ts
CHANGED
|
@@ -29,3 +29,48 @@ export const toDecimal = (value?: number): number => {
|
|
|
29
29
|
|
|
30
30
|
return new Big(value).div(100).round(2).toNumber();
|
|
31
31
|
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Truncates a decimal number to a specified number of decimal places.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const truncated = truncateDecimal(10.98765, 2);
|
|
39
|
+
* console.log(truncated); // 10.98
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param {number} value - The decimal number to truncate.
|
|
43
|
+
* @param {number} decimalPlaces - The number of decimal places to keep.
|
|
44
|
+
* @returns {number} The truncated decimal number.
|
|
45
|
+
*/
|
|
46
|
+
export const truncateDecimal = (
|
|
47
|
+
value: number,
|
|
48
|
+
decimalPlaces: number,
|
|
49
|
+
): number => {
|
|
50
|
+
if (!value) {
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const bigValue = new Big(value);
|
|
55
|
+
const factor = new Big(10).pow(decimalPlaces);
|
|
56
|
+
return bigValue.times(factor).round(0, Big.roundDown).div(factor).toNumber();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const sum = (values: (number | undefined)[]): number => {
|
|
60
|
+
return values
|
|
61
|
+
.reduce((acc: Big.Big, value) => {
|
|
62
|
+
if (value) {
|
|
63
|
+
return acc.plus(value);
|
|
64
|
+
}
|
|
65
|
+
return acc;
|
|
66
|
+
}, new Big(0))
|
|
67
|
+
.toNumber();
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const divide = (numerator: number, denominator: number) => {
|
|
71
|
+
if (denominator === 0) {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return new Big(numerator).div(denominator).toNumber();
|
|
76
|
+
};
|
package/src/helpers/sanitize.ts
CHANGED
|
@@ -102,6 +102,10 @@ export function sanitizeValue(
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
export function htmlEntityDecode(str = ''): string {
|
|
105
|
+
if (!str || typeof str !== 'string' || str.length === 0) {
|
|
106
|
+
return '';
|
|
107
|
+
}
|
|
108
|
+
|
|
105
109
|
return str
|
|
106
110
|
.replace(/&#(\d+);/g, (_, code) => String.fromCharCode(code))
|
|
107
111
|
.replace(/&#x([0-9a-f]+);/gi, (_, code) =>
|