mm_expand 2.3.7 → 2.3.9
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 +11 -18
- package/lib/base.js +3 -1
- package/lib/date.js +10 -10
- package/lib/event.js +6 -6
- package/lib/eventer.js +24 -56
- package/lib/file.js +7 -9
- package/lib/global.js +16 -16
- package/lib/lang.js +10 -9
- package/lib/object.js +8 -7
- package/lib/ret.js +11 -13
- package/lib/string.js +60 -46
- package/package.json +1 -1
package/lib/array.js
CHANGED
|
@@ -50,7 +50,7 @@ function toList(list, sub = 'sub', result = []) {
|
|
|
50
50
|
/**
|
|
51
51
|
* 排序
|
|
52
52
|
* @param {string} key 属性键
|
|
53
|
-
* @returns {Function
|
|
53
|
+
* @returns {Function} 排序函数
|
|
54
54
|
*/
|
|
55
55
|
function newSort(key) {
|
|
56
56
|
var field = key;
|
|
@@ -154,8 +154,7 @@ function createDescCnSort(field) {
|
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
156
|
* 列表转树形列表
|
|
157
|
-
* @param {string
|
|
158
|
-
* @param {string} id_or_config 主键字段名
|
|
157
|
+
* @param {string|object} id_or_config 主键字段名或配置对象
|
|
159
158
|
* @param {number} value 根节点值
|
|
160
159
|
* @param {string} father_id 父节点字段名
|
|
161
160
|
* @param {string} sub 子节点字段名
|
|
@@ -209,7 +208,7 @@ Array.prototype.copy = function (has) {
|
|
|
209
208
|
};
|
|
210
209
|
/**
|
|
211
210
|
* 遍历数组执行函数
|
|
212
|
-
* @param {Function
|
|
211
|
+
* @param {Function} func 处理函数
|
|
213
212
|
*/
|
|
214
213
|
Array.prototype.func = function (func) {
|
|
215
214
|
var count = this.length;
|
|
@@ -221,8 +220,7 @@ Array.prototype.func = function (func) {
|
|
|
221
220
|
};
|
|
222
221
|
/**
|
|
223
222
|
* 数组转字符串
|
|
224
|
-
* @param {string}
|
|
225
|
-
* @param split_str
|
|
223
|
+
* @param {string} split_str 分隔符
|
|
226
224
|
* @param {string} key 对象属性名
|
|
227
225
|
* @returns {string} 字符串
|
|
228
226
|
*/
|
|
@@ -312,8 +310,7 @@ Array.prototype._sortByValue = function (method) {
|
|
|
312
310
|
/**
|
|
313
311
|
* 获取属性排序函数
|
|
314
312
|
* @param {string} method 排序方式
|
|
315
|
-
* @param {boolean}
|
|
316
|
-
* @param is_str
|
|
313
|
+
* @param {boolean} is_str 是否为字符串
|
|
317
314
|
* @param {object} cs 排序对象
|
|
318
315
|
* @returns {Function} 排序函数
|
|
319
316
|
*/
|
|
@@ -330,8 +327,7 @@ Array.prototype._getSortFunc = function (method, is_str, cs) {
|
|
|
330
327
|
/**
|
|
331
328
|
* 获取值排序函数
|
|
332
329
|
* @param {string} method 排序方式
|
|
333
|
-
* @param {boolean}
|
|
334
|
-
* @param is_str
|
|
330
|
+
* @param {boolean} is_str 是否为字符串
|
|
335
331
|
* @returns {Function} 排序函数
|
|
336
332
|
*/
|
|
337
333
|
Array.prototype._getValueSortFunc = function (method, is_str) {
|
|
@@ -434,11 +430,6 @@ Array.prototype.get = function (query, single) {
|
|
|
434
430
|
return results;
|
|
435
431
|
}
|
|
436
432
|
};
|
|
437
|
-
/**
|
|
438
|
-
*
|
|
439
|
-
* query: (object)
|
|
440
|
-
* (object)
|
|
441
|
-
*/
|
|
442
433
|
|
|
443
434
|
/**
|
|
444
435
|
* 从数组获取对象
|
|
@@ -777,9 +768,9 @@ Array.prototype.setObj = function (replacement, query, end) {
|
|
|
777
768
|
/**
|
|
778
769
|
* 设置数组中对象的属性值
|
|
779
770
|
* @param {Array} items 对象列表
|
|
780
|
-
* @param {
|
|
771
|
+
* @param {string} key 对象主键
|
|
781
772
|
* @param {boolean} end 是否中断循环,中断只修改第一个符合条件的对象
|
|
782
|
-
* @
|
|
773
|
+
* @throws {Error} 第2个参数是必须的
|
|
783
774
|
*/
|
|
784
775
|
Array.prototype.setList = function (items, key, end) {
|
|
785
776
|
if (!key) {
|
|
@@ -994,7 +985,8 @@ Array.prototype.toObj = function (key) {
|
|
|
994
985
|
|
|
995
986
|
/**
|
|
996
987
|
* 将一维数组转成二维
|
|
997
|
-
* @param {
|
|
988
|
+
* @param {number} size 每个子数组的大小
|
|
989
|
+
* @returns {object} 返回转换后的二维对象数组
|
|
998
990
|
*/
|
|
999
991
|
Array.prototype.to2D = function (size) {
|
|
1000
992
|
var arr = this;
|
|
@@ -1008,6 +1000,7 @@ Array.prototype.to2D = function (size) {
|
|
|
1008
1000
|
|
|
1009
1001
|
/**
|
|
1010
1002
|
* 将二维数组合并转成一维
|
|
1003
|
+
* @returns {object} 返回合并后的数组对象
|
|
1011
1004
|
*/
|
|
1012
1005
|
Array.prototype.to1D = function () {
|
|
1013
1006
|
var source = this;
|
package/lib/base.js
CHANGED
|
@@ -175,6 +175,7 @@ Base.prototype._destroyCore = async function () {
|
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
177
|
* 销毁模块 - 内部方法
|
|
178
|
+
* @returns {object} 返回当前对象
|
|
178
179
|
* @private
|
|
179
180
|
*/
|
|
180
181
|
Base.prototype._destroy = async function () {
|
|
@@ -183,6 +184,7 @@ Base.prototype._destroy = async function () {
|
|
|
183
184
|
|
|
184
185
|
/**
|
|
185
186
|
* 销毁
|
|
187
|
+
* @returns {object} 返回当前对象
|
|
186
188
|
*/
|
|
187
189
|
Base.prototype.destroy = async function () {
|
|
188
190
|
return await this._destroy();
|
|
@@ -383,7 +385,6 @@ Base.prototype.doEasy = async function (method, ...params) {
|
|
|
383
385
|
* 智能执行方法(自动选择do或doEasy)
|
|
384
386
|
* @param {string} method 方法名称
|
|
385
387
|
* @param {...object} params 参数列表
|
|
386
|
-
* @param {object} options 执行选项
|
|
387
388
|
* @returns {object} 返回执行结果
|
|
388
389
|
*/
|
|
389
390
|
Base.prototype.doSmart = async function (method, ...params) {
|
|
@@ -435,6 +436,7 @@ Base.prototype._main = async function (...params) {
|
|
|
435
436
|
/**
|
|
436
437
|
* 主要执行方法
|
|
437
438
|
* @param {...any} params 参数列表
|
|
439
|
+
* @returns {object} 返回执行结果
|
|
438
440
|
*/
|
|
439
441
|
Base.prototype.main = async function (...params) {
|
|
440
442
|
return await this._main(...params);
|
package/lib/date.js
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @param {string} format 指定格式
|
|
4
4
|
* @returns {string} 时间格式字符串
|
|
5
5
|
*/
|
|
6
|
-
Date.prototype.toStr = function (
|
|
7
|
-
var
|
|
6
|
+
Date.prototype.toStr = function (format) {
|
|
7
|
+
var f = format;
|
|
8
8
|
var t = this;
|
|
9
|
-
if (
|
|
9
|
+
if (f.endsWith('Z')) {
|
|
10
10
|
t = t.addSeconds(-28800);
|
|
11
11
|
}
|
|
12
12
|
var obj = {
|
|
@@ -18,18 +18,18 @@ Date.prototype.toStr = function (fm) {
|
|
|
18
18
|
'q+': Math.floor((t.getMonth() + 3) / 3),
|
|
19
19
|
'S': t.getMilliseconds()
|
|
20
20
|
};
|
|
21
|
-
var
|
|
22
|
-
if (
|
|
23
|
-
|
|
21
|
+
var year_match = f.match(/(y+)/);
|
|
22
|
+
if (year_match) {
|
|
23
|
+
f = f.replace(year_match[1], (t.getFullYear() + '').substring(4 - year_match[1].length));
|
|
24
24
|
}
|
|
25
25
|
for (var k in obj) {
|
|
26
|
-
var match =
|
|
26
|
+
var match = f.match(new RegExp('(' + k + ')'));
|
|
27
27
|
if (match) {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
f = f.replace(match[1], match[1].length == 1 ? obj[k] : ('00' + obj[k]).substring(('' +
|
|
29
|
+
obj[k]).length));
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
return
|
|
32
|
+
return f;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
/**
|
package/lib/event.js
CHANGED
|
@@ -105,7 +105,7 @@ class Event {
|
|
|
105
105
|
/**
|
|
106
106
|
* 添加事件监听器
|
|
107
107
|
* @param {string} event_name 事件名称
|
|
108
|
-
* @param {
|
|
108
|
+
* @param {Function} listener 监听器函数
|
|
109
109
|
* @returns {Event} 返回this,支持链式调用
|
|
110
110
|
*/
|
|
111
111
|
on(event_name, listener) {
|
|
@@ -142,7 +142,7 @@ class Event {
|
|
|
142
142
|
/**
|
|
143
143
|
* 添加一次性事件监听器
|
|
144
144
|
* @param {string} event_name 事件名称
|
|
145
|
-
* @param {
|
|
145
|
+
* @param {Function} listener 监听器函数
|
|
146
146
|
* @returns {Event} 返回this,支持链式调用
|
|
147
147
|
*/
|
|
148
148
|
once(event_name, listener) {
|
|
@@ -158,7 +158,7 @@ class Event {
|
|
|
158
158
|
/**
|
|
159
159
|
* 移除事件监听器(removeListener 别名)
|
|
160
160
|
* @param {string} event_name 事件名称
|
|
161
|
-
* @param {
|
|
161
|
+
* @param {Function} listener 监听器函数
|
|
162
162
|
* @returns {Event} 返回this,支持链式调用
|
|
163
163
|
*/
|
|
164
164
|
removeListener(event_name, listener) {
|
|
@@ -168,7 +168,7 @@ class Event {
|
|
|
168
168
|
/**
|
|
169
169
|
* 移除事件监听器
|
|
170
170
|
* @param {string} event_name 事件名称
|
|
171
|
-
* @param {
|
|
171
|
+
* @param {Function} listener 监听器函数
|
|
172
172
|
* @returns {Event} 返回this,支持链式调用
|
|
173
173
|
*/
|
|
174
174
|
off(event_name, listener) {
|
|
@@ -456,7 +456,7 @@ class Event {
|
|
|
456
456
|
/**
|
|
457
457
|
* 前置添加事件监听器
|
|
458
458
|
* @param {string} event_name 事件名称
|
|
459
|
-
* @param {
|
|
459
|
+
* @param {Function} listener 监听器函数
|
|
460
460
|
* @returns {Event} 返回this,支持链式调用
|
|
461
461
|
*/
|
|
462
462
|
prependListener(event_name, listener) {
|
|
@@ -494,7 +494,7 @@ class Event {
|
|
|
494
494
|
/**
|
|
495
495
|
* 前置添加一次性事件监听器
|
|
496
496
|
* @param {string} event_name 事件名称
|
|
497
|
-
* @param {
|
|
497
|
+
* @param {Function} listener 监听器函数
|
|
498
498
|
* @returns {Event} 返回this,支持链式调用
|
|
499
499
|
*/
|
|
500
500
|
prependOnceListener(event_name, listener) {
|
package/lib/eventer.js
CHANGED
|
@@ -13,7 +13,7 @@ class Eventer {
|
|
|
13
13
|
error: null, // 全局错误处理函数
|
|
14
14
|
pause_enabled: true, // 是否启用暂停功能
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
// 日志对象
|
|
18
18
|
#logger = $.log || console;
|
|
19
19
|
|
|
@@ -53,7 +53,7 @@ class Eventer {
|
|
|
53
53
|
setLogger(logger) {
|
|
54
54
|
this.#logger = logger;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
/**
|
|
58
58
|
* 获取日志记录器
|
|
59
59
|
* @returns {object} 日志记录器对象
|
|
@@ -97,12 +97,8 @@ Eventer.prototype.init = async function (...args) {
|
|
|
97
97
|
* 监听事件(支持命名空间和优先级)
|
|
98
98
|
* @param {string} key 事件关键字,支持space:key格式
|
|
99
99
|
* @param {object} func 触发函数
|
|
100
|
-
* @
|
|
101
|
-
* @
|
|
102
|
-
* @param {number} priority 优先级,数字越小优先级越高,默认为0
|
|
103
|
-
* @param {object} options 其他选项,如throttle、debounce、timeout等
|
|
104
|
-
* @param {string} ns 可选的显式命名空间,优先级高于key中的命名空间
|
|
105
|
-
* @returns {object} 返回事件信息对象 {space, index, name, remove},remove为移除该事件的函数
|
|
100
|
+
* @throws {Error} 事件关键字必须是非空字符串
|
|
101
|
+
* @throws {Error} 事件处理函数必须是一个函数
|
|
106
102
|
*/
|
|
107
103
|
Eventer.prototype._validateOnParams = function (key, func) {
|
|
108
104
|
if (!key || typeof key !== 'string') {
|
|
@@ -191,9 +187,6 @@ Eventer.prototype._createHandlerResult = function (space, index, name, full_key)
|
|
|
191
187
|
space,
|
|
192
188
|
index,
|
|
193
189
|
name,
|
|
194
|
-
/**
|
|
195
|
-
*
|
|
196
|
-
*/
|
|
197
190
|
remove: () => this.off(full_key, name)
|
|
198
191
|
};
|
|
199
192
|
};
|
|
@@ -342,9 +335,7 @@ Eventer.prototype.once = function (key, func, name, options) {
|
|
|
342
335
|
/**
|
|
343
336
|
* 统一的事件执行引擎
|
|
344
337
|
* @param {string} key 事件关键字,支持space:key格式
|
|
345
|
-
* @
|
|
346
|
-
* @param {...any} params 传递参数
|
|
347
|
-
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
338
|
+
* @throws {Error} 事件关键字必须是非空字符串
|
|
348
339
|
* @private
|
|
349
340
|
*/
|
|
350
341
|
Eventer.prototype._validateExec = function (key) {
|
|
@@ -370,9 +361,6 @@ Eventer.prototype._createCtx = function (key, params) {
|
|
|
370
361
|
executed: 0,
|
|
371
362
|
error_count: 0,
|
|
372
363
|
cancelled: false,
|
|
373
|
-
/**
|
|
374
|
-
*
|
|
375
|
-
*/
|
|
376
364
|
cancel: () => {
|
|
377
365
|
ctx.cancelled = true;
|
|
378
366
|
this._cancelTasks(full_key);
|
|
@@ -525,9 +513,6 @@ Eventer.prototype._buildSafeResult = function (ctx) {
|
|
|
525
513
|
return {
|
|
526
514
|
results: [],
|
|
527
515
|
cancelled: true,
|
|
528
|
-
/**
|
|
529
|
-
*
|
|
530
|
-
*/
|
|
531
516
|
cancel: () => { },
|
|
532
517
|
error: return_error
|
|
533
518
|
};
|
|
@@ -575,7 +560,7 @@ Eventer.prototype._createPausedResult = function () {
|
|
|
575
560
|
|
|
576
561
|
/**
|
|
577
562
|
* 执行事件列表
|
|
578
|
-
* @param {object} config
|
|
563
|
+
* @param {object} config 配置参数
|
|
579
564
|
* @param {Array} config.list 事件列表
|
|
580
565
|
* @param {Array} config.params 参数
|
|
581
566
|
* @param {object} config.ctx 上下文
|
|
@@ -585,6 +570,8 @@ Eventer.prototype._createPausedResult = function () {
|
|
|
585
570
|
* @param {string} config.space 命名空间
|
|
586
571
|
* @param {string} config.event_key 事件键
|
|
587
572
|
* @param {number} config.start_time 开始时间
|
|
573
|
+
* @param {string} config.strategy 执行策略,默认sequential
|
|
574
|
+
* @returns {object} 返回执行结果
|
|
588
575
|
* @private
|
|
589
576
|
*/
|
|
590
577
|
Eventer.prototype._executeEventList = async function (config) {
|
|
@@ -611,7 +598,7 @@ Eventer.prototype._executeEventList = async function (config) {
|
|
|
611
598
|
|
|
612
599
|
/**
|
|
613
600
|
* 顺序执行事件处理函数
|
|
614
|
-
* @param config
|
|
601
|
+
* @param {object} config 配置参数
|
|
615
602
|
* @private
|
|
616
603
|
*/
|
|
617
604
|
Eventer.prototype._executeSequential = async function (config) {
|
|
@@ -650,9 +637,6 @@ Eventer.prototype._createTask = function (o, full_key) {
|
|
|
650
637
|
let cancelled = false;
|
|
651
638
|
const task = {
|
|
652
639
|
id: task_id,
|
|
653
|
-
/**
|
|
654
|
-
*
|
|
655
|
-
*/
|
|
656
640
|
cancel: () => { cancelled = true; }
|
|
657
641
|
};
|
|
658
642
|
|
|
@@ -707,7 +691,7 @@ Eventer.prototype._handleTaskError = function (e, o, ctx, task) {
|
|
|
707
691
|
|
|
708
692
|
/**
|
|
709
693
|
* 并行执行事件处理函数
|
|
710
|
-
* @param config
|
|
694
|
+
* @param {object} config 配置参数
|
|
711
695
|
* @private
|
|
712
696
|
*/
|
|
713
697
|
Eventer.prototype._executeParallel = async function (config) {
|
|
@@ -738,7 +722,7 @@ Eventer.prototype._executeBatches = async function (config) {
|
|
|
738
722
|
for (let i = 0; i < list.length && !ctx.cancelled; i += concurrency) {
|
|
739
723
|
let batch = list.slice(i, i + concurrency);
|
|
740
724
|
let batch_promises = batch.map((obj, batch_idx) => {
|
|
741
|
-
let global = i + batch_idx;
|
|
725
|
+
let global = i + batch_idx;
|
|
742
726
|
return this._executeSingleTask({
|
|
743
727
|
obj, params, ctx, full_key, arr, idx: global, proc_results
|
|
744
728
|
});
|
|
@@ -764,7 +748,7 @@ Eventer.prototype._setResults = function (ctx, proc_results) {
|
|
|
764
748
|
|
|
765
749
|
/**
|
|
766
750
|
* 瀑布流执行事件处理函数
|
|
767
|
-
* @param config
|
|
751
|
+
* @param {object} config 配置参数
|
|
768
752
|
* @private
|
|
769
753
|
*/
|
|
770
754
|
Eventer.prototype._executeWaterfall = async function (config) {
|
|
@@ -792,9 +776,6 @@ Eventer.prototype._createWaterfallTask = function (obj, full_key) {
|
|
|
792
776
|
let cancelled = false;
|
|
793
777
|
const task = {
|
|
794
778
|
id: task_id,
|
|
795
|
-
/**
|
|
796
|
-
*
|
|
797
|
-
*/
|
|
798
779
|
cancel: () => { cancelled = true; }
|
|
799
780
|
};
|
|
800
781
|
|
|
@@ -864,6 +845,11 @@ Eventer.prototype._callErrorHandler = function (e, obj, ctx) {
|
|
|
864
845
|
/**
|
|
865
846
|
* 竞争执行模式 - 返回第一个完成的结果
|
|
866
847
|
* @param {object} config 配置参数
|
|
848
|
+
* @param {Array} config.list 事件列表
|
|
849
|
+
* @param {Array} config.params 参数
|
|
850
|
+
* @param {object} config.ctx 上下文
|
|
851
|
+
* @param {string} config.strategy 执行策略
|
|
852
|
+
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
867
853
|
* @private
|
|
868
854
|
*/
|
|
869
855
|
Eventer.prototype._executeRace = async function (config) {
|
|
@@ -910,9 +896,6 @@ Eventer.prototype._createRaceTask = function (obj, full_key) {
|
|
|
910
896
|
let cancelled = false;
|
|
911
897
|
const task = {
|
|
912
898
|
id: task_id,
|
|
913
|
-
/**
|
|
914
|
-
*
|
|
915
|
-
*/
|
|
916
899
|
cancel: () => { cancelled = true; }
|
|
917
900
|
};
|
|
918
901
|
|
|
@@ -960,7 +943,7 @@ Eventer.prototype._runRace = async function (promises) {
|
|
|
960
943
|
|
|
961
944
|
/**
|
|
962
945
|
* 执行单个任务(并行执行时使用)
|
|
963
|
-
* @param config
|
|
946
|
+
* @param {object} config 配置参数
|
|
964
947
|
* @private
|
|
965
948
|
*/
|
|
966
949
|
Eventer.prototype._executeSingleTask = async function (config) {
|
|
@@ -983,9 +966,6 @@ Eventer.prototype._createParallelTask = function (obj, full_key) {
|
|
|
983
966
|
let cancelled = false;
|
|
984
967
|
const task = {
|
|
985
968
|
id: task_id,
|
|
986
|
-
/**
|
|
987
|
-
*
|
|
988
|
-
*/
|
|
989
969
|
cancel: () => { cancelled = true; }
|
|
990
970
|
};
|
|
991
971
|
|
|
@@ -1060,9 +1040,6 @@ Eventer.prototype.emit = function (key, ...params) {
|
|
|
1060
1040
|
result.catch(e => this.log('error', '事件异步执行失败:', e));
|
|
1061
1041
|
return {
|
|
1062
1042
|
...result,
|
|
1063
|
-
/**
|
|
1064
|
-
*
|
|
1065
|
-
*/
|
|
1066
1043
|
cancel: () => {
|
|
1067
1044
|
try {
|
|
1068
1045
|
return result.then(r => r && typeof r.cancel === 'function' && r.cancel());
|
|
@@ -1078,9 +1055,6 @@ Eventer.prototype.emit = function (key, ...params) {
|
|
|
1078
1055
|
return {
|
|
1079
1056
|
results: [],
|
|
1080
1057
|
cancelled: false,
|
|
1081
|
-
/**
|
|
1082
|
-
*
|
|
1083
|
-
*/
|
|
1084
1058
|
cancel: () => { },
|
|
1085
1059
|
error: error
|
|
1086
1060
|
};
|
|
@@ -1386,7 +1360,6 @@ Eventer.prototype._removeSingleEvent = function (events, index_or_name) {
|
|
|
1386
1360
|
* 删除整个事件
|
|
1387
1361
|
* @private
|
|
1388
1362
|
* @param {string} space 命名空间
|
|
1389
|
-
* @param {string} key 事件关键字
|
|
1390
1363
|
* @param {string} event_key 事件键
|
|
1391
1364
|
* @param {string} full_key 完整事件关键字
|
|
1392
1365
|
* @returns {boolean} 删除成功返回true
|
|
@@ -1951,7 +1924,6 @@ Eventer.prototype._handleChainError = function (e, obj, ctx, results) {
|
|
|
1951
1924
|
* 并行执行事件(提高性能)
|
|
1952
1925
|
* @param {string} key 事件关键字,支持space:key格式
|
|
1953
1926
|
* @param {object} params 传递参数
|
|
1954
|
-
* @param {object} options 执行选项,如concurrency(并发数限制)
|
|
1955
1927
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
1956
1928
|
*/
|
|
1957
1929
|
Eventer.prototype.runParallel = async function (key, ...params) {
|
|
@@ -1970,7 +1942,6 @@ Eventer.prototype.runParallel = async function (key, ...params) {
|
|
|
1970
1942
|
* 并行触发事件
|
|
1971
1943
|
* @param {string} key 事件关键字,支持space:key格式
|
|
1972
1944
|
* @param {object} params 传递参数
|
|
1973
|
-
* @param {object} options 执行选项,如concurrency(并发数限制)
|
|
1974
1945
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
1975
1946
|
*/
|
|
1976
1947
|
Eventer.prototype.emitParallel = function (key, ...params) {
|
|
@@ -1982,23 +1953,20 @@ Eventer.prototype.emitParallel = function (key, ...params) {
|
|
|
1982
1953
|
result.catch(e => this.log('error', '事件并行执行失败:', e));
|
|
1983
1954
|
return {
|
|
1984
1955
|
...result,
|
|
1985
|
-
/**
|
|
1986
|
-
*
|
|
1987
|
-
*/
|
|
1988
1956
|
cancel: () => result.then(r => r && typeof r.cancel === 'function' && r.cancel())
|
|
1989
1957
|
};
|
|
1990
1958
|
};
|
|
1991
1959
|
|
|
1992
1960
|
/**
|
|
1993
1961
|
* 防止内存泄漏警告
|
|
1994
|
-
* @param {number}
|
|
1962
|
+
* @param {number} max_listen 最大监听器数量
|
|
1995
1963
|
* @returns {Eventer} 返回当前实例,支持链式调用
|
|
1996
1964
|
*/
|
|
1997
|
-
Eventer.prototype.setMaxListeners = function (
|
|
1998
|
-
if (typeof
|
|
1965
|
+
Eventer.prototype.setMaxListeners = function (max_listen) {
|
|
1966
|
+
if (typeof max_listen !== 'number' || max_listen < 0) {
|
|
1999
1967
|
throw new Error('最大监听器数量必须是一个非负数');
|
|
2000
1968
|
}
|
|
2001
|
-
this._max_listeners =
|
|
1969
|
+
this._max_listeners = max_listen;
|
|
2002
1970
|
return this;
|
|
2003
1971
|
};
|
|
2004
1972
|
|
|
@@ -2159,7 +2127,7 @@ Eventer.prototype._createChainCtx = function (key, current_event, params) {
|
|
|
2159
2127
|
error_count: 0,
|
|
2160
2128
|
cancelled: false,
|
|
2161
2129
|
/**
|
|
2162
|
-
*
|
|
2130
|
+
* 取消事件执行
|
|
2163
2131
|
*/
|
|
2164
2132
|
cancel: () => { this.cancelled = true; }
|
|
2165
2133
|
};
|
|
@@ -2391,7 +2359,7 @@ Eventer.prototype.getPausedStatus = function () {
|
|
|
2391
2359
|
return {
|
|
2392
2360
|
global_paused: this._paused_global,
|
|
2393
2361
|
paused_events: Array.from(this._paused),
|
|
2394
|
-
paused_spaces: Array.from(this._paused_space)
|
|
2362
|
+
paused_spaces: Array.from(this._paused_space)
|
|
2395
2363
|
};
|
|
2396
2364
|
};
|
|
2397
2365
|
|
package/lib/file.js
CHANGED
|
@@ -136,7 +136,7 @@ function fullname(file, dir) {
|
|
|
136
136
|
class File extends Base {
|
|
137
137
|
/**
|
|
138
138
|
* 构造函数,用于初始化文件类
|
|
139
|
-
* @param config
|
|
139
|
+
* @param {object} config 文件配置对象
|
|
140
140
|
*/
|
|
141
141
|
constructor(config) {
|
|
142
142
|
super(config);
|
|
@@ -486,8 +486,8 @@ function getFile(list, dir, keyword) {
|
|
|
486
486
|
*/
|
|
487
487
|
class Dir extends Base {
|
|
488
488
|
/**
|
|
489
|
-
*
|
|
490
|
-
* @param config
|
|
489
|
+
* 构造函数
|
|
490
|
+
* @param {object} config 目录配置对象
|
|
491
491
|
*/
|
|
492
492
|
constructor(config) {
|
|
493
493
|
super(config);
|
|
@@ -552,7 +552,6 @@ Dir.prototype.copy = function (source, target, func) {
|
|
|
552
552
|
* 删除目录
|
|
553
553
|
* @param {string} dir 目录路径
|
|
554
554
|
* @param {Function} func 回调函数
|
|
555
|
-
* @returns {boolean} 保存成功返回true,否则返回false
|
|
556
555
|
*/
|
|
557
556
|
Dir.prototype.del = function (dir, func) {
|
|
558
557
|
rimrafSync(dir.fullname(), func);
|
|
@@ -570,7 +569,7 @@ Dir.prototype.exists = function (dir) {
|
|
|
570
569
|
/** === 文件类函数 === */
|
|
571
570
|
/**
|
|
572
571
|
* 补全路径
|
|
573
|
-
* @param {string} dir
|
|
572
|
+
* @param {string} dir 文件路径
|
|
574
573
|
* @returns {string} 全路径
|
|
575
574
|
*/
|
|
576
575
|
String.prototype.fullname = function (dir) {
|
|
@@ -607,7 +606,7 @@ String.prototype.copyFile = function (file) {
|
|
|
607
606
|
/**
|
|
608
607
|
* 复制目录
|
|
609
608
|
* @param {string} file 保存路径
|
|
610
|
-
* @param func
|
|
609
|
+
* @param {Function} func 回调函数
|
|
611
610
|
* @returns {boolean} 复制成功返回true, 失败返回false
|
|
612
611
|
*/
|
|
613
612
|
String.prototype.copyDir = function (file, func) {
|
|
@@ -794,9 +793,8 @@ String.prototype.delDir = function (dir, func) {
|
|
|
794
793
|
};
|
|
795
794
|
|
|
796
795
|
/**
|
|
797
|
-
*
|
|
798
|
-
* @param {string}
|
|
799
|
-
* @param dirbase
|
|
796
|
+
* 创建路径
|
|
797
|
+
* @param {string} dirbase 目录基础路径
|
|
800
798
|
*/
|
|
801
799
|
String.prototype.addDir = function (dirbase) {
|
|
802
800
|
var d = (this + '').fullname(dirbase);
|
package/lib/global.js
CHANGED
|
@@ -10,8 +10,7 @@ Set.prototype.del = Set.prototype.delete;
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* 延迟执行(休眠)
|
|
13
|
-
* @param {number}
|
|
14
|
-
* @param milli_seconds
|
|
13
|
+
* @param {number} milli_seconds 毫秒
|
|
15
14
|
* @param {object} obj 判断对象或函数
|
|
16
15
|
* @param {string} key 判断的对象属性, 为存在的情况下提成退出循环
|
|
17
16
|
* @example var obj = {ok: false}; sleep(2000, obj, 'ok');
|
|
@@ -21,20 +20,19 @@ function sleep(milli_seconds, obj, key) {
|
|
|
21
20
|
var end_time = new Date().getTime() + milli_seconds;
|
|
22
21
|
|
|
23
22
|
if (!obj) {
|
|
24
|
-
|
|
23
|
+
simpleSleep(end_time);
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
if (key) {
|
|
28
|
-
|
|
27
|
+
sleepWithKey(end_time, obj, key);
|
|
29
28
|
} else {
|
|
30
|
-
|
|
29
|
+
sleepWithFunc(end_time, obj);
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
34
|
* 简单休眠
|
|
36
|
-
* @param {number}
|
|
37
|
-
* @param end_time
|
|
35
|
+
* @param {number} end_time 结束时间
|
|
38
36
|
*/
|
|
39
37
|
function simpleSleep(end_time) {
|
|
40
38
|
while (new Date().getTime() < end_time) {
|
|
@@ -44,8 +42,7 @@ function simpleSleep(end_time) {
|
|
|
44
42
|
|
|
45
43
|
/**
|
|
46
44
|
* 带键值检查的休眠
|
|
47
|
-
* @param {number}
|
|
48
|
-
* @param end_time
|
|
45
|
+
* @param {number} end_time 结束时间
|
|
49
46
|
* @param {object} obj 判断对象
|
|
50
47
|
* @param {string} key 判断的对象属性
|
|
51
48
|
*/
|
|
@@ -59,8 +56,7 @@ function sleepWithKey(end_time, obj, key) {
|
|
|
59
56
|
|
|
60
57
|
/**
|
|
61
58
|
* 带函数检查的休眠
|
|
62
|
-
* @param {number}
|
|
63
|
-
* @param end_time
|
|
59
|
+
* @param {number} end_time 结束时间
|
|
64
60
|
* @param {Function} func 判断函数
|
|
65
61
|
*/
|
|
66
62
|
function sleepWithFunc(end_time, func) {
|
|
@@ -73,13 +69,14 @@ function sleepWithFunc(end_time, func) {
|
|
|
73
69
|
|
|
74
70
|
/**
|
|
75
71
|
* 测试函数执行速度
|
|
76
|
-
* @param {Function
|
|
77
|
-
* @param {number} times = [value]
|
|
72
|
+
* @param {Function} func 测试的函数
|
|
73
|
+
* @param {number} times = [value] 测试次数,默认值为 10000000
|
|
74
|
+
* @returns {number} 返回耗时(毫秒)
|
|
78
75
|
*/
|
|
79
|
-
function speed(func,
|
|
80
|
-
var
|
|
76
|
+
function speed(func, times) {
|
|
77
|
+
var t = times || 1000000;
|
|
81
78
|
var t1 = (new Date()).valueOf();
|
|
82
|
-
for (var i = 0; i <
|
|
79
|
+
for (var i = 0; i < t; i++) {
|
|
83
80
|
func();
|
|
84
81
|
}
|
|
85
82
|
var t2 = (new Date()).valueOf();
|
|
@@ -164,6 +161,7 @@ if (!global.$) {
|
|
|
164
161
|
/**
|
|
165
162
|
* 加载模块
|
|
166
163
|
* @param {string} file 文件路径
|
|
164
|
+
* @returns {object} 加载后的模块对象
|
|
167
165
|
*/
|
|
168
166
|
$.load = function (file) {
|
|
169
167
|
return require(file);
|
|
@@ -172,6 +170,7 @@ if (!global.$) {
|
|
|
172
170
|
/**
|
|
173
171
|
* 卸载模块
|
|
174
172
|
* @param {string} file 文件路径
|
|
173
|
+
* @returns {string} 卸载后的模块路径字符串
|
|
175
174
|
*/
|
|
176
175
|
$.unload = function (file) {
|
|
177
176
|
var f = require.resolve(file);
|
|
@@ -182,6 +181,7 @@ if (!global.$) {
|
|
|
182
181
|
/**
|
|
183
182
|
* 重载模块
|
|
184
183
|
* @param {string} file 文件路径
|
|
184
|
+
* @returns {object} 重载后的模块对象
|
|
185
185
|
*/
|
|
186
186
|
$.reload = function (file) {
|
|
187
187
|
var f = $.unload(file);
|
package/lib/lang.js
CHANGED
|
@@ -307,10 +307,10 @@ Lang.prototype.tc = function (key, count, ...param) {
|
|
|
307
307
|
/**
|
|
308
308
|
* 格式化日期
|
|
309
309
|
* @param {Date | number} dt - 日期对象或时间戳
|
|
310
|
-
* @param {string}
|
|
310
|
+
* @param {string} format - 格式,例如 'YYYY-MM-DD hh:mm:ss'
|
|
311
311
|
* @returns {string} 格式化后的日期字符串
|
|
312
312
|
*/
|
|
313
|
-
Lang.prototype.formatDate = function (dt,
|
|
313
|
+
Lang.prototype.formatDate = function (dt, format = 'YYYY-MM-DD hh:mm:ss') {
|
|
314
314
|
// 参数校验
|
|
315
315
|
if (!dt) {
|
|
316
316
|
throw new TypeError('date不能为空');
|
|
@@ -321,7 +321,6 @@ Lang.prototype.formatDate = function (dt, _format) {
|
|
|
321
321
|
throw new TypeError('无效的日期');
|
|
322
322
|
}
|
|
323
323
|
|
|
324
|
-
var format = _format || 'YYYY-MM-DD';
|
|
325
324
|
var year = d.getFullYear();
|
|
326
325
|
var month = String(d.getMonth() + 1).padStart(2, '0');
|
|
327
326
|
var day = String(d.getDate()).padStart(2, '0');
|
|
@@ -331,8 +330,10 @@ Lang.prototype.formatDate = function (dt, _format) {
|
|
|
331
330
|
|
|
332
331
|
return format
|
|
333
332
|
.replace('YYYY', year)
|
|
333
|
+
.replace('yyyy', year)
|
|
334
334
|
.replace('MM', month)
|
|
335
335
|
.replace('DD', day)
|
|
336
|
+
.replace('dd', day)
|
|
336
337
|
.replace('HH', hours)
|
|
337
338
|
.replace('hh', hours)
|
|
338
339
|
.replace('mm', minutes)
|
|
@@ -342,19 +343,19 @@ Lang.prototype.formatDate = function (dt, _format) {
|
|
|
342
343
|
/**
|
|
343
344
|
* 格式化数字为字符串
|
|
344
345
|
* @param {number} num - 要格式化的数字
|
|
345
|
-
* @param {object} [
|
|
346
|
-
* @param {number} [
|
|
347
|
-
* @param {string} [
|
|
348
|
-
* @param {string} [
|
|
346
|
+
* @param {object} [options] - 选项
|
|
347
|
+
* @param {number} [options.decimals=2] - 小数位数
|
|
348
|
+
* @param {string} [options.decimalSeparator] - 小数分隔符
|
|
349
|
+
* @param {string} [options.thousandsSeparator] - 千位分隔符
|
|
349
350
|
* @returns {string} 格式化后的数字字符串
|
|
350
351
|
*/
|
|
351
|
-
Lang.prototype.formatNumber = function (num,
|
|
352
|
+
Lang.prototype.formatNumber = function (num, options = {}) {
|
|
352
353
|
// 参数校验
|
|
353
354
|
if (typeof num !== 'number' || isNaN(num)) {
|
|
354
355
|
throw new TypeError('num必须是有效的数字');
|
|
355
356
|
}
|
|
356
357
|
|
|
357
|
-
var opt =
|
|
358
|
+
var opt = options || {};
|
|
358
359
|
var decimals = typeof opt.decimals === 'number' ? opt.decimals : 2;
|
|
359
360
|
|
|
360
361
|
// 获取语言特定的分隔符
|
package/lib/object.js
CHANGED
|
@@ -212,7 +212,7 @@ function processObject(source_obj, target_obj, exact, depth) {
|
|
|
212
212
|
* @param {boolean} supplement 是否补充没有的对象
|
|
213
213
|
* @returns {object} 修改后的目标对象
|
|
214
214
|
*/
|
|
215
|
-
function push(target, source, supplement) {
|
|
215
|
+
function push(target, source, supplement = false) {
|
|
216
216
|
if (!target || !source || typeof target !== 'object' || typeof source !== 'object') {
|
|
217
217
|
return target;
|
|
218
218
|
}
|
|
@@ -452,11 +452,11 @@ function saveJson(source, target, format = true) {
|
|
|
452
452
|
* @param {boolean} options.cdata 是否使用CDATA
|
|
453
453
|
* @returns {string} XML字符串
|
|
454
454
|
*/
|
|
455
|
-
function saveXml(source, target,
|
|
455
|
+
function saveXml(source, target, options = {}) {
|
|
456
456
|
// 设置默认选项
|
|
457
|
-
var
|
|
458
|
-
if (typeof
|
|
459
|
-
|
|
457
|
+
var ops = options;
|
|
458
|
+
if (typeof ops !== 'object' || ops === null) {
|
|
459
|
+
ops = {};
|
|
460
460
|
}
|
|
461
461
|
|
|
462
462
|
// 默认启用格式化
|
|
@@ -464,7 +464,7 @@ function saveXml(source, target, _options = {}) {
|
|
|
464
464
|
options.format = true;
|
|
465
465
|
}
|
|
466
466
|
|
|
467
|
-
var xml_str = toXml(source,
|
|
467
|
+
var xml_str = toXml(source, ops.format, ops.attributes, ops.cdata);
|
|
468
468
|
if (typeof target === 'string') {
|
|
469
469
|
require('fs').writeFileSync(target, xml_str);
|
|
470
470
|
} else if (typeof target === 'object' && target.write) {
|
|
@@ -472,6 +472,7 @@ function saveXml(source, target, _options = {}) {
|
|
|
472
472
|
}
|
|
473
473
|
return xml_str;
|
|
474
474
|
};
|
|
475
|
+
|
|
475
476
|
/**
|
|
476
477
|
* 复制对象或值
|
|
477
478
|
* @param {any} source 源对象或值
|
|
@@ -852,7 +853,7 @@ function _setDiffTypeProp(result, key, values) {
|
|
|
852
853
|
/**
|
|
853
854
|
* 将数组转换为对象
|
|
854
855
|
* @param {Array} source 源数组
|
|
855
|
-
* @param key
|
|
856
|
+
* @param {string} key 键属性名,默认值为 'key'
|
|
856
857
|
* @returns {object} 转换后的对象
|
|
857
858
|
*/
|
|
858
859
|
function arrToObj(source, key = 'key') {
|
package/lib/ret.js
CHANGED
|
@@ -85,7 +85,7 @@ Ret.prototype._getMessage = function (code) {
|
|
|
85
85
|
* @param {boolean} diy 是否自定义错误提示
|
|
86
86
|
* @returns {object} JSON-RPC 2.0响应格式
|
|
87
87
|
*/
|
|
88
|
-
Ret.prototype._toJsonRPC = function (result, error, id,
|
|
88
|
+
Ret.prototype._toJsonRPC = function (result, error, id, diy = true) {
|
|
89
89
|
const ret = {
|
|
90
90
|
};
|
|
91
91
|
if (id) {
|
|
@@ -99,8 +99,7 @@ Ret.prototype._toJsonRPC = function (result, error, id, _diy = true) {
|
|
|
99
99
|
if (!error.code) {
|
|
100
100
|
error.code = this.config.error_code;
|
|
101
101
|
}
|
|
102
|
-
|
|
103
|
-
if (!diy) {
|
|
102
|
+
if (!(diy || this.config.message_diy)) {
|
|
104
103
|
error.message = this._getMessage(error.code) || error.message;
|
|
105
104
|
}
|
|
106
105
|
else if (!error.message) {
|
|
@@ -128,9 +127,9 @@ Ret.prototype._toJsonRPC = function (result, error, id, _diy = true) {
|
|
|
128
127
|
* @param {object.message} error.message 返回的错误提示
|
|
129
128
|
* @param {string} id 消息ID
|
|
130
129
|
* @param {boolean} diy 是否自定义错误提示
|
|
131
|
-
* @returns {object} RESTful
|
|
130
|
+
* @returns {object} RESTful响应格式对象
|
|
132
131
|
*/
|
|
133
|
-
Ret.prototype._toRESTful = function (result, error, id,
|
|
132
|
+
Ret.prototype._toRESTful = function (result, error, id, diy = true) {
|
|
134
133
|
let ret = {};
|
|
135
134
|
if (id) {
|
|
136
135
|
ret.id = id;
|
|
@@ -143,8 +142,7 @@ Ret.prototype._toRESTful = function (result, error, id, _diy = true) {
|
|
|
143
142
|
}
|
|
144
143
|
else if (error) {
|
|
145
144
|
code = error.code || this.config.error_code;
|
|
146
|
-
|
|
147
|
-
if (!diy) {
|
|
145
|
+
if (!(diy || this.config.message_diy)) {
|
|
148
146
|
msg = this._getMessage(code) || error.message || error.msg;
|
|
149
147
|
}
|
|
150
148
|
else if (!error.message && !error.msg) {
|
|
@@ -236,13 +234,12 @@ Ret.prototype.obj = function (obj, id) {
|
|
|
236
234
|
* @param {boolean} bl 布尔结果
|
|
237
235
|
* @param {string} tip 提示信息
|
|
238
236
|
* @param {string} id 消息ID
|
|
239
|
-
* @returns {object}
|
|
237
|
+
* @returns {object} 布尔响应格式对象
|
|
240
238
|
*/
|
|
241
|
-
Ret.prototype.bl = function (bl,
|
|
242
|
-
var tip = _tip || (bl ? this.config.success_message : this.config.error_message);
|
|
239
|
+
Ret.prototype.bl = function (bl, tip, id) {
|
|
243
240
|
return this.body({
|
|
244
241
|
bl,
|
|
245
|
-
tip
|
|
242
|
+
tip: tip || (bl ? this.config.success_message : this.config.error_message)
|
|
246
243
|
}, null, id);
|
|
247
244
|
};
|
|
248
245
|
|
|
@@ -270,7 +267,7 @@ Ret.prototype.add = function (res, key, value) {
|
|
|
270
267
|
/**
|
|
271
268
|
* 解析响应
|
|
272
269
|
* @param {object} res 响应对象
|
|
273
|
-
* @returns {object}
|
|
270
|
+
* @returns {object} 解析后的响应格式对象
|
|
274
271
|
*/
|
|
275
272
|
Ret.prototype.parse = function (res) {
|
|
276
273
|
let error;
|
|
@@ -321,7 +318,8 @@ Ret.prototype.parse = function (res) {
|
|
|
321
318
|
|
|
322
319
|
/**
|
|
323
320
|
* 设置响应格式
|
|
324
|
-
* @param {string} format
|
|
321
|
+
* @param {string} format 响应格式, 可选值: json, json-rpc, json-rpc3, restful
|
|
322
|
+
* @returns {Ret} 响应对象
|
|
325
323
|
*/
|
|
326
324
|
Ret.prototype.setFormat = function (format) {
|
|
327
325
|
switch (format?.toLowerCase()) {
|
package/lib/string.js
CHANGED
|
@@ -89,14 +89,14 @@ String.prototype.aesDecode = function (key, iv) {
|
|
|
89
89
|
|
|
90
90
|
/**
|
|
91
91
|
* 获取字符串的拼音
|
|
92
|
-
* @returns {string}
|
|
92
|
+
* @returns {string} 拼音
|
|
93
93
|
*/
|
|
94
94
|
String.prototype.pinyin = function () {
|
|
95
95
|
return pinyin(this).join('');
|
|
96
96
|
};
|
|
97
97
|
/**
|
|
98
98
|
* 获取字符串的拼音
|
|
99
|
-
* @returns {string}
|
|
99
|
+
* @returns {string} 拼音首字母
|
|
100
100
|
*/
|
|
101
101
|
String.prototype.pinyinS = function () {
|
|
102
102
|
let arr = pinyin(this);
|
|
@@ -251,19 +251,16 @@ String.prototype.right = function (delimiter, retain) {
|
|
|
251
251
|
};
|
|
252
252
|
/**
|
|
253
253
|
* 取文本之间
|
|
254
|
-
* @param {string}
|
|
255
|
-
* @param {string}
|
|
256
|
-
* @param left_delimiter
|
|
257
|
-
* @param left_deli
|
|
258
|
-
* @param right_delimiter
|
|
259
|
-
* @param right_deli
|
|
254
|
+
* @param {string} left_str 索引的左边字符
|
|
255
|
+
* @param {string} right_str 索引的右边字符
|
|
260
256
|
* @param {boolean} retain 当索引字符不存在时是否保留右边
|
|
261
257
|
* @returns {string} 截取后的字符串
|
|
262
258
|
*/
|
|
263
|
-
String.prototype.between = function (
|
|
264
|
-
let str = this.right(
|
|
265
|
-
return str.left(
|
|
259
|
+
String.prototype.between = function (left_str, right_str, retain) {
|
|
260
|
+
let str = this.right(left_str, retain);
|
|
261
|
+
return str.left(right_str, retain);
|
|
266
262
|
};
|
|
263
|
+
|
|
267
264
|
/**
|
|
268
265
|
* 替换所有字符串
|
|
269
266
|
* @param {string} search 被替换的字符串
|
|
@@ -473,7 +470,7 @@ String.prototype.toObj = function () {
|
|
|
473
470
|
/**
|
|
474
471
|
* 转正则表达式
|
|
475
472
|
* @param {string} mode = [g|i|gi] 转换方式, g为全部, i为不区分大小写
|
|
476
|
-
* @returns {
|
|
473
|
+
* @returns {RegExp} 返回正则对象
|
|
477
474
|
*/
|
|
478
475
|
String.prototype.toRx = function (mode) {
|
|
479
476
|
var _mode = mode || 'gi';
|
|
@@ -490,95 +487,112 @@ String.prototype.toRx = function (mode) {
|
|
|
490
487
|
// 验证函数集合
|
|
491
488
|
const validators = {
|
|
492
489
|
/**
|
|
493
|
-
*
|
|
494
|
-
* @param value
|
|
490
|
+
* 验证手机号字符串
|
|
491
|
+
* @param {string} value 手机号字符串
|
|
492
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
495
493
|
*/
|
|
496
494
|
phone: (value) => /^0?(13|14|15|16|17|18|19)[0-9]{9}$/.test(value),
|
|
497
495
|
/**
|
|
498
|
-
*
|
|
499
|
-
* @param value
|
|
496
|
+
* 验证邮箱字符串
|
|
497
|
+
* @param {string} value 邮箱字符串
|
|
498
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
500
499
|
*/
|
|
501
500
|
email: (value) => /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value),
|
|
502
501
|
/**
|
|
503
|
-
*
|
|
504
|
-
* @param value
|
|
502
|
+
* 验证用户名字符串
|
|
503
|
+
* @param {string} value 用户名字符串
|
|
504
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
505
505
|
*/
|
|
506
506
|
username: (value) => /^[a-z0-9A-Z_]+$/.test(value),
|
|
507
507
|
/**
|
|
508
|
-
*
|
|
509
|
-
* @param value
|
|
508
|
+
* 验证密码字符串
|
|
509
|
+
* @param {string} value 密码字符串
|
|
510
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
510
511
|
*/
|
|
511
512
|
password: (value) => /^[a-z0-9A-Z]+$/.test(value),
|
|
512
513
|
/**
|
|
513
|
-
*
|
|
514
|
-
* @param value
|
|
514
|
+
* 验证URL字符串
|
|
515
|
+
* @param {string} value URL字符串
|
|
516
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
515
517
|
*/
|
|
516
518
|
url: (value) =>
|
|
517
519
|
/^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/gi.test(value),
|
|
518
520
|
/**
|
|
519
|
-
*
|
|
520
|
-
* @param value
|
|
521
|
+
* 验证日期字符串
|
|
522
|
+
* @param {string} value 日期字符串
|
|
523
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
521
524
|
*/
|
|
522
525
|
date: (value) =>
|
|
523
526
|
/^\d{4}(-|\/|\.)(0[1-9]|1[012]|[1-9])(-|\/|\.)([12][0-9]|0[1-9]|3[01]|[1-9])$/.test(value),
|
|
524
527
|
/**
|
|
525
|
-
*
|
|
526
|
-
* @param value
|
|
528
|
+
* 验证时间字符串
|
|
529
|
+
* @param {string} value 时间字符串
|
|
530
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
527
531
|
*/
|
|
528
532
|
time: (value) => /^([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9])(:([0-4][0-9]|5[0-9]))?$/.test(value),
|
|
529
533
|
/**
|
|
530
|
-
*
|
|
531
|
-
* @param value
|
|
534
|
+
* 验证日期时间字符串
|
|
535
|
+
* @param {string} value 日期时间字符串
|
|
536
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
532
537
|
*/
|
|
533
538
|
datetime: (value) =>
|
|
534
539
|
new RegExp('^\\d{4}(-|\\/|\\.)(0[1-9]|1[012]|[1-9])(-|\\/|\\.)([12][0-9]|0[1-9]|3[01]|[1-9]) ' +
|
|
535
540
|
'([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9])(:([0-4][0-9]|5[0-9]))?$').test(value),
|
|
536
541
|
/**
|
|
537
|
-
*
|
|
538
|
-
* @param value
|
|
542
|
+
* 验证ISO日期字符串
|
|
543
|
+
* @param {string} value 日期字符串
|
|
544
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
539
545
|
*/
|
|
540
546
|
dateiso: (value) =>
|
|
541
547
|
new RegExp('^\\d{4}-(0[1-9]|1[012]|[1-9])-([12][0-9]|0[1-9]|3[01]|[1-9])' +
|
|
542
548
|
'(([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9])(:([0-4][0-9]|5[0-9]))?)?$').test(value),
|
|
543
549
|
/**
|
|
544
|
-
*
|
|
545
|
-
* @param value
|
|
550
|
+
* 验证数字字符串
|
|
551
|
+
* @param {string} value 数字字符串
|
|
552
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
546
553
|
*/
|
|
547
554
|
number: (value) =>
|
|
548
555
|
/^[1-9]+[0-9]*(\.[0-9]+|[0-9]*)|0\.[0-9]+|0$/.test(value),
|
|
549
556
|
/**
|
|
550
|
-
*
|
|
551
|
-
* @param value
|
|
557
|
+
* 验证英文字符串
|
|
558
|
+
* @param {string} value 英文字符串
|
|
559
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
552
560
|
*/
|
|
553
561
|
en: (value) => /^[a-zA-Z]+$/.test(value),
|
|
554
562
|
/**
|
|
555
|
-
*
|
|
556
|
-
* @param value
|
|
563
|
+
* 验证数字字符串
|
|
564
|
+
* @param {string} value 数字字符串
|
|
565
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
557
566
|
*/
|
|
558
567
|
num: (value) => /^[0-9]+$/.test(value),
|
|
559
568
|
/**
|
|
560
|
-
*
|
|
561
|
-
* @param value
|
|
569
|
+
* 验证数字字符串
|
|
570
|
+
* @param {string} value 数字字符串
|
|
571
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
562
572
|
*/
|
|
563
573
|
digits: (value) => /^[0-9]+$/.test(value),
|
|
564
574
|
/**
|
|
565
|
-
*
|
|
566
|
-
* @param value
|
|
575
|
+
* 验证中文字符串
|
|
576
|
+
* @param {string} value 中文字符串
|
|
577
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
567
578
|
*/
|
|
568
579
|
ch: (value) => /^[\u4e00-\u9fa5]+$/.test(value),
|
|
569
580
|
/**
|
|
570
|
-
*
|
|
571
|
-
* @param value
|
|
581
|
+
* 验证中文字符串
|
|
582
|
+
* @param {string} value 中文字符串
|
|
583
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
572
584
|
*/
|
|
573
585
|
chs: (value) => /^[\u4e00-\u9fa5]+$/.test(value),
|
|
574
586
|
/**
|
|
575
|
-
*
|
|
576
|
-
* @param value
|
|
587
|
+
* 验证中文字符串
|
|
588
|
+
* @param {string} value 中文字符串
|
|
589
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
577
590
|
*/
|
|
578
591
|
chinese: (value) => /^[\u4e00-\u9fa5]+$/.test(value),
|
|
579
592
|
/**
|
|
580
|
-
*
|
|
581
|
-
* @param value
|
|
593
|
+
* 验证身份证号字符串
|
|
594
|
+
* @param {string} value 身份证号字符串
|
|
595
|
+
* @returns {boolean} 验证通过返回true, 失败返回false
|
|
582
596
|
*/
|
|
583
597
|
idcard: (value) =>
|
|
584
598
|
new RegExp('^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))' +
|