a-js-tools 1.0.13-beta.2 → 1.0.13-beta.3

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.
Files changed (2) hide show
  1. package/index.cjs.js +12 -6
  2. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -248,13 +248,19 @@ function getRandomString(options) {
248
248
  // 添加特殊字符
249
249
  if (initOptions.includeSpecial)
250
250
  interleaveString(templateCharsArr, initOptions.chars3);
251
- // 使用密码学安全的随机数生成器
252
- const bytes = globalThis.crypto.getRandomValues(new Uint8Array(initOptions.length));
253
251
  let result = '';
254
- /** 获取最后的 chars 数据 */
255
- const chars = templateCharsArr.join('');
256
- // 循环遍历
257
- bytes.forEach(byte => (result += chars.charAt(byte % chars.length)));
252
+ const templateCharsArrLength = templateCharsArr.length;
253
+ if (globalThis && globalThis.crypto && globalThis.crypto.getRandomValues) {
254
+ // 使用密码学安全的随机数生成器
255
+ const bytes = globalThis.crypto.getRandomValues(new Uint8Array(initOptions.length));
256
+ /** 获取最后的 chars 数据 */
257
+ // 循环遍历
258
+ bytes.forEach(byte => (result += templateCharsArr[byte % templateCharsArrLength]));
259
+ }
260
+ else {
261
+ for (let i = 0; i < initOptions.length; i++)
262
+ result += templateCharsArr[getRandomInt(templateCharsArrLength - 1)];
263
+ }
258
264
  /**
259
265
  *
260
266
  * 字符串交叉函数
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "1.0.13-beta.2",
3
+ "version": "1.0.13-beta.3",
4
4
  "name": "a-js-tools",
5
5
  "description": "一点点 🤏 js 函数",
6
6
  "license": "MIT",