@tiny-codes/react-easy 1.4.5 → 1.4.6
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/CHANGELOG.md +10 -0
- package/es/utils/math.js +52 -4
- package/es/utils/math.js.map +1 -1
- package/lib/utils/math.js +37 -4
- package/lib/utils/math.js.map +2 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/es/utils/math.js
CHANGED
|
@@ -9,9 +9,57 @@
|
|
|
9
9
|
* @returns The generated random number | 生成的随机数
|
|
10
10
|
*/
|
|
11
11
|
export function random(min, max) {
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
var _process$versions, _nodeCrypto, _nodeCrypto2;
|
|
13
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
14
|
+
throw new TypeError('min and max must be finite numbers');
|
|
15
|
+
}
|
|
16
|
+
if (Math.floor(min) !== min || Math.floor(max) !== max) {
|
|
17
|
+
throw new TypeError('min and max must be integers');
|
|
18
|
+
}
|
|
19
|
+
if (min > max) {
|
|
20
|
+
var _ref = [max, min];
|
|
21
|
+
min = _ref[0];
|
|
22
|
+
max = _ref[1];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Try to require Node.js crypto
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
var nodeCrypto;
|
|
28
|
+
if (typeof process !== 'undefined' && (_process$versions = process.versions) !== null && _process$versions !== void 0 && _process$versions.node) {
|
|
29
|
+
try {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
31
|
+
nodeCrypto = require('crypto');
|
|
32
|
+
} catch (_unused) {
|
|
33
|
+
// ignore
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 1. Node.js crypto.randomInt
|
|
38
|
+
if ((_nodeCrypto = nodeCrypto) !== null && _nodeCrypto !== void 0 && _nodeCrypto.randomInt) {
|
|
39
|
+
return nodeCrypto.randomInt(min, max + 1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 2. Web Crypto (Browsers or Node 19+ webcrypto)
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
var webCrypto = globalThis.crypto || ((_nodeCrypto2 = nodeCrypto) === null || _nodeCrypto2 === void 0 ? void 0 : _nodeCrypto2.webcrypto);
|
|
45
|
+
if (webCrypto !== null && webCrypto !== void 0 && webCrypto.getRandomValues) {
|
|
46
|
+
var range = max - min + 1;
|
|
47
|
+
if (range <= 0) return min;
|
|
48
|
+
|
|
49
|
+
// 使用拒绝采样避免 (2^32 % range) 造成的微小偏差
|
|
50
|
+
var maxUint32 = 0xffffffff;
|
|
51
|
+
var limit = Math.floor((maxUint32 + 1) / range) * range;
|
|
52
|
+
var arr = new Uint32Array(1);
|
|
53
|
+
var v;
|
|
54
|
+
do {
|
|
55
|
+
webCrypto.getRandomValues(arr);
|
|
56
|
+
v = arr[0];
|
|
57
|
+
} while (v >= limit);
|
|
58
|
+
return min + v % range;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 3. Fallback, should not be used for cryptographic purposes
|
|
62
|
+
var _math = Math;
|
|
63
|
+
return Math.floor(_math.random() * (max - min + 1)) + min;
|
|
16
64
|
}
|
|
17
65
|
//# sourceMappingURL=math.js.map
|
package/es/utils/math.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["random","min","max","
|
|
1
|
+
{"version":3,"names":["random","min","max","_process$versions","_nodeCrypto","_nodeCrypto2","Number","isFinite","TypeError","Math","floor","_ref","nodeCrypto","process","versions","node","require","_unused","randomInt","webCrypto","globalThis","crypto","webcrypto","getRandomValues","range","maxUint32","limit","arr","Uint32Array","v","_math"],"sources":["../../src/utils/math.ts"],"sourcesContent":["/**\n * **EN**: Generate a random number within a specified range (inclusive on both ends)\n *\n * **CN**: 生成指定范围(两端包含)内的随机数\n *\n * @param min The minimum value (inclusive) | 最小值(包含)\n * @param max The maximum value (inclusive) | 最大值(包含)\n *\n * @returns The generated random number | 生成的随机数\n */\nexport function random(min: number, max: number): number {\n if (!Number.isFinite(min) || !Number.isFinite(max)) {\n throw new TypeError('min and max must be finite numbers');\n }\n if (Math.floor(min) !== min || Math.floor(max) !== max) {\n throw new TypeError('min and max must be integers');\n }\n if (min > max) {\n [min, max] = [max, min];\n }\n\n // Try to require Node.js crypto\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let nodeCrypto: any;\n if (typeof process !== 'undefined' && process.versions?.node) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n nodeCrypto = require('crypto');\n } catch {\n // ignore\n }\n }\n\n // 1. Node.js crypto.randomInt\n if (nodeCrypto?.randomInt) {\n return nodeCrypto.randomInt(min, max + 1);\n }\n\n // 2. Web Crypto (Browsers or Node 19+ webcrypto)\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const webCrypto: Crypto | undefined = (globalThis as any).crypto || nodeCrypto?.webcrypto;\n\n if (webCrypto?.getRandomValues) {\n const range = max - min + 1;\n if (range <= 0) return min;\n\n // 使用拒绝采样避免 (2^32 % range) 造成的微小偏差\n const maxUint32 = 0xffffffff;\n const limit = Math.floor((maxUint32 + 1) / range) * range;\n const arr = new Uint32Array(1);\n let v: number;\n do {\n webCrypto.getRandomValues(arr);\n v = arr[0];\n } while (v >= limit);\n return min + (v % range);\n }\n\n // 3. Fallback, should not be used for cryptographic purposes\n const _math = Math;\n return Math.floor(_math.random() * (max - min + 1)) + min;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,MAAMA,CAACC,GAAW,EAAEC,GAAW,EAAU;EAAA,IAAAC,iBAAA,EAAAC,WAAA,EAAAC,YAAA;EACvD,IAAI,CAACC,MAAM,CAACC,QAAQ,CAACN,GAAG,CAAC,IAAI,CAACK,MAAM,CAACC,QAAQ,CAAC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AK,CAAC,CAACe,SAAS,GAAG,CAAC,IAAID,KAAK,CAAC,GAAGA,KAAK;IACzD,IAAMG,GAAG,GAAG,IAAIC,WAAW,CAAC,CAAC,CAAC;IAC9B,IAAIC,CAAS;IACb,GAAG;MACDV,SAAS,CAACI,eAAe,CAACI,GAAG,CAAC;MAC9BE,CAAC,GAAGF,GAAG,CAAC,CAAC,CAAC;IACZ,CAAC,QAAQE,CAAC,IAAIH,KAAK;IACnB,OAAOzB,GAAG,GAAI4B,CAAC,GAAGL,KAAM;EAC1B;;EAEA;EACA,IAAMM,KAAK,GAAGrB,IAAI;EAClB,OAAOA,IAAI,CAACC,KAAK,CAACoB,KAAK,CAAC9B,MAAM,CAAC,CAAC,IAAIE,GAAG,GAAGD,GAAG,GAAG,CAAC,CAAC,CAAC,GAAGA,GAAG;AAC3D"}
|
package/lib/utils/math.js
CHANGED
|
@@ -23,10 +23,43 @@ __export(math_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(math_exports);
|
|
25
25
|
function random(min, max) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
var _a;
|
|
27
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) {
|
|
28
|
+
throw new TypeError("min and max must be finite numbers");
|
|
29
|
+
}
|
|
30
|
+
if (Math.floor(min) !== min || Math.floor(max) !== max) {
|
|
31
|
+
throw new TypeError("min and max must be integers");
|
|
32
|
+
}
|
|
33
|
+
if (min > max) {
|
|
34
|
+
[min, max] = [max, min];
|
|
35
|
+
}
|
|
36
|
+
let nodeCrypto;
|
|
37
|
+
if (typeof process !== "undefined" && ((_a = process.versions) == null ? void 0 : _a.node)) {
|
|
38
|
+
try {
|
|
39
|
+
nodeCrypto = require("crypto");
|
|
40
|
+
} catch {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (nodeCrypto == null ? void 0 : nodeCrypto.randomInt) {
|
|
44
|
+
return nodeCrypto.randomInt(min, max + 1);
|
|
45
|
+
}
|
|
46
|
+
const webCrypto = globalThis.crypto || (nodeCrypto == null ? void 0 : nodeCrypto.webcrypto);
|
|
47
|
+
if (webCrypto == null ? void 0 : webCrypto.getRandomValues) {
|
|
48
|
+
const range = max - min + 1;
|
|
49
|
+
if (range <= 0)
|
|
50
|
+
return min;
|
|
51
|
+
const maxUint32 = 4294967295;
|
|
52
|
+
const limit = Math.floor((maxUint32 + 1) / range) * range;
|
|
53
|
+
const arr = new Uint32Array(1);
|
|
54
|
+
let v;
|
|
55
|
+
do {
|
|
56
|
+
webCrypto.getRandomValues(arr);
|
|
57
|
+
v = arr[0];
|
|
58
|
+
} while (v >= limit);
|
|
59
|
+
return min + v % range;
|
|
60
|
+
}
|
|
61
|
+
const _math = Math;
|
|
62
|
+
return Math.floor(_math.random() * (max - min + 1)) + min;
|
|
30
63
|
}
|
|
31
64
|
// Annotate the CommonJS export names for ESM import in node:
|
|
32
65
|
0 && (module.exports = {
|
package/lib/utils/math.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/math.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * **EN**: Generate a random number within a specified range (inclusive on both ends)\n *\n * **CN**: 生成指定范围(两端包含)内的随机数\n *\n * @param min The minimum value (inclusive) | 最小值(包含)\n * @param max The maximum value (inclusive) | 最大值(包含)\n *\n * @returns The generated random number | 生成的随机数\n */\nexport function random(min: number, max: number): number {\n
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,OAAO,KAAa,KAAqB;
|
|
4
|
+
"sourcesContent": ["/**\n * **EN**: Generate a random number within a specified range (inclusive on both ends)\n *\n * **CN**: 生成指定范围(两端包含)内的随机数\n *\n * @param min The minimum value (inclusive) | 最小值(包含)\n * @param max The maximum value (inclusive) | 最大值(包含)\n *\n * @returns The generated random number | 生成的随机数\n */\nexport function random(min: number, max: number): number {\n if (!Number.isFinite(min) || !Number.isFinite(max)) {\n throw new TypeError('min and max must be finite numbers');\n }\n if (Math.floor(min) !== min || Math.floor(max) !== max) {\n throw new TypeError('min and max must be integers');\n }\n if (min > max) {\n [min, max] = [max, min];\n }\n\n // Try to require Node.js crypto\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let nodeCrypto: any;\n if (typeof process !== 'undefined' && process.versions?.node) {\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n nodeCrypto = require('crypto');\n } catch {\n // ignore\n }\n }\n\n // 1. Node.js crypto.randomInt\n if (nodeCrypto?.randomInt) {\n return nodeCrypto.randomInt(min, max + 1);\n }\n\n // 2. Web Crypto (Browsers or Node 19+ webcrypto)\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const webCrypto: Crypto | undefined = (globalThis as any).crypto || nodeCrypto?.webcrypto;\n\n if (webCrypto?.getRandomValues) {\n const range = max - min + 1;\n if (range <= 0) return min;\n\n // 使用拒绝采样避免 (2^32 % range) 造成的微小偏差\n const maxUint32 = 0xffffffff;\n const limit = Math.floor((maxUint32 + 1) / range) * range;\n const arr = new Uint32Array(1);\n let v: number;\n do {\n webCrypto.getRandomValues(arr);\n v = arr[0];\n } while (v >= limit);\n return min + (v % range);\n }\n\n // 3. Fallback, should not be used for cryptographic purposes\n const _math = Math;\n return Math.floor(_math.random() * (max - min + 1)) + min;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,OAAO,KAAa,KAAqB;AAVzD;AAWE,MAAI,CAAC,OAAO,SAAS,GAAG,KAAK,CAAC,OAAO,SAAS,GAAG,GAAG;AAClD,UAAM,IAAI,UAAU,oCAAoC;AAAA,EAC1D;AACA,MAAI,KAAK,MAAM,GAAG,MAAM,OAAO,KAAK,MAAM,GAAG,MAAM,KAAK;AACtD,UAAM,IAAI,UAAU,8BAA8B;AAAA,EAC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