@qy_better_lib/core 0.1.2 → 0.1.4
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/DOCUMENTATION.md +2945 -0
- package/dist/@qy_better_lib/core.min.js +1 -1
- package/lib/directives/{click-outside.d.ts → click_outside.d.ts} +3 -0
- package/lib/directives/click_outside.js +92 -0
- package/lib/directives/index.d.ts +7 -1
- package/lib/directives/index.js +8 -0
- package/lib/index.js +209 -55
- package/lib/types/auto-imports.d.ts +76 -0
- package/lib/types/components.d.ts +13 -0
- package/lib/utils/color.d.ts +68 -5
- package/lib/utils/color.js +77 -10
- package/lib/utils/date.d.ts +71 -7
- package/lib/utils/date.js +102 -0
- package/lib/utils/dom.d.ts +176 -5
- package/lib/utils/dom.js +196 -12
- package/lib/utils/echarts.d.ts +132 -4
- package/lib/utils/echarts.js +209 -12
- package/lib/utils/file.d.ts +79 -3
- package/lib/utils/file.js +137 -14
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/is.d.ts +109 -19
- package/lib/utils/is.js +115 -32
- package/lib/utils/number.d.ts +76 -6
- package/lib/utils/number.js +94 -10
- package/lib/utils/object.d.ts +76 -6
- package/lib/utils/object.js +238 -64
- package/lib/utils/random.d.ts +87 -8
- package/lib/utils/random.js +101 -11
- package/lib/utils/share.d.ts +75 -10
- package/lib/utils/share.js +158 -13
- package/lib/utils/storage.d.ts +103 -14
- package/lib/utils/storage.js +169 -31
- package/lib/utils/tree.d.ts +176 -4
- package/lib/utils/tree.js +450 -10
- package/package.json +10 -3
- package/types/index.d.ts +39 -1
- package/vitest.config.ts +15 -0
- package/lib/directives/click-outside.js +0 -39
package/lib/utils/object.js
CHANGED
|
@@ -1,73 +1,247 @@
|
|
|
1
|
-
import {
|
|
2
|
-
function
|
|
3
|
-
const
|
|
4
|
-
if (!
|
|
5
|
-
return
|
|
6
|
-
if (e.nodeType && "cloneNode" in e)
|
|
7
|
-
return e.cloneNode(!0);
|
|
8
|
-
if (t.call(e) === "[object Date]")
|
|
9
|
-
return new Date(e.getTime());
|
|
10
|
-
if (t.call(e) === "[object RegExp]") {
|
|
11
|
-
const r = [];
|
|
12
|
-
return e.global && r.push("g"), e.multiline && r.push("m"), e.ignoreCase && r.push("i"), new RegExp(e.source, r.join(""));
|
|
1
|
+
import { is_object } from "./is.js";
|
|
2
|
+
function deep_clone(obj) {
|
|
3
|
+
const _toString = Object.prototype.toString;
|
|
4
|
+
if (!obj || typeof obj !== "object") {
|
|
5
|
+
return obj;
|
|
13
6
|
}
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
-
for (const [l, o] of e.entries())
|
|
17
|
-
r.append(l, o);
|
|
18
|
-
return r;
|
|
7
|
+
if (obj.nodeType && "cloneNode" in obj) {
|
|
8
|
+
return obj.cloneNode(true);
|
|
19
9
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
if (_toString.call(obj) === "[object Date]") {
|
|
11
|
+
return new Date(obj.getTime());
|
|
12
|
+
}
|
|
13
|
+
if (_toString.call(obj) === "[object RegExp]") {
|
|
14
|
+
const flags = [];
|
|
15
|
+
if (obj.global) {
|
|
16
|
+
flags.push("g");
|
|
17
|
+
}
|
|
18
|
+
if (obj.multiline) {
|
|
19
|
+
flags.push("m");
|
|
20
|
+
}
|
|
21
|
+
if (obj.ignoreCase) {
|
|
22
|
+
flags.push("i");
|
|
23
|
+
}
|
|
24
|
+
return new RegExp(obj.source, flags.join(""));
|
|
25
|
+
}
|
|
26
|
+
if (_toString.call(obj) === "[object FormData]") {
|
|
27
|
+
const form_data = new FormData();
|
|
28
|
+
for (const [key, value] of obj.entries()) {
|
|
29
|
+
form_data.append(key, value);
|
|
30
|
+
}
|
|
31
|
+
return form_data;
|
|
32
|
+
}
|
|
33
|
+
if (_toString.call(obj) === "[object Map]") {
|
|
34
|
+
const map = /* @__PURE__ */ new Map();
|
|
35
|
+
for (const [key, value] of obj.entries()) {
|
|
36
|
+
map.set(key, deep_clone(value));
|
|
37
|
+
}
|
|
38
|
+
return map;
|
|
39
|
+
}
|
|
40
|
+
if (_toString.call(obj) === "[object Set]") {
|
|
41
|
+
const set = /* @__PURE__ */ new Set();
|
|
42
|
+
for (const value of obj.values()) {
|
|
43
|
+
set.add(deep_clone(value));
|
|
44
|
+
}
|
|
45
|
+
return set;
|
|
46
|
+
}
|
|
47
|
+
const result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {};
|
|
48
|
+
for (const key in obj) {
|
|
49
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
50
|
+
result[key] = deep_clone(obj[key]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
24
54
|
}
|
|
25
|
-
function
|
|
26
|
-
let
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
let l = e[r];
|
|
30
|
-
if (c(l))
|
|
31
|
-
for (let o in l)
|
|
32
|
-
o === "__proto__" || n === l[o] || (c(l[o]) ? n[o] = s(n[o], l[o]) : n[o] = l[o]);
|
|
55
|
+
function deep_assign(...args) {
|
|
56
|
+
let len = args.length, target = args[0];
|
|
57
|
+
if (!is_object(target)) {
|
|
58
|
+
target = {};
|
|
33
59
|
}
|
|
34
|
-
|
|
60
|
+
for (let i = 1; i < len; i++) {
|
|
61
|
+
let source = args[i];
|
|
62
|
+
if (is_object(source)) {
|
|
63
|
+
for (let s in source) {
|
|
64
|
+
if (s === "__proto__" || target === source[s]) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (is_object(source[s])) {
|
|
68
|
+
if (!is_object(target[s])) {
|
|
69
|
+
target[s] = Array.isArray(source[s]) ? [] : {};
|
|
70
|
+
}
|
|
71
|
+
target[s] = deep_assign(target[s], source[s]);
|
|
72
|
+
} else {
|
|
73
|
+
target[s] = source[s];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return target;
|
|
35
79
|
}
|
|
36
|
-
function
|
|
37
|
-
if (
|
|
38
|
-
if (
|
|
39
|
-
if (
|
|
40
|
-
return
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
function deep_equal(a, b) {
|
|
81
|
+
if (a === b) return true;
|
|
82
|
+
if (a && b && typeof a == "object" && typeof b == "object") {
|
|
83
|
+
if (a.constructor !== b.constructor) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
let length, i, keys;
|
|
87
|
+
if (Array.isArray(a)) {
|
|
88
|
+
length = a.length;
|
|
89
|
+
if (length != b.length) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
for (i = length; i-- !== 0; ) {
|
|
93
|
+
if (!deep_equal(a[i], b[i])) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
|
|
100
|
+
if (a.valueOf !== Object.prototype.valueOf) {
|
|
101
|
+
return a.valueOf() === b.valueOf();
|
|
102
|
+
}
|
|
103
|
+
if (a.toString !== Object.prototype.toString) {
|
|
104
|
+
return a.toString() === b.toString();
|
|
105
|
+
}
|
|
106
|
+
keys = Object.keys(a);
|
|
107
|
+
length = keys.length;
|
|
108
|
+
if (length !== Object.keys(b).length) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
for (i = length; i-- !== 0; ) {
|
|
112
|
+
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (i = length; i-- !== 0; ) {
|
|
117
|
+
const key = keys[i];
|
|
118
|
+
if (!deep_equal(a[key], b[key])) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
return a !== a && b !== b;
|
|
125
|
+
}
|
|
126
|
+
function is_empty_object(obj) {
|
|
127
|
+
return is_object(obj) && Object.keys(obj).length === 0;
|
|
128
|
+
}
|
|
129
|
+
function pick(obj, keys) {
|
|
130
|
+
if (!is_object(obj)) return {};
|
|
131
|
+
const result = {};
|
|
132
|
+
keys.forEach((key) => {
|
|
133
|
+
if (key in obj) {
|
|
134
|
+
result[key] = obj[key];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
function omit(obj, keys) {
|
|
140
|
+
if (!is_object(obj)) return {};
|
|
141
|
+
const result = {};
|
|
142
|
+
Object.keys(obj).forEach((key) => {
|
|
143
|
+
if (!keys.includes(key)) {
|
|
144
|
+
result[key] = obj[key];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
function flatten_object(obj, prefix = "") {
|
|
150
|
+
if (!is_object(obj)) return {};
|
|
151
|
+
const result = {};
|
|
152
|
+
Object.keys(obj).forEach((key) => {
|
|
153
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
154
|
+
if (is_object(obj[key]) && !Array.isArray(obj[key])) {
|
|
155
|
+
Object.assign(result, flatten_object(obj[key], newKey));
|
|
156
|
+
} else {
|
|
157
|
+
result[newKey] = obj[key];
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
function unflatten_object(obj) {
|
|
163
|
+
if (!is_object(obj)) return {};
|
|
164
|
+
const result = {};
|
|
165
|
+
Object.keys(obj).forEach((key) => {
|
|
166
|
+
const parts = key.split(".");
|
|
167
|
+
let current = result;
|
|
168
|
+
parts.forEach((part, index) => {
|
|
169
|
+
if (index === parts.length - 1) {
|
|
170
|
+
current[part] = obj[key];
|
|
171
|
+
} else {
|
|
172
|
+
if (!current[part]) {
|
|
173
|
+
current[part] = {};
|
|
174
|
+
}
|
|
175
|
+
current = current[part];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
function get_object_value(obj, path, defaultValue = void 0) {
|
|
182
|
+
if (!is_object(obj)) return defaultValue;
|
|
183
|
+
const parts = path.split(".");
|
|
184
|
+
let current = obj;
|
|
185
|
+
for (const part of parts) {
|
|
186
|
+
if (current === void 0 || current === null) {
|
|
187
|
+
return defaultValue;
|
|
188
|
+
}
|
|
189
|
+
current = current[part];
|
|
190
|
+
}
|
|
191
|
+
return current !== void 0 ? current : defaultValue;
|
|
192
|
+
}
|
|
193
|
+
function set_object_value(obj, path, value) {
|
|
194
|
+
if (!is_object(obj)) obj = {};
|
|
195
|
+
const parts = path.split(".");
|
|
196
|
+
let current = obj;
|
|
197
|
+
parts.forEach((part, index) => {
|
|
198
|
+
if (index === parts.length - 1) {
|
|
199
|
+
current[part] = value;
|
|
200
|
+
} else {
|
|
201
|
+
if (!is_object(current[part])) {
|
|
202
|
+
current[part] = {};
|
|
203
|
+
}
|
|
204
|
+
current = current[part];
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
return obj;
|
|
208
|
+
}
|
|
209
|
+
function merge_objects(...args) {
|
|
210
|
+
return Object.assign({}, ...args);
|
|
211
|
+
}
|
|
212
|
+
function diff_objects(obj1, obj2) {
|
|
213
|
+
const result = {};
|
|
214
|
+
if (!is_object(obj1) && !is_object(obj2)) {
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
if (!is_object(obj1)) {
|
|
218
|
+
return obj2;
|
|
66
219
|
}
|
|
67
|
-
|
|
220
|
+
if (!is_object(obj2)) {
|
|
221
|
+
return obj1;
|
|
222
|
+
}
|
|
223
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(obj1), ...Object.keys(obj2)]);
|
|
224
|
+
allKeys.forEach((key) => {
|
|
225
|
+
if (!deep_equal(obj1[key], obj2[key])) {
|
|
226
|
+
result[key] = {
|
|
227
|
+
oldValue: obj1[key],
|
|
228
|
+
newValue: obj2[key]
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
return result;
|
|
68
233
|
}
|
|
69
234
|
export {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
235
|
+
deep_assign,
|
|
236
|
+
deep_clone,
|
|
237
|
+
deep_equal,
|
|
238
|
+
diff_objects,
|
|
239
|
+
flatten_object,
|
|
240
|
+
get_object_value,
|
|
241
|
+
is_empty_object,
|
|
242
|
+
merge_objects,
|
|
243
|
+
omit,
|
|
244
|
+
pick,
|
|
245
|
+
set_object_value,
|
|
246
|
+
unflatten_object
|
|
73
247
|
};
|
package/lib/utils/random.d.ts
CHANGED
|
@@ -7,19 +7,98 @@
|
|
|
7
7
|
* 生成[0-10000]的随机数,针对数据小量使用
|
|
8
8
|
* @returns 随机数
|
|
9
9
|
*/
|
|
10
|
-
export declare function
|
|
10
|
+
export declare function generate_id(): number;
|
|
11
11
|
/**
|
|
12
12
|
* 生成[0-max]的随机数
|
|
13
|
+
* @param max 最大值
|
|
13
14
|
* @returns 随机数
|
|
14
15
|
*/
|
|
15
|
-
export declare function
|
|
16
|
+
export declare function get_random_int(max: number): number;
|
|
16
17
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
18
|
+
* 生成指定范围的随机整数
|
|
19
|
+
* @param min 最小值
|
|
20
|
+
* @param max 最大值
|
|
21
|
+
* @returns 随机整数
|
|
19
22
|
*/
|
|
20
|
-
export declare function
|
|
23
|
+
export declare function get_random_int_range(min: number, max: number): number;
|
|
21
24
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @
|
|
25
|
+
* 生成随机浮点数
|
|
26
|
+
* @param min 最小值
|
|
27
|
+
* @param max 最大值
|
|
28
|
+
* @param decimalPlaces 小数位数
|
|
29
|
+
* @returns 随机浮点数
|
|
24
30
|
*/
|
|
25
|
-
export declare function
|
|
31
|
+
export declare function get_random_float(min: number, max: number, decimalPlaces?: number): number;
|
|
32
|
+
/**
|
|
33
|
+
* 生成GUID/UUID
|
|
34
|
+
* @returns GUID/UUID字符串
|
|
35
|
+
*/
|
|
36
|
+
export declare function generate_guid(): string;
|
|
37
|
+
/**
|
|
38
|
+
* 生成随机字符串
|
|
39
|
+
* @returns 随机字符串
|
|
40
|
+
*/
|
|
41
|
+
export declare function generate_string(): string;
|
|
42
|
+
/**
|
|
43
|
+
* 生成指定长度的随机字符串
|
|
44
|
+
* @param length 字符串长度
|
|
45
|
+
* @param chars 可选字符集
|
|
46
|
+
* @returns 随机字符串
|
|
47
|
+
*/
|
|
48
|
+
export declare function generate_random_string(length: number, chars?: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* 生成随机布尔值
|
|
51
|
+
* @returns 随机布尔值
|
|
52
|
+
*/
|
|
53
|
+
export declare function get_random_boolean(): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 从数组中随机选择一个元素
|
|
56
|
+
* @param array 源数组
|
|
57
|
+
* @returns 随机选中的元素
|
|
58
|
+
*/
|
|
59
|
+
export declare function get_random_element<T>(array: T[]): T | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* 从数组中随机选择多个元素
|
|
62
|
+
* @param array 源数组
|
|
63
|
+
* @param count 选择数量
|
|
64
|
+
* @returns 随机选中的元素数组
|
|
65
|
+
*/
|
|
66
|
+
export declare function get_random_elements<T>(array: T[], count: number): T[];
|
|
67
|
+
/**
|
|
68
|
+
* 生成随机颜色
|
|
69
|
+
* @returns 随机颜色的十六进制值
|
|
70
|
+
*/
|
|
71
|
+
export declare function generate_random_color(): string;
|
|
72
|
+
/**
|
|
73
|
+
* 生成指定长度的随机数字字符串
|
|
74
|
+
* @param length 字符串长度
|
|
75
|
+
* @returns 随机数字字符串
|
|
76
|
+
*/
|
|
77
|
+
export declare function generate_random_number_string(length: number): string;
|
|
78
|
+
/**
|
|
79
|
+
* 生成随机密码
|
|
80
|
+
* @param length 密码长度,默认8
|
|
81
|
+
* @returns 随机密码
|
|
82
|
+
*/
|
|
83
|
+
export declare function generate_random_password(length?: number): string;
|
|
84
|
+
/**
|
|
85
|
+
* 生成随机手机号
|
|
86
|
+
* @returns 随机手机号
|
|
87
|
+
*/
|
|
88
|
+
export declare function generate_random_phone(): string;
|
|
89
|
+
/**
|
|
90
|
+
* 生成随机邮箱
|
|
91
|
+
* @returns 随机邮箱
|
|
92
|
+
*/
|
|
93
|
+
export declare function generate_random_email(): string;
|
|
94
|
+
/**
|
|
95
|
+
* 生成随机URL
|
|
96
|
+
* @returns 随机URL
|
|
97
|
+
*/
|
|
98
|
+
export declare function generate_random_url(): string;
|
|
99
|
+
/**
|
|
100
|
+
* 打乱数组顺序
|
|
101
|
+
* @param array 源数组
|
|
102
|
+
* @returns 打乱后的新数组
|
|
103
|
+
*/
|
|
104
|
+
export declare function shuffle_array<T>(array: T[]): T[];
|
package/lib/utils/random.js
CHANGED
|
@@ -1,19 +1,109 @@
|
|
|
1
|
-
function
|
|
1
|
+
function generate_id() {
|
|
2
2
|
return Math.floor(Math.random() * 1e4);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
|
|
4
|
+
function get_random_int(max) {
|
|
5
|
+
if (max < 0) return 0;
|
|
6
|
+
return Math.floor(Math.random() * Math.floor(max));
|
|
6
7
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
return
|
|
8
|
+
function get_random_int_range(min, max) {
|
|
9
|
+
if (min > max) [min, max] = [max, min];
|
|
10
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
10
11
|
}
|
|
11
|
-
function
|
|
12
|
+
function get_random_float(min, max, decimalPlaces = 2) {
|
|
13
|
+
if (min > max) [min, max] = [max, min];
|
|
14
|
+
const random = Math.random() * (max - min) + min;
|
|
15
|
+
return parseFloat(random.toFixed(decimalPlaces));
|
|
16
|
+
}
|
|
17
|
+
function generate_guid() {
|
|
18
|
+
const gs = generate_string;
|
|
19
|
+
return `${gs()}${gs()}-${gs()}-${gs()}-${gs()}-${gs()}${gs()}${gs()}`;
|
|
20
|
+
}
|
|
21
|
+
function generate_string() {
|
|
12
22
|
return Math.floor((1 + Math.random()) * 65536).toString(16).substring(1);
|
|
13
23
|
}
|
|
24
|
+
function generate_random_string(length, chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
|
|
25
|
+
let result = "";
|
|
26
|
+
const charsLength = chars.length;
|
|
27
|
+
for (let i = 0; i < length; i++) {
|
|
28
|
+
result += chars.charAt(Math.floor(Math.random() * charsLength));
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
function get_random_boolean() {
|
|
33
|
+
return Math.random() > 0.5;
|
|
34
|
+
}
|
|
35
|
+
function get_random_element(array) {
|
|
36
|
+
if (!array || array.length === 0) return void 0;
|
|
37
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
38
|
+
}
|
|
39
|
+
function get_random_elements(array, count) {
|
|
40
|
+
if (!array || array.length === 0 || count <= 0) return [];
|
|
41
|
+
if (count >= array.length) return [...array];
|
|
42
|
+
const shuffled = [...array].sort(() => 0.5 - Math.random());
|
|
43
|
+
return shuffled.slice(0, count);
|
|
44
|
+
}
|
|
45
|
+
function generate_random_color() {
|
|
46
|
+
const letters = "0123456789ABCDEF";
|
|
47
|
+
let color = "#";
|
|
48
|
+
for (let i = 0; i < 6; i++) {
|
|
49
|
+
color += letters[Math.floor(Math.random() * 16)];
|
|
50
|
+
}
|
|
51
|
+
return color;
|
|
52
|
+
}
|
|
53
|
+
function generate_random_number_string(length) {
|
|
54
|
+
return generate_random_string(length, "0123456789");
|
|
55
|
+
}
|
|
56
|
+
function generate_random_password(length = 8) {
|
|
57
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+~`|}{[]:;?><,./-=";
|
|
58
|
+
return generate_random_string(length, chars);
|
|
59
|
+
}
|
|
60
|
+
function generate_random_phone() {
|
|
61
|
+
const prefixes = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "150", "151", "152", "153", "155", "156", "157", "158", "159", "170", "171", "172", "173", "175", "176", "177", "178", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189"];
|
|
62
|
+
const prefix = get_random_element(prefixes);
|
|
63
|
+
const suffix = generate_random_number_string(8);
|
|
64
|
+
return `${prefix}${suffix}`;
|
|
65
|
+
}
|
|
66
|
+
function generate_random_email() {
|
|
67
|
+
const domains = ["gmail.com", "yahoo.com", "hotmail.com", "outlook.com", "163.com", "126.com", "qq.com", "sina.com"];
|
|
68
|
+
const username = generate_random_string(8);
|
|
69
|
+
const domain = get_random_element(domains);
|
|
70
|
+
return `${username}@${domain}`;
|
|
71
|
+
}
|
|
72
|
+
function generate_random_url() {
|
|
73
|
+
const protocols = ["http", "https"];
|
|
74
|
+
const domains = ["example.com", "test.com", "demo.com", "sample.com"];
|
|
75
|
+
const paths = ["api", "docs", "about", "contact", "products"];
|
|
76
|
+
const protocol = get_random_element(protocols);
|
|
77
|
+
const domain = get_random_element(domains);
|
|
78
|
+
const path = get_random_element(paths);
|
|
79
|
+
const query = generate_random_string(4);
|
|
80
|
+
return `${protocol}://www.${domain}/${path}?id=${query}`;
|
|
81
|
+
}
|
|
82
|
+
function shuffle_array(array) {
|
|
83
|
+
if (!array) return [];
|
|
84
|
+
const shuffled = [...array];
|
|
85
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
86
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
87
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
88
|
+
}
|
|
89
|
+
return shuffled;
|
|
90
|
+
}
|
|
14
91
|
export {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
92
|
+
generate_guid,
|
|
93
|
+
generate_id,
|
|
94
|
+
generate_random_color,
|
|
95
|
+
generate_random_email,
|
|
96
|
+
generate_random_number_string,
|
|
97
|
+
generate_random_password,
|
|
98
|
+
generate_random_phone,
|
|
99
|
+
generate_random_string,
|
|
100
|
+
generate_random_url,
|
|
101
|
+
generate_string,
|
|
102
|
+
get_random_boolean,
|
|
103
|
+
get_random_element,
|
|
104
|
+
get_random_elements,
|
|
105
|
+
get_random_float,
|
|
106
|
+
get_random_int,
|
|
107
|
+
get_random_int_range,
|
|
108
|
+
shuffle_array
|
|
19
109
|
};
|
package/lib/utils/share.d.ts
CHANGED
|
@@ -1,16 +1,81 @@
|
|
|
1
1
|
type Fn = (...args: any[]) => any;
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param fn
|
|
5
|
-
* @param delay
|
|
6
|
-
* @
|
|
3
|
+
* 防抖函数
|
|
4
|
+
* @param fn 要执行的函数
|
|
5
|
+
* @param delay 延迟时间,默认200ms
|
|
6
|
+
* @param immediate 是否立即执行,默认false
|
|
7
|
+
* @returns 防抖处理后的函数
|
|
7
8
|
*/
|
|
8
|
-
export declare function debounce(fn:
|
|
9
|
+
export declare function debounce<T extends (...args: any[]) => any>(fn: T, delay?: number, immediate?: boolean): (this: any, ...args: Parameters<T>) => ReturnType<T>;
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param fn
|
|
12
|
-
* @param interval
|
|
13
|
-
* @
|
|
11
|
+
* 节流函数
|
|
12
|
+
* @param fn 要执行的函数
|
|
13
|
+
* @param interval 时间间隔,默认200ms
|
|
14
|
+
* @param options 配置选项
|
|
15
|
+
* @returns 节流处理后的函数
|
|
14
16
|
*/
|
|
15
|
-
export declare function throttle
|
|
17
|
+
export declare function throttle<T extends (...args: any[]) => any>(fn: T, interval?: number, options?: {
|
|
18
|
+
leading?: boolean;
|
|
19
|
+
trailing?: boolean;
|
|
20
|
+
}): (this: any, ...args: Parameters<T>) => ReturnType<T>;
|
|
21
|
+
/**
|
|
22
|
+
* 柯里化函数
|
|
23
|
+
* @param fn 要柯里化的函数
|
|
24
|
+
* @returns 柯里化后的函数
|
|
25
|
+
*/
|
|
26
|
+
export declare function curry<T extends (...args: any[]) => any>(fn: T): (...args: any[]) => any;
|
|
27
|
+
/**
|
|
28
|
+
* 函数组合
|
|
29
|
+
* @param fns 要组合的函数列表
|
|
30
|
+
* @returns 组合后的函数
|
|
31
|
+
*/
|
|
32
|
+
export declare function compose(...fns: Fn[]): (initialValue: any) => any;
|
|
33
|
+
/**
|
|
34
|
+
* 管道函数
|
|
35
|
+
* @param fns 要管道的函数列表
|
|
36
|
+
* @returns 管道后的函数
|
|
37
|
+
*/
|
|
38
|
+
export declare function pipe(...fns: Fn[]): (initialValue: any) => any;
|
|
39
|
+
/**
|
|
40
|
+
* 延迟执行函数
|
|
41
|
+
* @param fn 要延迟执行的函数
|
|
42
|
+
* @param delay 延迟时间,默认1000ms
|
|
43
|
+
* @returns Promise
|
|
44
|
+
*/
|
|
45
|
+
export declare function delay_execution(fn: Fn, delay?: number): Promise<any>;
|
|
46
|
+
/**
|
|
47
|
+
* 重试函数
|
|
48
|
+
* @param fn 要执行的函数
|
|
49
|
+
* @param maxRetries 最大重试次数,默认3
|
|
50
|
+
* @param delay 重试间隔,默认1000ms
|
|
51
|
+
* @returns Promise
|
|
52
|
+
*/
|
|
53
|
+
export declare function retry(fn: Fn, maxRetries?: number, delay?: number): Promise<any>;
|
|
54
|
+
/**
|
|
55
|
+
* 一次性函数
|
|
56
|
+
* @param fn 要执行的函数
|
|
57
|
+
* @returns 只能执行一次的函数
|
|
58
|
+
*/
|
|
59
|
+
export declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
|
|
60
|
+
/**
|
|
61
|
+
* 记忆化函数
|
|
62
|
+
* @param fn 要记忆化的函数
|
|
63
|
+
* @returns 记忆化后的函数
|
|
64
|
+
*/
|
|
65
|
+
export declare function memoize<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
|
|
66
|
+
/**
|
|
67
|
+
* 批量执行函数
|
|
68
|
+
* @param fns 要执行的函数列表
|
|
69
|
+
* @param parallel 是否并行执行,默认true
|
|
70
|
+
* @returns Promise数组
|
|
71
|
+
*/
|
|
72
|
+
export declare function batch_execution(fns: Array<() => Promise<any>>, parallel?: boolean): Promise<any[]>;
|
|
73
|
+
/**
|
|
74
|
+
* 超时控制函数
|
|
75
|
+
* @param fn 要执行的函数
|
|
76
|
+
* @param timeout 超时时间,默认5000ms
|
|
77
|
+
* @param timeoutMessage 超时消息
|
|
78
|
+
* @returns Promise
|
|
79
|
+
*/
|
|
80
|
+
export declare function timeout(fn: Fn, timeout?: number, timeoutMessage?: string): Promise<any>;
|
|
16
81
|
export {};
|