@yoooloo42/bean 1.0.13 → 1.0.14

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/package.json +1 -1
  2. package/src/utils/random.js +21 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yoooloo42/bean",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -2,29 +2,29 @@
2
2
  * 在指定的字符集内生成一个指定长度的随机字符串。
3
3
  *
4
4
  * @param {number} length - 随机字符串的所需长度。
5
- * @param {string} characters - 用于生成字符串的字符集(如 '0123456789abcdef')。
5
+ * @param {string} characterSet - 用于生成字符串的字符集(如 '0123456789abcdef')。
6
6
  * @returns {string} - 生成的随机字符串。
7
7
  * @throws {Error} - 如果长度不是正数或字符集为空。
8
8
  */
9
- function random(length, characters) {
9
+ function random(length, characterSet) {
10
10
  // 1. 输入校验
11
11
  if (typeof length !== 'number' || length <= 0) {
12
12
  throw new Error("长度参数必须是一个大于零的数字。");
13
13
  }
14
- if (typeof characters !== 'string' || characters.length === 0) {
14
+ if (typeof characterSet !== 'string' || characterSet.length === 0) {
15
15
  throw new Error("字符集参数必须是一个非空字符串。");
16
16
  }
17
17
 
18
- const charactersLength = characters.length;
18
+ const characterSetLength = characterSet.length;
19
19
  let result = '';
20
20
 
21
21
  // 2. 循环 'length' 次,每次随机选择一个字符
22
22
  for (let i = 0; i < length; i++) {
23
23
  // 随机选择字符的索引
24
- const randomIndex = Math.floor(Math.random() * charactersLength);
24
+ const randomIndex = Math.floor(Math.random() * characterSetLength);
25
25
 
26
26
  // 将随机选中的字符添加到结果字符串中
27
- result += characters.charAt(randomIndex);
27
+ result += characterSet.charAt(randomIndex);
28
28
  }
29
29
 
30
30
  return result;
@@ -36,8 +36,8 @@ function random(length, characters) {
36
36
  */
37
37
  function vercode() {
38
38
  // 定义所有允许的字符集 (数字 0-9, 小写字母 a-z, 大写字母 A-Z)
39
- const characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
40
- const charactersLength = characters.length;
39
+ const characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
40
+ const characterSetLength = characterSet.length;
41
41
 
42
42
  // 随机确定验证码的长度,范围在 [4, 8] 之间
43
43
  // Math.random() * (max - min + 1) + min
@@ -47,13 +47,13 @@ function vercode() {
47
47
 
48
48
  let result = '';
49
49
 
50
- // 循环 'length' 次,每次随机从 characters 中选择一个字符
50
+ // 循环 'length' 次,每次随机从 characterSet 中选择一个字符
51
51
  for (let i = 0; i < length; i++) {
52
52
  // 随机选择字符的索引
53
- const randomIndex = Math.floor(Math.random() * charactersLength);
53
+ const randomIndex = Math.floor(Math.random() * characterSetLength);
54
54
 
55
55
  // 将随机选中的字符添加到结果字符串中
56
- result += characters.charAt(randomIndex);
56
+ result += characterSet.charAt(randomIndex);
57
57
  }
58
58
 
59
59
  return result;
@@ -65,17 +65,17 @@ function vercode() {
65
65
  */
66
66
  function vercode6(length = 6) {
67
67
  // 定义所有允许的字符集 (数字 0-9, 小写字母 a-z, 大写字母 A-Z)
68
- const characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
69
- const charactersLength = characters.length;
68
+ const characterSet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
69
+ const characterSetLength = characterSet.length;
70
70
 
71
71
  let result = '';
72
- // 循环 6 次,每次随机从 characters 中选择一个字符
72
+ // 循环 6 次,每次随机从 characterSet 中选择一个字符
73
73
  for (let i = 0; i < length; i++) {
74
74
  // 随机选择字符的索引
75
- const randomIndex = Math.floor(Math.random() * charactersLength);
75
+ const randomIndex = Math.floor(Math.random() * characterSetLength);
76
76
 
77
77
  // 将随机选中的字符添加到结果字符串中
78
- result += characters.charAt(randomIndex);
78
+ result += characterSet.charAt(randomIndex);
79
79
  }
80
80
 
81
81
  return result;
@@ -87,17 +87,17 @@ function vercode6(length = 6) {
87
87
  */
88
88
  function vercode6N(length = 6) {
89
89
  // 定义所有允许的字符集 (数字 0-9, 小写字母 a-z, 大写字母 A-Z)
90
- const characters = '0123456789';
91
- const charactersLength = characters.length;
90
+ const characterSet = '0123456789';
91
+ const characterSetLength = characterSet.length;
92
92
 
93
93
  let result = '';
94
- // 循环 6 次,每次随机从 characters 中选择一个字符
94
+ // 循环 6 次,每次随机从 characterSet 中选择一个字符
95
95
  for (let i = 0; i < length; i++) {
96
96
  // 随机选择字符的索引
97
- const randomIndex = Math.floor(Math.random() * charactersLength);
97
+ const randomIndex = Math.floor(Math.random() * characterSetLength);
98
98
 
99
99
  // 将随机选中的字符添加到结果字符串中
100
- result += characters.charAt(randomIndex);
100
+ result += characterSet.charAt(randomIndex);
101
101
  }
102
102
 
103
103
  return result;