mm_expand 2.3.3 → 2.3.5
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/array.js +36 -36
- package/lib/base.js +15 -15
- package/lib/date.js +9 -8
- package/lib/errors.js +99 -99
- package/lib/event.js +45 -45
- package/lib/eventer.js +214 -166
- package/lib/file.js +126 -14
- package/lib/global.js +51 -53
- package/lib/lang.js +10 -10
- package/lib/logger.js +2 -2
- package/lib/number.js +10 -10
- package/lib/object.js +72 -75
- package/lib/req.js +42 -42
- package/lib/ret.js +248 -249
- package/lib/string.js +87 -93
- package/lib/timer.js +15 -15
- package/lib/validator.js +11 -11
- package/package.json +6 -6
package/lib/string.js
CHANGED
|
@@ -1,29 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 字符串工具类,用于扩展String原型
|
|
3
3
|
*/
|
|
4
|
-
const {
|
|
5
|
-
XMLParser
|
|
6
|
-
} = require('fast-xml-parser');
|
|
7
|
-
|
|
8
4
|
const json5 = require('json5');
|
|
9
5
|
const pinyin = require('pinyinlite');
|
|
10
|
-
const {
|
|
11
|
-
createHash,
|
|
12
|
-
createCipheriv,
|
|
13
|
-
createDecipheriv
|
|
14
|
-
} = require('crypto');
|
|
6
|
+
const { createHash, createCipheriv, create_deci: create_deci } = require('crypto');
|
|
15
7
|
|
|
16
8
|
/**
|
|
17
|
-
*
|
|
9
|
+
* form-data转对象
|
|
18
10
|
* @returns {object} 转换后的对象
|
|
19
11
|
*/
|
|
20
12
|
String.prototype.toQuery = function () {
|
|
21
13
|
const result = {};
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
let str = this + '';
|
|
15
|
+
let parts = str.split('?');
|
|
24
16
|
|
|
25
17
|
if (parts.length > 1) {
|
|
26
|
-
|
|
18
|
+
let params = parts[1].split('&');
|
|
27
19
|
for (let i = 0; i < params.length; i++) {
|
|
28
20
|
const [key, value] = params[i].split('=');
|
|
29
21
|
result[key] = value;
|
|
@@ -34,17 +26,17 @@ String.prototype.toQuery = function () {
|
|
|
34
26
|
};
|
|
35
27
|
|
|
36
28
|
/**
|
|
37
|
-
*
|
|
29
|
+
* MD5加密
|
|
38
30
|
* @returns {string} 加密后的字符串
|
|
39
31
|
*/
|
|
40
32
|
String.prototype.md5 = function () {
|
|
41
|
-
|
|
33
|
+
let hash = createHash('md5');
|
|
42
34
|
hash.update(this + '');
|
|
43
35
|
return hash.digest('hex');
|
|
44
36
|
};
|
|
45
37
|
|
|
46
38
|
/**
|
|
47
|
-
*
|
|
39
|
+
* aes加密
|
|
48
40
|
* @param {string} key - 必须为32位私钥
|
|
49
41
|
* @param {string} iv - 初始向量
|
|
50
42
|
* @returns {string} 加密后的字符串
|
|
@@ -54,12 +46,12 @@ String.prototype.aesEncode = function (key, iv) {
|
|
|
54
46
|
throw new Error('密钥必须为32位');
|
|
55
47
|
}
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
|
|
49
|
+
var _iv = iv || '';
|
|
50
|
+
let encoding = 'base64';
|
|
59
51
|
const chunks = [];
|
|
60
52
|
|
|
61
53
|
try {
|
|
62
|
-
|
|
54
|
+
let cipher = createCipheriv('aes-256-ecb', key, _iv);
|
|
63
55
|
cipher.setAutoPadding(true);
|
|
64
56
|
chunks.push(cipher.update(this + '', 'utf8', encoding));
|
|
65
57
|
chunks.push(cipher.final(encoding));
|
|
@@ -70,7 +62,7 @@ String.prototype.aesEncode = function (key, iv) {
|
|
|
70
62
|
};
|
|
71
63
|
|
|
72
64
|
/**
|
|
73
|
-
*
|
|
65
|
+
* aes解密
|
|
74
66
|
* @param {string} key - 必须为32位私钥
|
|
75
67
|
* @param {string} iv - 初始向量
|
|
76
68
|
* @returns {string} 解密后的字符串
|
|
@@ -80,12 +72,12 @@ String.prototype.aesDecode = function (key, iv) {
|
|
|
80
72
|
throw new Error('密钥必须为32位');
|
|
81
73
|
}
|
|
82
74
|
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
var _iv = iv || '';
|
|
76
|
+
let encoding = 'utf8';
|
|
85
77
|
const chunks = [];
|
|
86
78
|
|
|
87
79
|
try {
|
|
88
|
-
|
|
80
|
+
let decipher = create_deci('aes-256-ecb', key, _iv);
|
|
89
81
|
decipher.setAutoPadding(true);
|
|
90
82
|
chunks.push(decipher.update(this + '', 'base64', encoding));
|
|
91
83
|
chunks.push(decipher.final(encoding));
|
|
@@ -96,25 +88,25 @@ String.prototype.aesDecode = function (key, iv) {
|
|
|
96
88
|
};
|
|
97
89
|
|
|
98
90
|
/**
|
|
99
|
-
*
|
|
91
|
+
* 获取字符串的拼音
|
|
100
92
|
* @returns {string}
|
|
101
93
|
*/
|
|
102
94
|
String.prototype.pinyin = function () {
|
|
103
95
|
return pinyin(this).join('');
|
|
104
96
|
};
|
|
105
97
|
/**
|
|
106
|
-
*
|
|
98
|
+
* 获取字符串的拼音
|
|
107
99
|
* @returns {string} 拼音
|
|
108
100
|
*/
|
|
109
101
|
String.prototype.pinyinS = function () {
|
|
110
|
-
|
|
102
|
+
let arr = pinyin(this);
|
|
111
103
|
let result = '';
|
|
112
|
-
|
|
104
|
+
let len = arr.length;
|
|
113
105
|
|
|
114
106
|
for (let i = 0; i < len; i++) {
|
|
115
|
-
|
|
107
|
+
let ar = arr[i];
|
|
116
108
|
if (ar.length > 0) {
|
|
117
|
-
|
|
109
|
+
let char = ar[0];
|
|
118
110
|
result += char.charAt(0).toLocaleUpperCase() + char.substring(1);
|
|
119
111
|
} else {
|
|
120
112
|
result += ' ';
|
|
@@ -124,7 +116,7 @@ String.prototype.pinyinS = function () {
|
|
|
124
116
|
return result;
|
|
125
117
|
};
|
|
126
118
|
/**
|
|
127
|
-
*
|
|
119
|
+
* 将json字符串转为对象
|
|
128
120
|
* @returns {object} 对象
|
|
129
121
|
*/
|
|
130
122
|
String.prototype.toJson = function () {
|
|
@@ -135,38 +127,38 @@ String.prototype.toJson = function () {
|
|
|
135
127
|
}
|
|
136
128
|
};
|
|
137
129
|
/**
|
|
138
|
-
*
|
|
130
|
+
* 将xml字符串转为对象
|
|
139
131
|
* @returns {object} 对象
|
|
140
132
|
*/
|
|
141
133
|
String.prototype.toXml = function () {
|
|
142
134
|
try {
|
|
143
|
-
const parser = new
|
|
135
|
+
const parser = new XmlParser();
|
|
144
136
|
return parser.parse(this);
|
|
145
137
|
} catch (error) {
|
|
146
138
|
throw new Error('xml反序列化错误!', error);
|
|
147
139
|
}
|
|
148
140
|
};
|
|
149
141
|
/**
|
|
150
|
-
*
|
|
142
|
+
* 将url字符串转为对象
|
|
151
143
|
* @returns {object} 对象
|
|
152
144
|
*/
|
|
153
145
|
String.prototype.toUrl = function () {
|
|
154
|
-
|
|
146
|
+
let parts = this.split('&');
|
|
155
147
|
const url = {};
|
|
156
148
|
|
|
157
|
-
parts.forEach(
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
url[
|
|
149
|
+
parts.forEach((item) => {
|
|
150
|
+
var parts = item.split('=');
|
|
151
|
+
if (parts.length > 1) {
|
|
152
|
+
url[parts[0]] = decodeURIComponent(parts[1]);
|
|
161
153
|
} else {
|
|
162
|
-
url[
|
|
154
|
+
url[parts[0]] = null;
|
|
163
155
|
}
|
|
164
156
|
});
|
|
165
157
|
|
|
166
158
|
return url;
|
|
167
159
|
};
|
|
168
160
|
/**
|
|
169
|
-
*
|
|
161
|
+
* 删除首字符
|
|
170
162
|
* @param {string} chars 要删除的字符, 默认删除空字符
|
|
171
163
|
* @returns {string} 删除后字符串
|
|
172
164
|
*/
|
|
@@ -184,7 +176,7 @@ String.prototype.startTrim = function (chars) {
|
|
|
184
176
|
return this.replace(regex, '');
|
|
185
177
|
};
|
|
186
178
|
/**
|
|
187
|
-
*
|
|
179
|
+
* 删除尾字符
|
|
188
180
|
* @param {string} chars 要删除的字符, 默认删除空字符
|
|
189
181
|
* @returns {string} 删除后字符串
|
|
190
182
|
*/
|
|
@@ -202,7 +194,7 @@ String.prototype.endTrim = function (chars) {
|
|
|
202
194
|
return this.replace(regex, '');
|
|
203
195
|
};
|
|
204
196
|
/**
|
|
205
|
-
*
|
|
197
|
+
* 删除首尾字符
|
|
206
198
|
* @param {string} chars 要删除的字符, 默认删除空字符
|
|
207
199
|
* @returns {string} 删除后字符串
|
|
208
200
|
*/
|
|
@@ -220,13 +212,13 @@ String.prototype.trim = function (chars) {
|
|
|
220
212
|
return this.replace(regex, '');
|
|
221
213
|
};
|
|
222
214
|
/**
|
|
223
|
-
*
|
|
215
|
+
* 取文本左边
|
|
224
216
|
* @param {string} delimiter 分割字符
|
|
225
217
|
* @param {boolean} retain 当索引字符不存在时是否保留左边
|
|
226
218
|
* @returns {string} 截取后的字符串
|
|
227
219
|
*/
|
|
228
220
|
String.prototype.left = function (delimiter, retain) {
|
|
229
|
-
|
|
221
|
+
let index = this.indexOf(delimiter);
|
|
230
222
|
|
|
231
223
|
if (index === -1) {
|
|
232
224
|
if (retain) {
|
|
@@ -239,13 +231,13 @@ String.prototype.left = function (delimiter, retain) {
|
|
|
239
231
|
}
|
|
240
232
|
};
|
|
241
233
|
/**
|
|
242
|
-
*
|
|
234
|
+
* 取文本右边
|
|
243
235
|
* @param {string} delimiter 分割字符
|
|
244
236
|
* @param {boolean} retain 当索引字符不存在时是否保留右边
|
|
245
237
|
* @returns {string} 截取后的字符串
|
|
246
238
|
*/
|
|
247
239
|
String.prototype.right = function (delimiter, retain) {
|
|
248
|
-
|
|
240
|
+
let index = this.indexOf(delimiter);
|
|
249
241
|
|
|
250
242
|
if (index === -1) {
|
|
251
243
|
if (retain) {
|
|
@@ -258,27 +250,29 @@ String.prototype.right = function (delimiter, retain) {
|
|
|
258
250
|
}
|
|
259
251
|
};
|
|
260
252
|
/**
|
|
261
|
-
*
|
|
253
|
+
* 取文本之间
|
|
262
254
|
* @param {string} leftDelimiter 索引的左边字符
|
|
263
255
|
* @param {string} rightDelimiter 索引的右边字符
|
|
264
256
|
* @param left_delimiter
|
|
257
|
+
* @param left_deli
|
|
265
258
|
* @param right_delimiter
|
|
259
|
+
* @param right_deli
|
|
266
260
|
* @param {boolean} retain 当索引字符不存在时是否保留右边
|
|
267
261
|
* @returns {string} 截取后的字符串
|
|
268
262
|
*/
|
|
269
|
-
String.prototype.between = function (
|
|
270
|
-
|
|
263
|
+
String.prototype.between = function (left_deli, right_deli, retain) {
|
|
264
|
+
let str = this.right(left_delimiter, retain);
|
|
271
265
|
return str.left(right_delimiter, retain);
|
|
272
266
|
};
|
|
273
267
|
/**
|
|
274
|
-
*
|
|
268
|
+
* 替换所有字符串
|
|
275
269
|
* @param {string} search 被替换的字符串
|
|
276
270
|
* @param {string} replacement 替换后的字符串
|
|
277
271
|
* @returns {string} 替换后的字符串
|
|
278
272
|
*/
|
|
279
273
|
String.prototype.replaceAll = function (search, replacement) {
|
|
280
274
|
let txt = this + '';
|
|
281
|
-
|
|
275
|
+
let value = replacement || '';
|
|
282
276
|
|
|
283
277
|
while (txt.indexOf(search) !== -1) {
|
|
284
278
|
txt = txt.replace(search, value);
|
|
@@ -288,25 +282,25 @@ String.prototype.replaceAll = function (search, replacement) {
|
|
|
288
282
|
};
|
|
289
283
|
|
|
290
284
|
/**
|
|
291
|
-
*
|
|
285
|
+
* 是否含字符串
|
|
292
286
|
* @param {string} pattern = [word|word*|*word|*word*]字符串, 支持*号匹配, 前*表示匹配后面字符串, 后*表示匹配前面字符串,
|
|
293
287
|
前后*表示匹配包含字符串
|
|
294
288
|
* @returns {boolean} 包含返回true, 不包含返回false
|
|
295
289
|
*/
|
|
296
290
|
String.prototype.has = function (pattern) {
|
|
297
|
-
|
|
291
|
+
let str = this + '';
|
|
298
292
|
|
|
299
293
|
if (str === pattern) {
|
|
300
294
|
return true;
|
|
301
295
|
} else if (pattern.startsWith('*')) {
|
|
302
|
-
|
|
296
|
+
let clean = pattern.replaceAll('*', '');
|
|
303
297
|
if (pattern.endsWith('*')) {
|
|
304
298
|
return str.indexOf(clean) !== -1;
|
|
305
299
|
} else {
|
|
306
300
|
return str.endsWith(clean);
|
|
307
301
|
}
|
|
308
302
|
} else if (pattern.endsWith('*')) {
|
|
309
|
-
|
|
303
|
+
let clean = pattern.replaceAll('*', '');
|
|
310
304
|
return str.startsWith(clean);
|
|
311
305
|
}
|
|
312
306
|
else {
|
|
@@ -315,7 +309,7 @@ String.prototype.has = function (pattern) {
|
|
|
315
309
|
};
|
|
316
310
|
|
|
317
311
|
/**
|
|
318
|
-
*
|
|
312
|
+
* 转为时间对象
|
|
319
313
|
* @returns {Date} 时间对象
|
|
320
314
|
*/
|
|
321
315
|
String.prototype.toTime = function () {
|
|
@@ -340,51 +334,51 @@ String.prototype.toTime = function () {
|
|
|
340
334
|
return date;
|
|
341
335
|
};
|
|
342
336
|
/**
|
|
343
|
-
*
|
|
337
|
+
* 转为时间戳
|
|
344
338
|
* @returns {number} 返回时间戳
|
|
345
339
|
*/
|
|
346
340
|
String.prototype.stamp = function () {
|
|
347
|
-
|
|
341
|
+
let date = this.toTime();
|
|
348
342
|
return typeof date.stamp === 'function' ? date.stamp() : Math.floor(date.getTime() / 1000);
|
|
349
343
|
};
|
|
350
344
|
/**
|
|
351
|
-
*
|
|
345
|
+
* 转为时间格式字符串
|
|
352
346
|
* @param {string} format 转换的格式
|
|
353
347
|
* @returns {string} 时间格式字符串
|
|
354
348
|
*/
|
|
355
349
|
String.prototype.toTimeFormat = function (format) {
|
|
356
|
-
|
|
350
|
+
let date = this.toTime();
|
|
357
351
|
return typeof date.toStr === 'function' ? date.toStr(format) : date.toISOString();
|
|
358
352
|
};
|
|
359
353
|
/**
|
|
360
|
-
*
|
|
354
|
+
* 转为时间格式字符串
|
|
361
355
|
* @param {string} format 转换的格式
|
|
362
356
|
* @returns {string} 时间格式字符串
|
|
363
357
|
*/
|
|
364
358
|
String.prototype.toTimeStr = function (format) {
|
|
365
|
-
|
|
359
|
+
let date = this.toTime();
|
|
366
360
|
return typeof date.toStr === 'function' ? date.toStr(format) : date.toISOString();
|
|
367
361
|
};
|
|
368
362
|
/**
|
|
369
|
-
*
|
|
363
|
+
* 转为时间戳
|
|
370
364
|
* @returns {number} 时间戳
|
|
371
365
|
*/
|
|
372
366
|
String.prototype.toTimestamp = function () {
|
|
373
|
-
|
|
367
|
+
let date = this.toTime();
|
|
374
368
|
return typeof date.stamp === 'function' ? date.stamp() : Math.floor(date.getTime() / 1000);
|
|
375
369
|
};
|
|
376
370
|
|
|
377
371
|
/**
|
|
378
|
-
*
|
|
372
|
+
* 时间增加秒数
|
|
379
373
|
* @param {number} seconds 增加秒数
|
|
380
374
|
* @param {string} format 转换的格式
|
|
381
375
|
* @returns {string | Date} 时间格式字符串
|
|
382
376
|
*/
|
|
383
377
|
String.prototype.addSeconds = function (seconds, format) {
|
|
384
|
-
|
|
378
|
+
let date = this.toTime();
|
|
385
379
|
|
|
386
380
|
if (typeof date.addSeconds === 'function') {
|
|
387
|
-
|
|
381
|
+
let result = date.addSeconds(seconds);
|
|
388
382
|
|
|
389
383
|
if (format && typeof result.toStr === 'function') {
|
|
390
384
|
return result.toStr(format);
|
|
@@ -394,21 +388,21 @@ String.prototype.addSeconds = function (seconds, format) {
|
|
|
394
388
|
}
|
|
395
389
|
|
|
396
390
|
// 降级处理
|
|
397
|
-
const
|
|
398
|
-
return format ?
|
|
391
|
+
const newTime = new Date(date.getTime() + seconds * 1000);
|
|
392
|
+
return format ? newTime.toISOString() : newTime;
|
|
399
393
|
};
|
|
400
394
|
|
|
401
395
|
/**
|
|
402
|
-
*
|
|
396
|
+
* 时间增加天数
|
|
403
397
|
* @param {number} days 增加天数
|
|
404
398
|
* @param {string} format 转换的格式
|
|
405
399
|
* @returns {string | Date} 时间格式字符串
|
|
406
400
|
*/
|
|
407
401
|
String.prototype.addDays = function (days, format) {
|
|
408
|
-
|
|
402
|
+
let date = this.toTime();
|
|
409
403
|
|
|
410
404
|
if (typeof date.addDays === 'function') {
|
|
411
|
-
|
|
405
|
+
let result = date.addDays(days);
|
|
412
406
|
|
|
413
407
|
if (format && typeof result.toStr === 'function') {
|
|
414
408
|
return result.toStr(format);
|
|
@@ -418,16 +412,16 @@ String.prototype.addDays = function (days, format) {
|
|
|
418
412
|
}
|
|
419
413
|
|
|
420
414
|
// 降级处理
|
|
421
|
-
const
|
|
422
|
-
return format ?
|
|
415
|
+
const newTime = new Date(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
416
|
+
return format ? newTime.toISOString() : newTime;
|
|
423
417
|
};
|
|
424
418
|
|
|
425
419
|
/**
|
|
426
|
-
*
|
|
420
|
+
* 转为数组
|
|
427
421
|
* @returns {Array} 数组
|
|
428
422
|
*/
|
|
429
423
|
String.prototype.toArr = function () {
|
|
430
|
-
|
|
424
|
+
let str = this.trim();
|
|
431
425
|
|
|
432
426
|
try {
|
|
433
427
|
if (str.startsWith('[') && str.endsWith(']')) {
|
|
@@ -442,12 +436,12 @@ String.prototype.toArr = function () {
|
|
|
442
436
|
};
|
|
443
437
|
|
|
444
438
|
/**
|
|
445
|
-
*
|
|
439
|
+
* 转为数值
|
|
446
440
|
* @param {number} precision 精度
|
|
447
441
|
* @returns {number} 数值
|
|
448
442
|
*/
|
|
449
443
|
String.prototype.toNum = function (precision) {
|
|
450
|
-
|
|
444
|
+
let val = parseFloat(this);
|
|
451
445
|
|
|
452
446
|
if (isNaN(val)) {
|
|
453
447
|
return 0;
|
|
@@ -460,11 +454,11 @@ String.prototype.toNum = function (precision) {
|
|
|
460
454
|
return val;
|
|
461
455
|
};
|
|
462
456
|
/**
|
|
463
|
-
*
|
|
457
|
+
* 转为对象
|
|
464
458
|
* @returns {object} 对象
|
|
465
459
|
*/
|
|
466
460
|
String.prototype.toObj = function () {
|
|
467
|
-
|
|
461
|
+
let str = this.trim();
|
|
468
462
|
|
|
469
463
|
try {
|
|
470
464
|
if (str.startsWith('{') && str.endsWith('}')) {
|
|
@@ -477,17 +471,17 @@ String.prototype.toObj = function () {
|
|
|
477
471
|
return {};
|
|
478
472
|
};
|
|
479
473
|
/**
|
|
480
|
-
*
|
|
474
|
+
* 转正则表达式
|
|
481
475
|
* @param {string} mode = [g|i|gi] 转换方式, g为全部, i为不区分大小写
|
|
482
476
|
* @returns {Regex} 返回正则对象
|
|
483
477
|
*/
|
|
484
478
|
String.prototype.toRx = function (mode) {
|
|
485
|
-
|
|
479
|
+
var _mode = mode || 'gi';
|
|
486
480
|
|
|
487
481
|
try {
|
|
488
482
|
// 转义正则特殊字符
|
|
489
|
-
|
|
490
|
-
return new RegExp(pattern,
|
|
483
|
+
let pattern = this.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
484
|
+
return new RegExp(pattern, _mode);
|
|
491
485
|
} catch (error) {
|
|
492
486
|
throw new Error('字符串转正则错误:', error);
|
|
493
487
|
}
|
|
@@ -592,15 +586,15 @@ const validators = {
|
|
|
592
586
|
};
|
|
593
587
|
|
|
594
588
|
/**
|
|
595
|
-
*
|
|
589
|
+
* 验证字符串格式
|
|
596
590
|
* @param {string} format 所属格式
|
|
597
591
|
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
598
592
|
*/
|
|
599
593
|
String.prototype.validate = function (format) {
|
|
600
|
-
|
|
601
|
-
|
|
594
|
+
let value = this + '';
|
|
595
|
+
let format_key = format.toLowerCase();
|
|
602
596
|
|
|
603
|
-
|
|
597
|
+
let validator = validators[format_key];
|
|
604
598
|
if (!validator) {
|
|
605
599
|
throw new Error('输入的验证类型错误');
|
|
606
600
|
}
|
|
@@ -609,7 +603,7 @@ String.prototype.validate = function (format) {
|
|
|
609
603
|
};
|
|
610
604
|
|
|
611
605
|
/**
|
|
612
|
-
*
|
|
606
|
+
* 模板替换
|
|
613
607
|
* @param {object} data 替换的数据
|
|
614
608
|
* @param {RegExp} regex 正则表达式
|
|
615
609
|
* @returns {string} 替换后的字符串
|
|
@@ -623,8 +617,8 @@ String.prototype.tpl = function (data, regex) {
|
|
|
623
617
|
}
|
|
624
618
|
|
|
625
619
|
// 处理模板字符串
|
|
626
|
-
compiled = compiled.replace(regex || /\$\{(.*?)\}/g,
|
|
627
|
-
|
|
620
|
+
compiled = compiled.replace(regex || /\$\{(.*?)\}/g, (match, key) => {
|
|
621
|
+
let keys = key.trim().split('.');
|
|
628
622
|
let val = data;
|
|
629
623
|
|
|
630
624
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -641,7 +635,7 @@ String.prototype.tpl = function (data, regex) {
|
|
|
641
635
|
};
|
|
642
636
|
|
|
643
637
|
/**
|
|
644
|
-
*
|
|
638
|
+
* 导出String原型扩展
|
|
645
639
|
*/
|
|
646
640
|
module.exports = {
|
|
647
641
|
String
|
package/lib/timer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { Base } = require('./base.js');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 定时器类
|
|
5
5
|
*/
|
|
6
6
|
class Timer extends Base {
|
|
7
7
|
static config = {
|
|
@@ -12,7 +12,7 @@ class Timer extends Base {
|
|
|
12
12
|
remove_faulty_tasks: false
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* 定时器构造函数
|
|
16
16
|
* @param {object} config 配置参数
|
|
17
17
|
*/
|
|
18
18
|
constructor(config) {
|
|
@@ -27,7 +27,7 @@ class Timer extends Base {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* 执行所有定时任务(并行执行,性能优化版)
|
|
31
31
|
* @returns {Promise<void>} Promise对象,支持异步操作
|
|
32
32
|
*/
|
|
33
33
|
Timer.prototype.run = async function () {
|
|
@@ -138,7 +138,7 @@ Timer.prototype._handleTaskError = function (error, obj, index) {
|
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
/**
|
|
141
|
-
*
|
|
141
|
+
* 启动
|
|
142
142
|
*/
|
|
143
143
|
Timer.prototype.start = function () {
|
|
144
144
|
if (!this.core) {
|
|
@@ -148,7 +148,7 @@ Timer.prototype.start = function () {
|
|
|
148
148
|
};
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
|
-
*
|
|
151
|
+
* 结束
|
|
152
152
|
*/
|
|
153
153
|
Timer.prototype.end = function () {
|
|
154
154
|
clearInterval(this.core);
|
|
@@ -156,7 +156,7 @@ Timer.prototype.end = function () {
|
|
|
156
156
|
};
|
|
157
157
|
|
|
158
158
|
/**
|
|
159
|
-
*
|
|
159
|
+
* 任务模型
|
|
160
160
|
* @param {object} obj 任务对象
|
|
161
161
|
* @returns {object} 任务模型对象
|
|
162
162
|
*/
|
|
@@ -169,7 +169,7 @@ Timer.prototype.model = function (obj) {
|
|
|
169
169
|
return {
|
|
170
170
|
name: 'anonymous',
|
|
171
171
|
run: function () { }, ...obj
|
|
172
|
-
}
|
|
172
|
+
};
|
|
173
173
|
};
|
|
174
174
|
|
|
175
175
|
/**
|
|
@@ -189,7 +189,7 @@ Timer.prototype.addObj = function (obj) {
|
|
|
189
189
|
};
|
|
190
190
|
|
|
191
191
|
/**
|
|
192
|
-
*
|
|
192
|
+
* 获取任务名称
|
|
193
193
|
* @param {Function} func 任务函数
|
|
194
194
|
* @param {string} [name] 可选的任务名称
|
|
195
195
|
* @returns {string} 任务名称
|
|
@@ -211,7 +211,7 @@ Timer.prototype._getTaskName = function (func, name) {
|
|
|
211
211
|
};
|
|
212
212
|
|
|
213
213
|
/**
|
|
214
|
-
*
|
|
214
|
+
* 添加函数任务
|
|
215
215
|
* @param {Function} func 任务函数
|
|
216
216
|
* @param {string} [name] 可选的任务名称
|
|
217
217
|
* @private
|
|
@@ -226,7 +226,7 @@ Timer.prototype._addFunc = function (func, name) {
|
|
|
226
226
|
};
|
|
227
227
|
|
|
228
228
|
/**
|
|
229
|
-
*
|
|
229
|
+
* 添加定时执行任务
|
|
230
230
|
* @param {Function | object} obj_or_func 要添加的任务函数或对象
|
|
231
231
|
* @param {string} [name] 可选的任务名称,用于标识任务
|
|
232
232
|
*/
|
|
@@ -262,7 +262,7 @@ Timer.prototype.del = function (name) {
|
|
|
262
262
|
};
|
|
263
263
|
|
|
264
264
|
/**
|
|
265
|
-
*
|
|
265
|
+
* 设置定时器
|
|
266
266
|
* @param {Function} func 要执行的函数
|
|
267
267
|
* @param {number} delay 延迟时间(毫秒)
|
|
268
268
|
* @returns {object} 定时器ID
|
|
@@ -279,7 +279,7 @@ Timer.prototype.setTimeout = function (func, delay) {
|
|
|
279
279
|
};
|
|
280
280
|
|
|
281
281
|
/**
|
|
282
|
-
*
|
|
282
|
+
* 清除定时器
|
|
283
283
|
* @param {object} timer_id 定时器ID
|
|
284
284
|
*/
|
|
285
285
|
Timer.prototype.clearTimeout = function (timer_id) {
|
|
@@ -290,7 +290,7 @@ Timer.prototype.clearTimeout = function (timer_id) {
|
|
|
290
290
|
};
|
|
291
291
|
|
|
292
292
|
/**
|
|
293
|
-
*
|
|
293
|
+
* 设置间隔定时器
|
|
294
294
|
* @param {Function} func 要执行的函数
|
|
295
295
|
* @param {number} interval 间隔时间(毫秒)
|
|
296
296
|
* @returns {object} 定时器ID
|
|
@@ -307,7 +307,7 @@ Timer.prototype.setInterval = function (func, interval) {
|
|
|
307
307
|
};
|
|
308
308
|
|
|
309
309
|
/**
|
|
310
|
-
*
|
|
310
|
+
* 清除间隔定时器
|
|
311
311
|
* @param {object} timer_id 定时器ID
|
|
312
312
|
*/
|
|
313
313
|
Timer.prototype.clearInterval = function (timer_id) {
|
|
@@ -318,7 +318,7 @@ Timer.prototype.clearInterval = function (timer_id) {
|
|
|
318
318
|
};
|
|
319
319
|
|
|
320
320
|
/**
|
|
321
|
-
*
|
|
321
|
+
* 移除有问题的任务
|
|
322
322
|
* @param {number} index 任务索引
|
|
323
323
|
* @param {string} task_name 任务名称
|
|
324
324
|
* @private
|