@knsdev/node-utils 1.0.1 → 2.0.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/README.md +363 -0
- package/dist/CLengths.d.ts +60 -0
- package/dist/CLengths.d.ts.map +1 -0
- package/dist/CLengths.js +60 -0
- package/dist/CLengths.js.map +1 -0
- package/dist/Cookies.d.ts +33 -0
- package/dist/Cookies.d.ts.map +1 -0
- package/dist/Cookies.js +54 -0
- package/dist/Cookies.js.map +1 -0
- package/dist/EApiCodes.d.ts +2 -0
- package/dist/EApiCodes.d.ts.map +1 -0
- package/dist/EApiCodes.js +2 -0
- package/dist/EApiCodes.js.map +1 -0
- package/dist/ErrorClasses.d.ts +78 -0
- package/dist/ErrorClasses.d.ts.map +1 -0
- package/dist/ErrorClasses.js +118 -0
- package/dist/ErrorClasses.js.map +1 -0
- package/dist/FBUtils.d.ts +22 -0
- package/dist/FBUtils.d.ts.map +1 -0
- package/dist/FBUtils.js +68 -0
- package/dist/FBUtils.js.map +1 -0
- package/dist/GoogleUtils.d.ts +43 -0
- package/dist/GoogleUtils.d.ts.map +1 -0
- package/dist/GoogleUtils.js +79 -0
- package/dist/GoogleUtils.js.map +1 -0
- package/dist/HMACUtils.d.ts +40 -0
- package/dist/HMACUtils.d.ts.map +1 -0
- package/dist/HMACUtils.js +62 -0
- package/dist/HMACUtils.js.map +1 -0
- package/dist/HttpCodes.d.ts +26 -0
- package/dist/HttpCodes.d.ts.map +1 -0
- package/dist/HttpCodes.js +25 -0
- package/dist/HttpCodes.js.map +1 -0
- package/dist/IDUtils.d.ts +28 -0
- package/dist/IDUtils.d.ts.map +1 -0
- package/dist/IDUtils.js +47 -0
- package/dist/IDUtils.js.map +1 -0
- package/dist/JwtUtils.d.ts +51 -0
- package/dist/JwtUtils.d.ts.map +1 -0
- package/dist/JwtUtils.js +78 -0
- package/dist/JwtUtils.js.map +1 -0
- package/dist/Nums.d.ts +101 -0
- package/dist/Nums.d.ts.map +1 -0
- package/dist/Nums.js +141 -0
- package/dist/Nums.js.map +1 -0
- package/dist/PassUtils.d.ts +59 -0
- package/dist/PassUtils.d.ts.map +1 -0
- package/dist/PassUtils.js +84 -0
- package/dist/PassUtils.js.map +1 -0
- package/dist/Rand.d.ts +44 -0
- package/dist/Rand.d.ts.map +1 -0
- package/dist/Rand.js +72 -0
- package/dist/Rand.js.map +1 -0
- package/dist/TurnstileValidator.d.ts +57 -0
- package/dist/TurnstileValidator.d.ts.map +1 -0
- package/dist/TurnstileValidator.js +99 -0
- package/dist/TurnstileValidator.js.map +1 -0
- package/dist/bnUtils.d.ts +15 -0
- package/dist/bnUtils.d.ts.map +1 -0
- package/dist/bnUtils.js +67 -0
- package/dist/bnUtils.js.map +1 -0
- package/dist/getEnvv.d.ts +21 -0
- package/dist/getEnvv.d.ts.map +1 -0
- package/dist/getEnvv.js +68 -0
- package/dist/getEnvv.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/tryCatch.d.ts +23 -0
- package/dist/tryCatch.d.ts.map +1 -0
- package/dist/tryCatch.js +29 -0
- package/dist/tryCatch.js.map +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validators.d.ts +7 -0
- package/dist/validators.d.ts.map +1 -0
- package/dist/validators.js +11 -0
- package/dist/validators.js.map +1 -0
- package/dist/zod.d.ts +12 -0
- package/dist/zod.d.ts.map +1 -0
- package/dist/zod.js +33 -0
- package/dist/zod.js.map +1 -0
- package/dist/zodTypes.d.ts +2 -0
- package/dist/zodTypes.d.ts.map +1 -0
- package/dist/zodTypes.js +2 -0
- package/dist/zodTypes.js.map +1 -0
- package/package.json +2 -3
- package/dist/l.d.ts +0 -2
- package/dist/l.d.ts.map +0 -1
- package/dist/l.js +0 -2
- package/dist/l.js.map +0 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
declare class jwtUtils {
|
|
2
|
+
/**
|
|
3
|
+
* Signs a JWT (HS256) and returns the compact token.
|
|
4
|
+
* @template PayloadType - The type of the payload to sign.
|
|
5
|
+
* @param secret - Symmetric signing secret.
|
|
6
|
+
* @param data - Payload (object, string, or Buffer).
|
|
7
|
+
* @param expiresMs - Milliseconds until expiry (ignored if payload already sets `exp`).
|
|
8
|
+
* @returns Compact JWT string.
|
|
9
|
+
* @throws {InternalServerError} If signing fails.
|
|
10
|
+
*/
|
|
11
|
+
readonly signOrThrow: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, data: NoInfer<PayloadType>, expiresMs: number) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Signs a JWT (HS256); returns result or error object.
|
|
14
|
+
* @template PayloadType - The type of the payload to sign.
|
|
15
|
+
*/
|
|
16
|
+
readonly sign: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, data: NoInfer<PayloadType>, expiresMs: number) => {
|
|
17
|
+
data: PayloadType;
|
|
18
|
+
error: null;
|
|
19
|
+
} | {
|
|
20
|
+
data: null;
|
|
21
|
+
error: Error;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Verifies a JWT (HS256) and returns the payload.
|
|
25
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
26
|
+
* @param secret - Symmetric signing secret.
|
|
27
|
+
* @param token - Compact JWT string.
|
|
28
|
+
* @returns Decoded payload.
|
|
29
|
+
* @throws {BadRequest} If `token` is not a string.
|
|
30
|
+
* @throws {Unauthorized} If the token is invalid or expired.
|
|
31
|
+
*/
|
|
32
|
+
readonly verifyOrThrow: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, token: unknown) => PayloadType;
|
|
33
|
+
/**
|
|
34
|
+
* Verifies a JWT (HS256); returns payload or error object.
|
|
35
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
36
|
+
*/
|
|
37
|
+
readonly verify: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, token: unknown) => {
|
|
38
|
+
data: PayloadType;
|
|
39
|
+
error: null;
|
|
40
|
+
} | {
|
|
41
|
+
data: null;
|
|
42
|
+
error: Error;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* JWT utilities for use across projects.
|
|
47
|
+
* Import via: @sajjat/utils/JwtUtils
|
|
48
|
+
*/
|
|
49
|
+
export declare const JwtUtils: jwtUtils;
|
|
50
|
+
export {};
|
|
51
|
+
//# sourceMappingURL=JwtUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JwtUtils.d.ts","sourceRoot":"","sources":["../src/JwtUtils.ts"],"names":[],"mappings":"AAIA,cAAM,QAAQ;IAEb;;;;;;;;OAQG;IACH,QAAQ,CAAC,WAAW,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EAC1G,QAAQ,MAAM,EACd,MAAM,OAAO,CAAC,WAAW,CAAC,EAC1B,WAAW,MAAM,KACf,MAAM,CAaR;IAED;;;OAGG;IACH,QAAQ,CAAC,IAAI,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EACnG,QAAQ,MAAM,EACd,MAAM,OAAO,CAAC,WAAW,CAAC,EAC1B,WAAW,MAAM,KACf;QAAE,IAAI,EAAE,WAAW,CAAC;QAAC,KAAK,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAOnE;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,aAAa,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EAAE,QAAQ,MAAM,EAAE,OAAO,OAAO,KAC1I,WAAW,CAWb;IAED;;;OAGG;IACH,QAAQ,CAAC,MAAM,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EAAE,QAAQ,MAAM,EAAE,OAAO,OAAO,KACnI;QAAE,IAAI,EAAE,WAAW,CAAC;QAAC,KAAK,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAOnE;CACD;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,UAAiB,CAAC"}
|
package/dist/JwtUtils.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { BadRequest, InternalServerError, Unauthorized } from "./ErrorClasses.js";
|
|
2
|
+
import JWT from "jsonwebtoken";
|
|
3
|
+
class jwtUtils {
|
|
4
|
+
/**
|
|
5
|
+
* Signs a JWT (HS256) and returns the compact token.
|
|
6
|
+
* @template PayloadType - The type of the payload to sign.
|
|
7
|
+
* @param secret - Symmetric signing secret.
|
|
8
|
+
* @param data - Payload (object, string, or Buffer).
|
|
9
|
+
* @param expiresMs - Milliseconds until expiry (ignored if payload already sets `exp`).
|
|
10
|
+
* @returns Compact JWT string.
|
|
11
|
+
* @throws {InternalServerError} If signing fails.
|
|
12
|
+
*/
|
|
13
|
+
signOrThrow = (secret, data, expiresMs) => {
|
|
14
|
+
try {
|
|
15
|
+
const payload = data;
|
|
16
|
+
const options = { algorithm: 'HS256' };
|
|
17
|
+
// Do not pass expiresIn when payload already has exp (jsonwebtoken v9 throws otherwise)
|
|
18
|
+
if (payload.exp === undefined) {
|
|
19
|
+
options.expiresIn = `${expiresMs}ms`;
|
|
20
|
+
}
|
|
21
|
+
return JWT.sign(data, secret, options);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
throw new InternalServerError('JWT__ENCRYPTION_FAILED');
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Signs a JWT (HS256); returns result or error object.
|
|
29
|
+
* @template PayloadType - The type of the payload to sign.
|
|
30
|
+
*/
|
|
31
|
+
sign = (secret, data, expiresMs) => {
|
|
32
|
+
try {
|
|
33
|
+
return { data: this.signOrThrow(secret, data, expiresMs), error: null };
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
return { data: null, error: error };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Verifies a JWT (HS256) and returns the payload.
|
|
41
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
42
|
+
* @param secret - Symmetric signing secret.
|
|
43
|
+
* @param token - Compact JWT string.
|
|
44
|
+
* @returns Decoded payload.
|
|
45
|
+
* @throws {BadRequest} If `token` is not a string.
|
|
46
|
+
* @throws {Unauthorized} If the token is invalid or expired.
|
|
47
|
+
*/
|
|
48
|
+
verifyOrThrow = (secret, token) => {
|
|
49
|
+
if (typeof token !== 'string')
|
|
50
|
+
throw new BadRequest('JWT__INVALID_OR_EXPIRED');
|
|
51
|
+
try {
|
|
52
|
+
return JWT.verify(token, secret, {
|
|
53
|
+
algorithms: ['HS256'],
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new Unauthorized('JWT__INVALID_OR_EXPIRED');
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Verifies a JWT (HS256); returns payload or error object.
|
|
62
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
63
|
+
*/
|
|
64
|
+
verify = (secret, token) => {
|
|
65
|
+
try {
|
|
66
|
+
return { data: this.verifyOrThrow(secret, token), error: null };
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
return { data: null, error: error };
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* JWT utilities for use across projects.
|
|
75
|
+
* Import via: @sajjat/utils/JwtUtils
|
|
76
|
+
*/
|
|
77
|
+
export const JwtUtils = new jwtUtils();
|
|
78
|
+
//# sourceMappingURL=JwtUtils.js.map
|
|
@@ -0,0 +1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MAAM,IAAI,UAAU,CAAC,yBAAyB,CAAC,CAAC;QAE/E,IAAI,CAAC;YACJ,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE;gBAChC,UAAU,EAAE,CAAC,OAAO,CAAC;aACO,CAAgB,CAAC;QAC/C,CAAC;QACD,MAAM,CAAC;YACN,MAAM,IAAI,YAAY,CAAC,yBAAyB,CAAC,CAAC;QACnD,CAAC;IACF,CAAC,CAAA;IAED;;;OAGG;IACM,MAAM,GAAG,CAAsF,MAAc,EAAE,KAAc,EACjE,EAAE;QACtE,IAAI,CAAC;YACJ,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,aAAa,CAAc,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;QAC9E,CAAC;QACD,OAAO,KAAK,EAAE,CAAC;YACd,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAc,EAAE,CAAC;QAC9C,CAAC;IACF,CAAC,CAAA;CACD;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC"}
|
package/dist/Nums.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clamp a number to an inclusive range.
|
|
3
|
+
* @param value - Input number.
|
|
4
|
+
* @param min - Inclusive lower bound.
|
|
5
|
+
* @param max - Inclusive upper bound.
|
|
6
|
+
* @returns `value` constrained to `[min, max]`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const clamp: (value: number, min: number, max: number) => number;
|
|
9
|
+
/**
|
|
10
|
+
* Check whether a number falls within an inclusive range.
|
|
11
|
+
* @param value - Number to test.
|
|
12
|
+
* @param min - Inclusive lower bound.
|
|
13
|
+
* @param max - Inclusive upper bound.
|
|
14
|
+
* @returns `true` when `min <= value <= max`; otherwise `false`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const isBetween: (value: number, min: number, max: number) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Round a number to a fixed number of decimal places.
|
|
19
|
+
* @param value - Number to round.
|
|
20
|
+
* @param decimals - Decimal places to keep.
|
|
21
|
+
* @returns Rounded number.
|
|
22
|
+
*/
|
|
23
|
+
export declare const roundTo: (value: number, decimals: number) => number;
|
|
24
|
+
/**
|
|
25
|
+
* Round a number down to a fixed number of decimal places.
|
|
26
|
+
* @param value - Number to floor.
|
|
27
|
+
* @param decimals - Decimal places to keep.
|
|
28
|
+
* @returns Floored number.
|
|
29
|
+
*/
|
|
30
|
+
export declare const floorTo: (value: number, decimals: number) => number;
|
|
31
|
+
/**
|
|
32
|
+
* Round a number up to a fixed number of decimal places.
|
|
33
|
+
* @param value - Number to ceil.
|
|
34
|
+
* @param decimals - Decimal places to keep.
|
|
35
|
+
* @returns Ceiled number.
|
|
36
|
+
*/
|
|
37
|
+
export declare const ceilTo: (value: number, decimals: number) => number;
|
|
38
|
+
/**
|
|
39
|
+
* Parse a value into an integer safely.
|
|
40
|
+
* @param value - Value to parse.
|
|
41
|
+
* @param radix - Numeric base for parsing string inputs.
|
|
42
|
+
* @returns Parsed integer, or `null` for `null`/`undefined`/`NaN`.
|
|
43
|
+
*/
|
|
44
|
+
export declare const parseIntSafe: (value: string | number | null | undefined, radix?: number) => number | null;
|
|
45
|
+
/**
|
|
46
|
+
* Parse a value into a floating-point number safely.
|
|
47
|
+
* @param value - Value to parse.
|
|
48
|
+
* @returns Parsed number, or `null` for `null`/`undefined`/`NaN`.
|
|
49
|
+
*/
|
|
50
|
+
export declare const parseFloatSafe: (value: string | number | null | undefined) => number | null;
|
|
51
|
+
/**
|
|
52
|
+
* Type guard for finite numbers.
|
|
53
|
+
* @param value - Unknown input.
|
|
54
|
+
* @returns `true` when `value` is a finite `number`.
|
|
55
|
+
*/
|
|
56
|
+
export declare const isFiniteNumber: (value: unknown) => value is number;
|
|
57
|
+
/**
|
|
58
|
+
* Type guard for integer numbers.
|
|
59
|
+
* @param value - Unknown input.
|
|
60
|
+
* @returns `true` when `value` is an integer `number`.
|
|
61
|
+
*/
|
|
62
|
+
export declare const isInteger: (value: unknown) => value is number;
|
|
63
|
+
/**
|
|
64
|
+
* Format a number using `Intl.NumberFormat`.
|
|
65
|
+
* @param value - Number to format.
|
|
66
|
+
* @param locale - BCP47 locale string.
|
|
67
|
+
* @param options - Number format options.
|
|
68
|
+
* @returns Formatted number string.
|
|
69
|
+
*/
|
|
70
|
+
export declare const formatNumber: (value: number, locale?: string, options?: Intl.NumberFormatOptions) => string;
|
|
71
|
+
/**
|
|
72
|
+
* Format a number with group separators.
|
|
73
|
+
* @param value - Number to format.
|
|
74
|
+
* @param locale - BCP47 locale string. (default: 'en')
|
|
75
|
+
* @param decimals - Optional fixed fraction digits.
|
|
76
|
+
* @returns Formatted string with commas/grouping.
|
|
77
|
+
*/
|
|
78
|
+
export declare const formatWithCommas: (value: number, locale?: string, decimals?: number) => string;
|
|
79
|
+
/**
|
|
80
|
+
* Calculate percentage of `part` out of `total`.
|
|
81
|
+
* @param part - Numerator.
|
|
82
|
+
* @param total - Denominator.
|
|
83
|
+
* @param decimals - Decimal places in the result.
|
|
84
|
+
* @returns Percentage value. Returns `0` if `total` is `0`.
|
|
85
|
+
*/
|
|
86
|
+
export declare const percentage: (part: number, total: number, decimals?: number) => number;
|
|
87
|
+
/**
|
|
88
|
+
* Convert unknown input to a finite number with fallback.
|
|
89
|
+
* @param value - Value to convert.
|
|
90
|
+
* @param fallback - Value used when conversion is not finite.
|
|
91
|
+
* @returns Finite number from input, or `fallback`.
|
|
92
|
+
*/
|
|
93
|
+
export declare const toNumber: (value: unknown, fallback: number) => number;
|
|
94
|
+
/**
|
|
95
|
+
* Convert unknown input to an integer with fallback.
|
|
96
|
+
* @param value - Value to convert.
|
|
97
|
+
* @param fallback - Value used when conversion is not an integer.
|
|
98
|
+
* @returns Integer from input, or `fallback`.
|
|
99
|
+
*/
|
|
100
|
+
export declare const toInt: (value: unknown, fallback: number) => number;
|
|
101
|
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gB,GAAI,OAAO,MAAM,EAAE,eAAa,EAAE,WAAW,MAAM,KAAG,MAOlF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,EAAE,OAAO,MAAM,EAAE,iBAAY,KAAG,MAGtE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,EAAE,UAAU,MAAM,KAAG,MAG3D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,OAAO,EAAE,UAAU,MAAM,KAAG,MAGxD,CAAC"}
|
package/dist/Nums.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clamp a number to an inclusive range.
|
|
3
|
+
* @param value - Input number.
|
|
4
|
+
* @param min - Inclusive lower bound.
|
|
5
|
+
* @param max - Inclusive upper bound.
|
|
6
|
+
* @returns `value` constrained to `[min, max]`.
|
|
7
|
+
*/
|
|
8
|
+
export const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
9
|
+
/**
|
|
10
|
+
* Check whether a number falls within an inclusive range.
|
|
11
|
+
* @param value - Number to test.
|
|
12
|
+
* @param min - Inclusive lower bound.
|
|
13
|
+
* @param max - Inclusive upper bound.
|
|
14
|
+
* @returns `true` when `min <= value <= max`; otherwise `false`.
|
|
15
|
+
*/
|
|
16
|
+
export const isBetween = (value, min, max) => value >= min && value <= max;
|
|
17
|
+
/**
|
|
18
|
+
* Round a number to a fixed number of decimal places.
|
|
19
|
+
* @param value - Number to round.
|
|
20
|
+
* @param decimals - Decimal places to keep.
|
|
21
|
+
* @returns Rounded number.
|
|
22
|
+
*/
|
|
23
|
+
export const roundTo = (value, decimals) => {
|
|
24
|
+
const factor = 10 ** decimals;
|
|
25
|
+
return Math.round(value * factor) / factor;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Round a number down to a fixed number of decimal places.
|
|
29
|
+
* @param value - Number to floor.
|
|
30
|
+
* @param decimals - Decimal places to keep.
|
|
31
|
+
* @returns Floored number.
|
|
32
|
+
*/
|
|
33
|
+
export const floorTo = (value, decimals) => {
|
|
34
|
+
const factor = 10 ** decimals;
|
|
35
|
+
return Math.floor(value * factor) / factor;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Round a number up to a fixed number of decimal places.
|
|
39
|
+
* @param value - Number to ceil.
|
|
40
|
+
* @param decimals - Decimal places to keep.
|
|
41
|
+
* @returns Ceiled number.
|
|
42
|
+
*/
|
|
43
|
+
export const ceilTo = (value, decimals) => {
|
|
44
|
+
const factor = 10 ** decimals;
|
|
45
|
+
return Math.ceil(value * factor) / factor;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Parse a value into an integer safely.
|
|
49
|
+
* @param value - Value to parse.
|
|
50
|
+
* @param radix - Numeric base for parsing string inputs.
|
|
51
|
+
* @returns Parsed integer, or `null` for `null`/`undefined`/`NaN`.
|
|
52
|
+
*/
|
|
53
|
+
export const parseIntSafe = (value, radix = 10) => {
|
|
54
|
+
if (value === null || value === undefined)
|
|
55
|
+
return null;
|
|
56
|
+
const n = typeof value === 'number' ? value : parseInt(String(value), radix);
|
|
57
|
+
return Number.isNaN(n) ? null : n;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Parse a value into a floating-point number safely.
|
|
61
|
+
* @param value - Value to parse.
|
|
62
|
+
* @returns Parsed number, or `null` for `null`/`undefined`/`NaN`.
|
|
63
|
+
*/
|
|
64
|
+
export const parseFloatSafe = (value) => {
|
|
65
|
+
if (value === null || value === undefined)
|
|
66
|
+
return null;
|
|
67
|
+
const n = typeof value === 'number' ? value : parseFloat(String(value));
|
|
68
|
+
return Number.isNaN(n) ? null : n;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Type guard for finite numbers.
|
|
72
|
+
* @param value - Unknown input.
|
|
73
|
+
* @returns `true` when `value` is a finite `number`.
|
|
74
|
+
*/
|
|
75
|
+
export const isFiniteNumber = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
76
|
+
/**
|
|
77
|
+
* Type guard for integer numbers.
|
|
78
|
+
* @param value - Unknown input.
|
|
79
|
+
* @returns `true` when `value` is an integer `number`.
|
|
80
|
+
*/
|
|
81
|
+
export const isInteger = (value) => typeof value === 'number' && Number.isInteger(value);
|
|
82
|
+
/**
|
|
83
|
+
* Format a number using `Intl.NumberFormat`.
|
|
84
|
+
* @param value - Number to format.
|
|
85
|
+
* @param locale - BCP47 locale string.
|
|
86
|
+
* @param options - Number format options.
|
|
87
|
+
* @returns Formatted number string.
|
|
88
|
+
*/
|
|
89
|
+
export const formatNumber = (value, locale = 'en', options) => new Intl.NumberFormat(locale, {
|
|
90
|
+
minimumFractionDigits: 0,
|
|
91
|
+
maximumFractionDigits: 2,
|
|
92
|
+
...options,
|
|
93
|
+
}).format(value);
|
|
94
|
+
/**
|
|
95
|
+
* Format a number with group separators.
|
|
96
|
+
* @param value - Number to format.
|
|
97
|
+
* @param locale - BCP47 locale string. (default: 'en')
|
|
98
|
+
* @param decimals - Optional fixed fraction digits.
|
|
99
|
+
* @returns Formatted string with commas/grouping.
|
|
100
|
+
*/
|
|
101
|
+
export const formatWithCommas = (value, locale = 'en', decimals) => {
|
|
102
|
+
const opts = {};
|
|
103
|
+
if (decimals !== undefined) {
|
|
104
|
+
opts.minimumFractionDigits = decimals;
|
|
105
|
+
opts.maximumFractionDigits = decimals;
|
|
106
|
+
}
|
|
107
|
+
return new Intl.NumberFormat(locale, opts).format(value);
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Calculate percentage of `part` out of `total`.
|
|
111
|
+
* @param part - Numerator.
|
|
112
|
+
* @param total - Denominator.
|
|
113
|
+
* @param decimals - Decimal places in the result.
|
|
114
|
+
* @returns Percentage value. Returns `0` if `total` is `0`.
|
|
115
|
+
*/
|
|
116
|
+
export const percentage = (part, total, decimals = 0) => {
|
|
117
|
+
if (total === 0)
|
|
118
|
+
return 0;
|
|
119
|
+
return roundTo((part / total) * 100, decimals);
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Convert unknown input to a finite number with fallback.
|
|
123
|
+
* @param value - Value to convert.
|
|
124
|
+
* @param fallback - Value used when conversion is not finite.
|
|
125
|
+
* @returns Finite number from input, or `fallback`.
|
|
126
|
+
*/
|
|
127
|
+
export const toNumber = (value, fallback) => {
|
|
128
|
+
const n = typeof value === 'number' ? value : parseFloat(String(value));
|
|
129
|
+
return Number.isFinite(n) ? n : fallback;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Convert unknown input to an integer with fallback.
|
|
133
|
+
* @param value - Value to convert.
|
|
134
|
+
* @param fallback - Value used when conversion is not an integer.
|
|
135
|
+
* @returns Integer from input, or `fallback`.
|
|
136
|
+
*/
|
|
137
|
+
export const toInt = (value, fallback) => {
|
|
138
|
+
const n = typeof value === 'number' ? value : parseInt(String(value), 10);
|
|
139
|
+
return Number.isInteger(n) ? n : fallback;
|
|
140
|
+
};
|
|
141
|
+
//# sourceMappingURL=Nums.js.map
|
package/dist/Nums.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Nums.js","sourceRoot":"","sources":["../src/Nums.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW,EAAU,EAAE,CACxE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAErC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW,EAAW,EAAE,CAC7E,KAAK,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,CAAC;AAE9B;;;;;GAKG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAa,EAAE,QAAgB,E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,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC1D,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAE,KAAa,EAAE,QAAQ,GAAG,CAAC,EAAU,EAAE;IAC/E,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAC1B,OAAO,OAAO,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,GAAG,GAAG,EAAE,QAAQ,CAAC,CAAC;AAChD,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAE,QAAgB,EAAU,EAAE;IACpE,MAAM,CAAC,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACxE,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC1C,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,KAAc,EAAE,QAAgB,EAAU,EAAE;IACjE,MAAM,CAAC,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;IAC1E,OAAO,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC3C,CAAC,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
declare class passwordUtils {
|
|
2
|
+
/**
|
|
3
|
+
* Hash a password with bcrypt and return result as success/error object.
|
|
4
|
+
* @param password - Plain-text password.
|
|
5
|
+
* @param saltRounds - BCrypt salt rounds.
|
|
6
|
+
* @returns Success object with hash, or failure object with error.
|
|
7
|
+
*/
|
|
8
|
+
readonly hashPassword: (password: string, saltRounds?: number) => Promise<{
|
|
9
|
+
success: true;
|
|
10
|
+
hashedPassword: string;
|
|
11
|
+
} | {
|
|
12
|
+
success: false;
|
|
13
|
+
error: Error;
|
|
14
|
+
}>;
|
|
15
|
+
/**
|
|
16
|
+
* Hash a password with bcrypt and throw on failure.
|
|
17
|
+
* @param password - Plain-text password.
|
|
18
|
+
* @param saltRounds - BCrypt salt rounds.
|
|
19
|
+
* @returns Hashed password string.
|
|
20
|
+
* @throws {InternalServerError} If hashing fails.
|
|
21
|
+
*/
|
|
22
|
+
readonly hashPasswordOrThrow: (password: string, saltRounds?: number) => Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Compare plain-text password with bcrypt hash.
|
|
25
|
+
* @param password - Plain-text password.
|
|
26
|
+
* @param hashedPassword - BCrypt hash.
|
|
27
|
+
* @returns `true`/`false` comparison result, or `false` if comparison throws.
|
|
28
|
+
*/
|
|
29
|
+
readonly comparePassword: (password: string, hashedPassword: string) => Promise<boolean | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Compare plain-text password with bcrypt hash and throw on failure.
|
|
32
|
+
* @param password - Plain-text password.
|
|
33
|
+
* @param hashedPassword - BCrypt hash.
|
|
34
|
+
* @returns `true` when password matches hash, otherwise `false`.
|
|
35
|
+
* @throws {InternalServerError} If comparison fails unexpectedly.
|
|
36
|
+
*/
|
|
37
|
+
readonly comparePasswordOrThrow: (password: string, hashedPassword: string) => Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* Validate password length boundaries.
|
|
40
|
+
* @param password - Plain-text password.
|
|
41
|
+
* @param minLength - Inclusive minimum length.
|
|
42
|
+
* @param maxLength - Inclusive maximum length.
|
|
43
|
+
* @returns `true` when password length is within bounds.
|
|
44
|
+
*/
|
|
45
|
+
readonly validateStrength: (password: string, minLength: number, maxLength: number) => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Check whether a string appears to be a bcrypt hash.
|
|
48
|
+
* @param value - Input string.
|
|
49
|
+
* @returns `true` when input matches bcrypt hash pattern.
|
|
50
|
+
*/
|
|
51
|
+
readonly isBcryptHash: (value: string) => boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Singleton instance of password utilities.
|
|
55
|
+
*/
|
|
56
|
+
export declare const PasswordUtils: passwordUtils;
|
|
57
|
+
export declare const bbbb = 1;
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=PassUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PassUtils.d.ts","sourceRoot":"","sources":["../src/PassUtils.ts"],"names":[],"mappings":"AAMA,cAAM,aAAa;IAClB;;;;;OAKG;IACH,QAAQ,CAAC,YAAY,GACpB,UAAU,MAAM,EAChB,mBAAgC,KAC9B,OAAO,CAAC;QAAE,OAAO,EAAE,IAAI,CAAC;QAAC,cAAc,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,OAAO,EAAE,KAAK,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAAC,CAOtF;IAEF;;;;;;OAMG;IACH,QAAQ,CAAC,mBAAmB,GAC3B,UAAU,MAAM,EAChB,mBAAgC,KAC9B,OAAO,CAAC,MAAM,CAAC,CAOhB;IAEF;;;;;OAKG;IACH,QAAQ,CAAC,eAAe,GACvB,UAAU,MAAM,EAChB,gBAAgB,MAAM,KACpB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAOxB;IAEF;;;;;;OAMG;IACH,QAAQ,CAAC,sBAAsB,GAC9B,UAAU,MAAM,EAChB,gBAAgB,MAAM,KACpB,OAAO,CAAC,OAAO,CAAC,CAOjB;IAEF;;;;;;OAMG;IACH,QAAQ,CAAC,gBAAgB,GAAI,UAAU,MAAM,EAAE,WAAW,MAAM,EAAE,WAAW,MAAM,KAAG,OAAO,CAC/B;IAE9D;;;;OAIG;IACH,QAAQ,CAAC,YAAY,GAAI,OAAO,MAAM,KAAG,OAAO,CACI;CACpD;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,eAAsB,CAAC;AAEjD,eAAO,MAAM,IAAI,IAAM,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import bcrypt from "bcrypt";
|
|
2
|
+
import { InternalServerError } from "./ErrorClasses.js";
|
|
3
|
+
import { aaa } from "./HMACUtils.js";
|
|
4
|
+
const DEFAULT_SALT_ROUNDS = 10;
|
|
5
|
+
class passwordUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Hash a password with bcrypt and return result as success/error object.
|
|
8
|
+
* @param password - Plain-text password.
|
|
9
|
+
* @param saltRounds - BCrypt salt rounds.
|
|
10
|
+
* @returns Success object with hash, or failure object with error.
|
|
11
|
+
*/
|
|
12
|
+
hashPassword = async (password, saltRounds = DEFAULT_SALT_ROUNDS) => {
|
|
13
|
+
try {
|
|
14
|
+
return { success: true, hashedPassword: await bcrypt.hash(password, saltRounds) };
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return { success: false, error: error };
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Hash a password with bcrypt and throw on failure.
|
|
22
|
+
* @param password - Plain-text password.
|
|
23
|
+
* @param saltRounds - BCrypt salt rounds.
|
|
24
|
+
* @returns Hashed password string.
|
|
25
|
+
* @throws {InternalServerError} If hashing fails.
|
|
26
|
+
*/
|
|
27
|
+
hashPasswordOrThrow = async (password, saltRounds = DEFAULT_SALT_ROUNDS) => {
|
|
28
|
+
try {
|
|
29
|
+
return await bcrypt.hash(password, saltRounds);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new InternalServerError();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Compare plain-text password with bcrypt hash.
|
|
37
|
+
* @param password - Plain-text password.
|
|
38
|
+
* @param hashedPassword - BCrypt hash.
|
|
39
|
+
* @returns `true`/`false` comparison result, or `false` if comparison throws.
|
|
40
|
+
*/
|
|
41
|
+
comparePassword = async (password, hashedPassword) => {
|
|
42
|
+
try {
|
|
43
|
+
return await bcrypt.compare(password, hashedPassword);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Compare plain-text password with bcrypt hash and throw on failure.
|
|
51
|
+
* @param password - Plain-text password.
|
|
52
|
+
* @param hashedPassword - BCrypt hash.
|
|
53
|
+
* @returns `true` when password matches hash, otherwise `false`.
|
|
54
|
+
* @throws {InternalServerError} If comparison fails unexpectedly.
|
|
55
|
+
*/
|
|
56
|
+
comparePasswordOrThrow = async (password, hashedPassword) => {
|
|
57
|
+
try {
|
|
58
|
+
return await bcrypt.compare(password, hashedPassword);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new InternalServerError();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Validate password length boundaries.
|
|
66
|
+
* @param password - Plain-text password.
|
|
67
|
+
* @param minLength - Inclusive minimum length.
|
|
68
|
+
* @param maxLength - Inclusive maximum length.
|
|
69
|
+
* @returns `true` when password length is within bounds.
|
|
70
|
+
*/
|
|
71
|
+
validateStrength = (password, minLength, maxLength) => password.length >= minLength && password.length <= maxLength;
|
|
72
|
+
/**
|
|
73
|
+
* Check whether a string appears to be a bcrypt hash.
|
|
74
|
+
* @param value - Input string.
|
|
75
|
+
* @returns `true` when input matches bcrypt hash pattern.
|
|
76
|
+
*/
|
|
77
|
+
isBcryptHash = (value) => /^\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}$/.test(value);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Singleton instance of password utilities.
|
|
81
|
+
*/
|
|
82
|
+
export const PasswordUtils = new passwordUtils();
|
|
83
|
+
export const bbbb = aaa;
|
|
84
|
+
//# sourceMappingURL=P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MG;IACM,sBAAsB,GAAG,KAAK,EACtC,QAAgB,EAChB,cAAsB,EACH,EAAE;QACrB,IAAI,CAAC;YACJ,OAAO,MAAM,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;QACvD,CAAC;QACD,MAAM,CAAC;YACN,MAAM,IAAI,mBAAmB,EAAE,CAAC;QACjC,CAAC;IACF,CAAC,CAAC;IAEF;;;;;;OAMG;IACM,gBAAgB,GAAG,CAAC,QAAgB,EAAE,SAAiB,EAAE,SAAiB,EAAW,EAAE,CAC/F,QAAQ,CAAC,MAAM,IAAI,SAAS,IAAI,QAAQ,CAAC,MAAM,IAAI,SAAS,CAAC;IAE9D;;;;OAIG;IACM,YAAY,GAAG,CAAC,KAAa,EAAW,EAAE,CAClD,sCAAsC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;CACpD;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC;AAEjD,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC"}
|
package/dist/Rand.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pseudo-random integer between `min` and `max` (inclusive) using `Math.random()`.
|
|
3
|
+
* @param min - Inclusive lower bound.
|
|
4
|
+
* @param max - Inclusive upper bound.
|
|
5
|
+
* @returns A pseudo-random integer `n` such that `min <= n <= max`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const srandInt: (min: number, max: number) => number;
|
|
8
|
+
/**
|
|
9
|
+
* Crypto-random integer between `min` and `max` (inclusive).
|
|
10
|
+
* @param min - Inclusive lower bound. Must be a safe integer.
|
|
11
|
+
* @param max - Inclusive upper bound. Must be a safe integer.
|
|
12
|
+
* @returns A crypto-random integer `n` such that `min <= n <= max`.
|
|
13
|
+
* @throws {TypeError} If `min` or `max` are not safe integers.
|
|
14
|
+
* @throws {RangeError} If `max < min`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const randInt: (min: number, max: number) => number;
|
|
17
|
+
/**
|
|
18
|
+
* Pseudo-random float between `min` and `max`.
|
|
19
|
+
* Note: uses `Math.random()`, so the result is effectively in `[min, max)` (with `max` excluded).
|
|
20
|
+
* @param min - Inclusive lower bound.
|
|
21
|
+
* @param max - Upper bound (effectively exclusive).
|
|
22
|
+
* @returns A pseudo-random number `x` such that `min <= x < max`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const srandFloat: (min: number, max: number) => number;
|
|
25
|
+
/**
|
|
26
|
+
* Crypto-random float between `min` and `max` (inclusive).
|
|
27
|
+
* @param min - Inclusive lower bound.
|
|
28
|
+
* @param max - Inclusive upper bound.
|
|
29
|
+
* @returns A crypto-random number `x` such that `min <= x <= max` (subject to normal floating-point rounding).
|
|
30
|
+
*/
|
|
31
|
+
export declare const randFloat: (min: number, max: number) => number;
|
|
32
|
+
/**
|
|
33
|
+
* Generates a crypto-random RFC4122 v4 UUID.
|
|
34
|
+
* @returns A UUID string (e.g. `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`).
|
|
35
|
+
*/
|
|
36
|
+
export declare const randUuid: () => string;
|
|
37
|
+
/**
|
|
38
|
+
* Crypto-random Base58 string.
|
|
39
|
+
* Uses `randInt(0, max)` with an inclusive `max` contract.
|
|
40
|
+
* @param length - Number of characters to generate.
|
|
41
|
+
* @returns A Base58 string of length `length` (or `""` if `length <= 0`).
|
|
42
|
+
*/
|
|
43
|
+
export declare const randStr: (length: number) => string;
|
|
44
|
+
//# sourceMappingURL=Rand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Rand.d.ts","sourceRoot":"","sources":["../src/Rand.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,EAAE,KAAK,MAAM,WACC,CAAC;AAEnD;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,GAAI,KAAK,MAAM,EAAE,KAAK,MAAM,WAW/C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,KAAK,MAAM,EAAE,KAAK,MAAM,WAAsC,CAAC;AAE1F;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,MAAM,EAAE,KAAK,MAAM,WAUjD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,QAAQ,QAAO,MAAsB,CAAC;AAEnD;;;;;GAKG;AACH,eAAO,MAAM,OAAO,GAAI,QAAQ,MAAM,WAOrC,CAAA"}
|
package/dist/Rand.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { randomUUID, randomInt as cryptoRandomInt } from "crypto";
|
|
2
|
+
const BASE58_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
3
|
+
/**
|
|
4
|
+
* Pseudo-random integer between `min` and `max` (inclusive) using `Math.random()`.
|
|
5
|
+
* @param min - Inclusive lower bound.
|
|
6
|
+
* @param max - Inclusive upper bound.
|
|
7
|
+
* @returns A pseudo-random integer `n` such that `min <= n <= max`.
|
|
8
|
+
*/
|
|
9
|
+
export const srandInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
|
|
10
|
+
/**
|
|
11
|
+
* Crypto-random integer between `min` and `max` (inclusive).
|
|
12
|
+
* @param min - Inclusive lower bound. Must be a safe integer.
|
|
13
|
+
* @param max - Inclusive upper bound. Must be a safe integer.
|
|
14
|
+
* @returns A crypto-random integer `n` such that `min <= n <= max`.
|
|
15
|
+
* @throws {TypeError} If `min` or `max` are not safe integers.
|
|
16
|
+
* @throws {RangeError} If `max < min`.
|
|
17
|
+
*/
|
|
18
|
+
export const randInt = (min, max) => {
|
|
19
|
+
// Node's `crypto.randomInt` uses an exclusive upper bound.
|
|
20
|
+
// We keep the previous inclusive `max` contract.
|
|
21
|
+
if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) {
|
|
22
|
+
throw new TypeError("randInt(min, max) requires safe integers");
|
|
23
|
+
}
|
|
24
|
+
if (max < min) {
|
|
25
|
+
throw new RangeError("randInt(min, max): max must be >= min");
|
|
26
|
+
}
|
|
27
|
+
return cryptoRandomInt(min, max + 1);
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Pseudo-random float between `min` and `max`.
|
|
31
|
+
* Note: uses `Math.random()`, so the result is effectively in `[min, max)` (with `max` excluded).
|
|
32
|
+
* @param min - Inclusive lower bound.
|
|
33
|
+
* @param max - Upper bound (effectively exclusive).
|
|
34
|
+
* @returns A pseudo-random number `x` such that `min <= x < max`.
|
|
35
|
+
*/
|
|
36
|
+
export const srandFloat = (min, max) => Math.random() * (max - min) + min;
|
|
37
|
+
/**
|
|
38
|
+
* Crypto-random float between `min` and `max` (inclusive).
|
|
39
|
+
* @param min - Inclusive lower bound.
|
|
40
|
+
* @param max - Inclusive upper bound.
|
|
41
|
+
* @returns A crypto-random number `x` such that `min <= x <= max` (subject to normal floating-point rounding).
|
|
42
|
+
*/
|
|
43
|
+
export const randFloat = (min, max) => {
|
|
44
|
+
const range = max - min;
|
|
45
|
+
if (range === 0)
|
|
46
|
+
return min;
|
|
47
|
+
// Uniform value in [0, 1] using 48 random bits (inclusive upper endpoint).
|
|
48
|
+
// `crypto.randomInt(min, max)` treats `max` as exclusive.
|
|
49
|
+
const maxInclusiveInt = 2 ** 48 - 2;
|
|
50
|
+
const uInt = cryptoRandomInt(0, maxInclusiveInt + 1); // -> [0, maxInclusiveInt]
|
|
51
|
+
const u = uInt / maxInclusiveInt; // -> [0, 1]
|
|
52
|
+
return min + u * range;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Generates a crypto-random RFC4122 v4 UUID.
|
|
56
|
+
* @returns A UUID string (e.g. `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`).
|
|
57
|
+
*/
|
|
58
|
+
export const randUuid = () => randomUUID();
|
|
59
|
+
/**
|
|
60
|
+
* Crypto-random Base58 string.
|
|
61
|
+
* Uses `randInt(0, max)` with an inclusive `max` contract.
|
|
62
|
+
* @param length - Number of characters to generate.
|
|
63
|
+
* @returns A Base58 string of length `length` (or `""` if `length <= 0`).
|
|
64
|
+
*/
|
|
65
|
+
export const randStr = (length) => {
|
|
66
|
+
let result = '';
|
|
67
|
+
for (let i = 0; i < length; i++) {
|
|
68
|
+
result += BASE58_CHARS[randInt(0, BASE58_CHARS.length - 1)];
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
//# sourceMappingURL=Rand.js.map
|