@excofy/utils 2.3.3 → 2.5.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 +42 -4
- package/dist/index.d.cts +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +42 -4
- package/package.json +1 -1
- package/src/helpers/crypto.ts +30 -12
- package/src/helpers/number.ts +45 -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 = "") {
|
|
@@ -809,6 +809,19 @@ async function verifyDeterministicHash(inputCode, storedHash, secret) {
|
|
|
809
809
|
}
|
|
810
810
|
var cryptoUtils = {
|
|
811
811
|
uuidV4: () => crypto.randomUUID(),
|
|
812
|
+
ulid: () => {
|
|
813
|
+
function randomChar() {
|
|
814
|
+
const chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
815
|
+
return chars[Math.floor(Math.random() * chars.length)];
|
|
816
|
+
}
|
|
817
|
+
const timestamp = Date.now();
|
|
818
|
+
const timestampStr = timestamp.toString(36).toUpperCase().padStart(10, "0");
|
|
819
|
+
let randomPart = "";
|
|
820
|
+
for (let i = 0; i < 16; i++) {
|
|
821
|
+
randomPart += randomChar();
|
|
822
|
+
}
|
|
823
|
+
return timestampStr + randomPart;
|
|
824
|
+
},
|
|
812
825
|
hash: async (password, salt = 10) => {
|
|
813
826
|
const importedKey = await crypto.subtle.importKey(
|
|
814
827
|
"raw",
|
|
@@ -911,8 +924,11 @@ var removeTrailingNumber = (slug) => {
|
|
|
911
924
|
// src/helpers/number.ts
|
|
912
925
|
var number_exports = {};
|
|
913
926
|
__export(number_exports, {
|
|
927
|
+
divide: () => divide,
|
|
928
|
+
sum: () => sum,
|
|
914
929
|
toCents: () => toCents,
|
|
915
|
-
toDecimal: () => toDecimal
|
|
930
|
+
toDecimal: () => toDecimal,
|
|
931
|
+
truncateDecimal: () => truncateDecimal
|
|
916
932
|
});
|
|
917
933
|
var import_big = __toESM(require("big.js"), 1);
|
|
918
934
|
var toCents = (value) => {
|
|
@@ -927,6 +943,28 @@ var toDecimal = (value) => {
|
|
|
927
943
|
}
|
|
928
944
|
return new import_big.default(value).div(100).round(2).toNumber();
|
|
929
945
|
};
|
|
946
|
+
var truncateDecimal = (value, decimalPlaces) => {
|
|
947
|
+
if (!value) {
|
|
948
|
+
return 0;
|
|
949
|
+
}
|
|
950
|
+
const bigValue = new import_big.default(value);
|
|
951
|
+
const factor = new import_big.default(10).pow(decimalPlaces);
|
|
952
|
+
return bigValue.times(factor).round(0, import_big.default.roundDown).div(factor).toNumber();
|
|
953
|
+
};
|
|
954
|
+
var sum = (values) => {
|
|
955
|
+
return values.reduce((acc, value) => {
|
|
956
|
+
if (value) {
|
|
957
|
+
return acc.plus(value);
|
|
958
|
+
}
|
|
959
|
+
return acc;
|
|
960
|
+
}, new import_big.default(0)).toNumber();
|
|
961
|
+
};
|
|
962
|
+
var divide = (numerator, denominator) => {
|
|
963
|
+
if (denominator === 0) {
|
|
964
|
+
return 0;
|
|
965
|
+
}
|
|
966
|
+
return new import_big.default(numerator).div(denominator).toNumber();
|
|
967
|
+
};
|
|
930
968
|
// Annotate the CommonJS export names for ESM import in node:
|
|
931
969
|
0 && (module.exports = {
|
|
932
970
|
ExpiredTokenError,
|
package/dist/index.d.cts
CHANGED
|
@@ -122,6 +122,7 @@ interface IValidateAccessToken {
|
|
|
122
122
|
}
|
|
123
123
|
interface ICrypto {
|
|
124
124
|
uuidV4: () => string;
|
|
125
|
+
ulid: () => string;
|
|
125
126
|
hash: (password: string, salt: number) => Promise<string>;
|
|
126
127
|
isMatch: (password: string, hash: string, salt?: number) => Promise<boolean>;
|
|
127
128
|
generateAccessToken: (data: IGenerateAccessToken) => Promise<string>;
|
|
@@ -195,11 +196,30 @@ declare const toCents: (value?: number) => number;
|
|
|
195
196
|
* @returns {number} The decimal value (e.g., 10.90).
|
|
196
197
|
*/
|
|
197
198
|
declare const toDecimal: (value?: number) => number;
|
|
199
|
+
/**
|
|
200
|
+
* Truncates a decimal number to a specified number of decimal places.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const truncated = truncateDecimal(10.98765, 2);
|
|
205
|
+
* console.log(truncated); // 10.98
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @param {number} value - The decimal number to truncate.
|
|
209
|
+
* @param {number} decimalPlaces - The number of decimal places to keep.
|
|
210
|
+
* @returns {number} The truncated decimal number.
|
|
211
|
+
*/
|
|
212
|
+
declare const truncateDecimal: (value: number, decimalPlaces: number) => number;
|
|
213
|
+
declare const sum: (values: (number | undefined)[]) => number;
|
|
214
|
+
declare const divide: (numerator: number, denominator: number) => number;
|
|
198
215
|
|
|
216
|
+
declare const number_divide: typeof divide;
|
|
217
|
+
declare const number_sum: typeof sum;
|
|
199
218
|
declare const number_toCents: typeof toCents;
|
|
200
219
|
declare const number_toDecimal: typeof toDecimal;
|
|
220
|
+
declare const number_truncateDecimal: typeof truncateDecimal;
|
|
201
221
|
declare namespace number {
|
|
202
|
-
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
222
|
+
export { number_divide as divide, number_sum as sum, number_toCents as toCents, number_toDecimal as toDecimal, number_truncateDecimal as truncateDecimal };
|
|
203
223
|
}
|
|
204
224
|
|
|
205
225
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -122,6 +122,7 @@ interface IValidateAccessToken {
|
|
|
122
122
|
}
|
|
123
123
|
interface ICrypto {
|
|
124
124
|
uuidV4: () => string;
|
|
125
|
+
ulid: () => string;
|
|
125
126
|
hash: (password: string, salt: number) => Promise<string>;
|
|
126
127
|
isMatch: (password: string, hash: string, salt?: number) => Promise<boolean>;
|
|
127
128
|
generateAccessToken: (data: IGenerateAccessToken) => Promise<string>;
|
|
@@ -195,11 +196,30 @@ declare const toCents: (value?: number) => number;
|
|
|
195
196
|
* @returns {number} The decimal value (e.g., 10.90).
|
|
196
197
|
*/
|
|
197
198
|
declare const toDecimal: (value?: number) => number;
|
|
199
|
+
/**
|
|
200
|
+
* Truncates a decimal number to a specified number of decimal places.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const truncated = truncateDecimal(10.98765, 2);
|
|
205
|
+
* console.log(truncated); // 10.98
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @param {number} value - The decimal number to truncate.
|
|
209
|
+
* @param {number} decimalPlaces - The number of decimal places to keep.
|
|
210
|
+
* @returns {number} The truncated decimal number.
|
|
211
|
+
*/
|
|
212
|
+
declare const truncateDecimal: (value: number, decimalPlaces: number) => number;
|
|
213
|
+
declare const sum: (values: (number | undefined)[]) => number;
|
|
214
|
+
declare const divide: (numerator: number, denominator: number) => number;
|
|
198
215
|
|
|
216
|
+
declare const number_divide: typeof divide;
|
|
217
|
+
declare const number_sum: typeof sum;
|
|
199
218
|
declare const number_toCents: typeof toCents;
|
|
200
219
|
declare const number_toDecimal: typeof toDecimal;
|
|
220
|
+
declare const number_truncateDecimal: typeof truncateDecimal;
|
|
201
221
|
declare namespace number {
|
|
202
|
-
export { number_toCents as toCents, number_toDecimal as toDecimal };
|
|
222
|
+
export { number_divide as divide, number_sum as sum, number_toCents as toCents, number_toDecimal as toDecimal, number_truncateDecimal as truncateDecimal };
|
|
203
223
|
}
|
|
204
224
|
|
|
205
225
|
/**
|
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 = "") {
|
|
@@ -771,6 +771,19 @@ async function verifyDeterministicHash(inputCode, storedHash, secret) {
|
|
|
771
771
|
}
|
|
772
772
|
var cryptoUtils = {
|
|
773
773
|
uuidV4: () => crypto.randomUUID(),
|
|
774
|
+
ulid: () => {
|
|
775
|
+
function randomChar() {
|
|
776
|
+
const chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
777
|
+
return chars[Math.floor(Math.random() * chars.length)];
|
|
778
|
+
}
|
|
779
|
+
const timestamp = Date.now();
|
|
780
|
+
const timestampStr = timestamp.toString(36).toUpperCase().padStart(10, "0");
|
|
781
|
+
let randomPart = "";
|
|
782
|
+
for (let i = 0; i < 16; i++) {
|
|
783
|
+
randomPart += randomChar();
|
|
784
|
+
}
|
|
785
|
+
return timestampStr + randomPart;
|
|
786
|
+
},
|
|
774
787
|
hash: async (password, salt = 10) => {
|
|
775
788
|
const importedKey = await crypto.subtle.importKey(
|
|
776
789
|
"raw",
|
|
@@ -873,8 +886,11 @@ var removeTrailingNumber = (slug) => {
|
|
|
873
886
|
// src/helpers/number.ts
|
|
874
887
|
var number_exports = {};
|
|
875
888
|
__export(number_exports, {
|
|
889
|
+
divide: () => divide,
|
|
890
|
+
sum: () => sum,
|
|
876
891
|
toCents: () => toCents,
|
|
877
|
-
toDecimal: () => toDecimal
|
|
892
|
+
toDecimal: () => toDecimal,
|
|
893
|
+
truncateDecimal: () => truncateDecimal
|
|
878
894
|
});
|
|
879
895
|
import Big from "big.js";
|
|
880
896
|
var toCents = (value) => {
|
|
@@ -889,6 +905,28 @@ var toDecimal = (value) => {
|
|
|
889
905
|
}
|
|
890
906
|
return new Big(value).div(100).round(2).toNumber();
|
|
891
907
|
};
|
|
908
|
+
var truncateDecimal = (value, decimalPlaces) => {
|
|
909
|
+
if (!value) {
|
|
910
|
+
return 0;
|
|
911
|
+
}
|
|
912
|
+
const bigValue = new Big(value);
|
|
913
|
+
const factor = new Big(10).pow(decimalPlaces);
|
|
914
|
+
return bigValue.times(factor).round(0, Big.roundDown).div(factor).toNumber();
|
|
915
|
+
};
|
|
916
|
+
var sum = (values) => {
|
|
917
|
+
return values.reduce((acc, value) => {
|
|
918
|
+
if (value) {
|
|
919
|
+
return acc.plus(value);
|
|
920
|
+
}
|
|
921
|
+
return acc;
|
|
922
|
+
}, new Big(0)).toNumber();
|
|
923
|
+
};
|
|
924
|
+
var divide = (numerator, denominator) => {
|
|
925
|
+
if (denominator === 0) {
|
|
926
|
+
return 0;
|
|
927
|
+
}
|
|
928
|
+
return new Big(numerator).div(denominator).toNumber();
|
|
929
|
+
};
|
|
892
930
|
export {
|
|
893
931
|
ExpiredTokenError,
|
|
894
932
|
InvalidTokenError,
|
package/package.json
CHANGED
package/src/helpers/crypto.ts
CHANGED
|
@@ -17,6 +17,7 @@ interface IValidateAccessToken {
|
|
|
17
17
|
|
|
18
18
|
interface ICrypto {
|
|
19
19
|
uuidV4: () => string;
|
|
20
|
+
ulid: () => string;
|
|
20
21
|
hash: (password: string, salt: number) => Promise<string>;
|
|
21
22
|
isMatch: (password: string, hash: string, salt?: number) => Promise<boolean>;
|
|
22
23
|
generateAccessToken: (data: IGenerateAccessToken) => Promise<string>;
|
|
@@ -34,7 +35,7 @@ interface ICrypto {
|
|
|
34
35
|
verifyDeterministicHash: (
|
|
35
36
|
inputCode: string,
|
|
36
37
|
storedHash: string,
|
|
37
|
-
secret: string
|
|
38
|
+
secret: string,
|
|
38
39
|
) => Promise<boolean>;
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -54,7 +55,7 @@ const signatureKey = async (AUTH_SIGN_SECRET: string) =>
|
|
|
54
55
|
encoder.encode(AUTH_SIGN_SECRET),
|
|
55
56
|
{ name: 'HMAC', hash: 'SHA-256' },
|
|
56
57
|
false,
|
|
57
|
-
['sign', 'verify']
|
|
58
|
+
['sign', 'verify'],
|
|
58
59
|
);
|
|
59
60
|
|
|
60
61
|
const payloadKey = async (AUTH_PAYLOAD_SECRET: string) =>
|
|
@@ -63,7 +64,7 @@ const payloadKey = async (AUTH_PAYLOAD_SECRET: string) =>
|
|
|
63
64
|
encoder.encode(AUTH_PAYLOAD_SECRET.substring(0, 32)),
|
|
64
65
|
{ name: 'AES-GCM', length: 256 },
|
|
65
66
|
true,
|
|
66
|
-
['encrypt', 'decrypt']
|
|
67
|
+
['encrypt', 'decrypt'],
|
|
67
68
|
);
|
|
68
69
|
|
|
69
70
|
/* --------------------- Payload (encrypt/decrypt) --------------------- */
|
|
@@ -80,7 +81,7 @@ const encryptPayload = async ({
|
|
|
80
81
|
const buffer = await crypto.subtle.encrypt(
|
|
81
82
|
{ name: 'AES-GCM', iv },
|
|
82
83
|
key,
|
|
83
|
-
encoder.encode(payload)
|
|
84
|
+
encoder.encode(payload),
|
|
84
85
|
);
|
|
85
86
|
|
|
86
87
|
return `${toBase64(iv)}.${toBase64(buffer)}`;
|
|
@@ -99,7 +100,7 @@ const decryptPayload = async ({
|
|
|
99
100
|
const buffer = await crypto.subtle.decrypt(
|
|
100
101
|
{ name: 'AES-GCM', iv: fromBase64(header).buffer as ArrayBuffer },
|
|
101
102
|
key,
|
|
102
|
-
fromBase64(payload).buffer as ArrayBuffer
|
|
103
|
+
fromBase64(payload).buffer as ArrayBuffer,
|
|
103
104
|
);
|
|
104
105
|
|
|
105
106
|
return decoder.decode(buffer);
|
|
@@ -115,14 +116,14 @@ const decryptPayload = async ({
|
|
|
115
116
|
*/
|
|
116
117
|
export async function generateDeterministicHash(
|
|
117
118
|
code: string,
|
|
118
|
-
secret: string
|
|
119
|
+
secret: string,
|
|
119
120
|
): Promise<string> {
|
|
120
121
|
const key = await crypto.subtle.importKey(
|
|
121
122
|
'raw',
|
|
122
123
|
encoder.encode(secret),
|
|
123
124
|
{ name: 'HMAC', hash: 'SHA-256' },
|
|
124
125
|
false,
|
|
125
|
-
['sign']
|
|
126
|
+
['sign'],
|
|
126
127
|
);
|
|
127
128
|
|
|
128
129
|
const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(code));
|
|
@@ -143,7 +144,7 @@ export async function generateDeterministicHash(
|
|
|
143
144
|
export async function verifyDeterministicHash(
|
|
144
145
|
inputCode: string,
|
|
145
146
|
storedHash: string,
|
|
146
|
-
secret: string
|
|
147
|
+
secret: string,
|
|
147
148
|
): Promise<boolean> {
|
|
148
149
|
const hash = await generateDeterministicHash(inputCode, secret);
|
|
149
150
|
return hash === storedHash;
|
|
@@ -153,13 +154,30 @@ export async function verifyDeterministicHash(
|
|
|
153
154
|
export const cryptoUtils: ICrypto = {
|
|
154
155
|
uuidV4: (): string => crypto.randomUUID(),
|
|
155
156
|
|
|
157
|
+
ulid: (): string => {
|
|
158
|
+
function randomChar() {
|
|
159
|
+
const chars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; // Crockford's Base32
|
|
160
|
+
return chars[Math.floor(Math.random() * chars.length)];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const timestamp = Date.now();
|
|
164
|
+
const timestampStr = timestamp.toString(36).toUpperCase().padStart(10, '0');
|
|
165
|
+
|
|
166
|
+
let randomPart = '';
|
|
167
|
+
for (let i = 0; i < 16; i++) {
|
|
168
|
+
randomPart += randomChar();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return timestampStr + randomPart;
|
|
172
|
+
},
|
|
173
|
+
|
|
156
174
|
hash: async (password: string, salt = 10): Promise<string> => {
|
|
157
175
|
const importedKey = await crypto.subtle.importKey(
|
|
158
176
|
'raw',
|
|
159
177
|
encoder.encode(password),
|
|
160
178
|
{ name: 'PBKDF2' },
|
|
161
179
|
false,
|
|
162
|
-
['deriveBits']
|
|
180
|
+
['deriveBits'],
|
|
163
181
|
);
|
|
164
182
|
|
|
165
183
|
const derivedKey = await crypto.subtle.deriveBits(
|
|
@@ -170,7 +188,7 @@ export const cryptoUtils: ICrypto = {
|
|
|
170
188
|
hash: 'SHA-256',
|
|
171
189
|
},
|
|
172
190
|
importedKey,
|
|
173
|
-
256
|
|
191
|
+
256,
|
|
174
192
|
);
|
|
175
193
|
|
|
176
194
|
return Array.from(new Uint8Array(derivedKey))
|
|
@@ -200,7 +218,7 @@ export const cryptoUtils: ICrypto = {
|
|
|
200
218
|
const signatureBuffer = await crypto.subtle.sign(
|
|
201
219
|
'HMAC',
|
|
202
220
|
key,
|
|
203
|
-
encoder.encode(payload)
|
|
221
|
+
encoder.encode(payload),
|
|
204
222
|
);
|
|
205
223
|
const signature = toBase64(signatureBuffer);
|
|
206
224
|
|
|
@@ -227,7 +245,7 @@ export const cryptoUtils: ICrypto = {
|
|
|
227
245
|
'HMAC',
|
|
228
246
|
key,
|
|
229
247
|
fromBase64(signature).buffer as ArrayBuffer,
|
|
230
|
-
encoder.encode(payloadDecrypted)
|
|
248
|
+
encoder.encode(payloadDecrypted),
|
|
231
249
|
);
|
|
232
250
|
|
|
233
251
|
if (!valid) throw new Error('Invalid access token');
|
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
|
+
};
|