@yoooloo42/bean 1.0.9 → 1.0.10

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/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@yoooloo42/bean",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
- "main": "index.js",
6
5
  "scripts": {
7
6
  "test": "echo \"Error: no test specified\" && exit 1"
8
7
  },
@@ -18,7 +17,6 @@
18
17
  },
19
18
  "homepage": "https://github.com/13993193075/bean#readme",
20
19
  "exports": {
21
- ".": "./src/index.js",
22
20
  "./utils/*": "./src/utils/*.js"
23
21
  },
24
22
  "files": [
@@ -1,3 +1,35 @@
1
+ /**
2
+ * 在指定的字符集内生成一个指定长度的随机字符串。
3
+ *
4
+ * @param {number} length - 随机字符串的所需长度。
5
+ * @param {string} characters - 用于生成字符串的字符集(如 '0123456789abcdef')。
6
+ * @returns {string} - 生成的随机字符串。
7
+ * @throws {Error} - 如果长度不是正数或字符集为空。
8
+ */
9
+ function random(length, characters) {
10
+ // 1. 输入校验
11
+ if (typeof length !== 'number' || length <= 0) {
12
+ throw new Error("长度参数必须是一个大于零的数字。");
13
+ }
14
+ if (typeof characters !== 'string' || characters.length === 0) {
15
+ throw new Error("字符集参数必须是一个非空字符串。");
16
+ }
17
+
18
+ const charactersLength = characters.length;
19
+ let result = '';
20
+
21
+ // 2. 循环 'length' 次,每次随机选择一个字符
22
+ for (let i = 0; i < length; i++) {
23
+ // 随机选择字符的索引
24
+ const randomIndex = Math.floor(Math.random() * charactersLength);
25
+
26
+ // 将随机选中的字符添加到结果字符串中
27
+ result += characters.charAt(randomIndex);
28
+ }
29
+
30
+ return result;
31
+ }
32
+
1
33
  /**
2
34
  * 随机生成一个符合常见验证码规则(4-8位,数字+大小写字母)的字符串。
3
35
  * @returns {string} - 随机生成的验证码字符串。
@@ -50,6 +82,7 @@ function vercode6(length = 6) {
50
82
  }
51
83
 
52
84
  const bean = {
85
+ random,
53
86
  vercode,
54
87
  vercode6
55
88
  }
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- import utils from './src/utils/index.js';
2
-
3
- const bean = {
4
- utils
5
- }
6
- export default bean
@@ -1,14 +0,0 @@
1
- import dateFormat from './dateFormat.js';
2
- import deepClone from './deepClone.js';
3
- import random from './random.js'
4
- import regex from './regex.js'
5
- import sort from './sort.js';
6
-
7
- const bean = {
8
- dateFormat,
9
- deepClone,
10
- random,
11
- regex,
12
- sort
13
- }
14
- export default bean