ph-utils 0.4.6 → 0.4.7

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/lib/index.d.ts CHANGED
@@ -50,6 +50,13 @@ interface RandomConfig {
50
50
  * 1. 生成指定长度的随机数
51
51
  * 2. 生成介于 [min, max] 之间的随机数
52
52
  * @param opts 生成随机数的配置
53
+ *
54
+ * @example <caption>1. 生成指定长度的随机字符串</caption>
55
+ * random(1); // 长度为 1 的随机字符串
56
+ *
57
+ * @example <caption>2. 生成纯数字且不能为0长度为1的随机字符</caption>
58
+ * random({ length: 1, hasLetter: false, firstIsZero: false })
59
+ *
53
60
  * @returns
54
61
  */
55
62
  export declare function random(opts: number | RandomConfig): string | number;
package/lib/index.js CHANGED
@@ -58,6 +58,13 @@ export function isBoolean(str) {
58
58
  * 1. 生成指定长度的随机数
59
59
  * 2. 生成介于 [min, max] 之间的随机数
60
60
  * @param opts 生成随机数的配置
61
+ *
62
+ * @example <caption>1. 生成指定长度的随机字符串</caption>
63
+ * random(1); // 长度为 1 的随机字符串
64
+ *
65
+ * @example <caption>2. 生成纯数字且不能为0长度为1的随机字符</caption>
66
+ * random({ length: 1, hasLetter: false, firstIsZero: false })
67
+ *
61
68
  * @returns
62
69
  */
63
70
  export function random(opts) {
@@ -74,12 +81,11 @@ export function random(opts) {
74
81
  }
75
82
  const len = typeof opts === "object" ? opts.length : opts;
76
83
  /* 生成指定长度的随机数 */
77
- const charLens = RANDOM_CHARS.length;
78
84
  let chars = RANDOM_CHARS;
79
85
  if (typeof opts === "object" && opts.hasLetter === false) {
80
86
  chars = NUMBER_RANDOM_CHARTS;
81
87
  }
82
- const resRandom = Array.from({ length: len }, () => chars.charAt(Math.floor(Math.random() * charLens))).join("");
88
+ const resRandom = Array.from({ length: len }, () => chars.charAt(random({ min: 0, max: 9, hasEnd: true }))).join("");
83
89
  if (typeof opts === "object" &&
84
90
  opts.firstIsZero === false &&
85
91
  resRandom.indexOf("0") === 0) {
package/package.json CHANGED
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "./*": "./lib/*"
54
54
  },
55
- "version": "0.4.6",
55
+ "version": "0.4.7",
56
56
  "type": "module",
57
57
  "repository": {
58
58
  "type": "git",