util-helpers 4.7.2 → 4.8.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 CHANGED
@@ -81,6 +81,7 @@ formatMoney('1000'); // => 1,000.00
81
81
  - [validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword) - 验证密码
82
82
  - 其他
83
83
  - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
84
+ - [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
84
85
  - [waitTime](https://doly-dev.github.io/util-helpers/module-Other.html#.waitTime) - 等待时间返回 Promise
85
86
 
86
87
  ## 精选第三方工具库
@@ -357,9 +357,6 @@
357
357
  // 基础字符组成
358
358
  const baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
359
359
 
360
- //加权因子
361
- const weightFactor = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
362
-
363
360
  /**
364
361
  * 获取字符位置
365
362
  *
@@ -400,7 +397,7 @@
400
397
  // 字符位置对应的基础编码序号
401
398
  const index = getBaseCodeIndex(preCode[i]);
402
399
  // 加权因子
403
- const wf = weightFactor[i];
400
+ const wf = Math.pow(3, i) % 31;
404
401
  // 计算序号和加权因子的乘积,并计算级数之和
405
402
  total += index * wf;
406
403
  }
@@ -2599,6 +2596,49 @@
2599
2596
  return pos;
2600
2597
  }
2601
2598
 
2599
+ const numberChars = '0123456789';
2600
+ const letterChars = 'abcdefghijklmnopqrstuvwxyz';
2601
+ const defaultChars = numberChars + letterChars + letterChars.toUpperCase();
2602
+
2603
+ /**
2604
+ * @private
2605
+ * @param {number} [len=0] 长度
2606
+ * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
2607
+ * @param {string} [prefix=''] 前缀部分,不计入长度
2608
+ * @returns {string}
2609
+ */
2610
+ function internalRandomString(len = 0, optionalChars = defaultChars, prefix = '') {
2611
+ while (len-- > 0) {
2612
+ const r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
2613
+ return internalRandomString(len, optionalChars, prefix + r);
2614
+ }
2615
+ return prefix;
2616
+ }
2617
+
2618
+ /**
2619
+ * 生成随机字符串
2620
+ *
2621
+ * @static
2622
+ * @alias module:Other.randomString
2623
+ * @since 4.8.0
2624
+ * @param {number} [len=0] 长度
2625
+ * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
2626
+ * @returns {string} 随机字符串
2627
+ * @example
2628
+ * randomString(5); // slk23
2629
+ * randomString(8); // 71mHqo2A
2630
+ *
2631
+ * // 自定义允许的字符
2632
+ * randomString(5, 'abc'); // ccbcb
2633
+ * randomString(8, 'abcefg'); // bcgcfabg
2634
+ */
2635
+ function randomString(len = 0, optionalChars) {
2636
+
2637
+ const realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
2638
+
2639
+ return internalRandomString(len, realChars);
2640
+ }
2641
+
2602
2642
  exports.blobToDataURL = blobToDataURL;
2603
2643
  exports.bytesToSize = bytesToSize;
2604
2644
  exports.calculateCursorPosition = calculateCursorPosition;
@@ -2633,6 +2673,7 @@
2633
2673
  exports.padZero = padZero;
2634
2674
  exports.parseIdCard = parseIdCard;
2635
2675
  exports.plus = plus;
2676
+ exports.randomString = randomString;
2636
2677
  exports.replaceChar = replaceChar;
2637
2678
  exports.round = round;
2638
2679
  exports.safeDate = safeDate;