mm_expand 2.3.4 → 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/file.js CHANGED
@@ -6,7 +6,8 @@ const {
6
6
  statSync,
7
7
  readdirSync,
8
8
  mkdirSync,
9
- unlinkSync
9
+ unlinkSync,
10
+ promises: fsp
10
11
  } = require('fs');
11
12
  const {
12
13
  join,
@@ -135,6 +136,7 @@ function fullname(file, dir) {
135
136
  class File extends Base {
136
137
  /**
137
138
  * 构造函数,用于初始化文件类
139
+ * @param config
138
140
  */
139
141
  constructor(config) {
140
142
  super(config);
@@ -171,6 +173,75 @@ File.prototype.getAll = function (dir, keyword, keyword_dir) {
171
173
  return list;
172
174
  };
173
175
 
176
+ /**
177
+ * 搜索目录下所有文件
178
+ * @param {string} dir 目录地址
179
+ * @param {string} keyword 搜索关键词
180
+ * @param {string} keyword_dir 目录搜素关键词
181
+ * @returns {Array} 文件路径数组
182
+ */
183
+ File.prototype.getAll = function (dir, keyword, keyword_dir) {
184
+ // 文件数组
185
+ var list = [];
186
+
187
+ // 目录数组
188
+ var dirs = [];
189
+ // 获取相关目录
190
+ eachDir(dirs, dir.fullname(), keyword_dir);
191
+ var len = dirs.length;
192
+ for (var i = 0; i < len; i++) {
193
+ getFile(list, dirs[i], keyword);
194
+ }
195
+ return list;
196
+ };
197
+
198
+ /**
199
+ * 搜索目录下所有文件(异步并行极速版)
200
+ * @param {string} dir 目录地址
201
+ * @param {string} keyword 搜索关键词
202
+ * @param {string} keyword_dir 目录搜素关键词
203
+ * @returns {Array} 文件路径数组
204
+ */
205
+ File.prototype.getAllAsync = async function (dir, keyword, keyword_dir) {
206
+ var root_dir = dir.fullname();
207
+ var matchedDirs = [];
208
+
209
+ // 使用独立 eachDirAsync 并行收集匹配的目录
210
+ await eachDirAsync(matchedDirs, root_dir, keyword_dir);
211
+
212
+ /**
213
+ * 异步获取目录下的匹配文件
214
+ * @param {string} directory 目录路径
215
+ * @returns {Array} 文件路径数组
216
+ */
217
+ async function getFileAsync(directory) {
218
+ var items;
219
+ try {
220
+ items = await fsp.readdir(directory, { with_file_types: true });
221
+ } catch {
222
+ return [];
223
+ }
224
+
225
+ var files = [];
226
+ for (var i = 0; i < items.length; i++) {
227
+ var item = items[i];
228
+ if (item.isFile() && (!keyword || item.name.has(keyword))) {
229
+ files.push(join(directory, item.name));
230
+ }
231
+ }
232
+ return files;
233
+ }
234
+
235
+ // 没有匹配的子目录时,直接搜索根目录
236
+ if (matchedDirs.length === 0) {
237
+ return await getFileAsync(root_dir);
238
+ }
239
+
240
+ // 并行处理所有匹配目录的文件收集
241
+ var file_lists = await Promise.all(matchedDirs.map(getFileAsync));
242
+ return file_lists.flat();
243
+ };
244
+
174
245
  /**
175
246
  * 获取当前目录下所有文件
176
247
  * @param {string} dir 目录地址
@@ -218,11 +289,9 @@ File.prototype.save = function (file, data, encode) {
218
289
  if (data === undefined || data === null) {
219
290
  throw new Error('data参数不能为空');
220
291
  }
221
- if (!encode) {
222
- encode = 'utf-8';
223
- }
292
+ var enc = encode || 'utf-8';
224
293
  try {
225
- writeFileSync(file, data, encode);
294
+ writeFileSync(file, data, enc);
226
295
  return true;
227
296
  } catch (e) {
228
297
  this.log('error', '保存文件失败:', e);
@@ -289,10 +358,8 @@ File.prototype.exists = function (file) {
289
358
  * @returns {string} 文件内容
290
359
  */
291
360
  File.prototype.read = function (file, encode) {
292
- if (!encode) {
293
- encode = 'utf-8';
294
- }
295
- return this.load(file, encode);
361
+ var enc = encode || 'utf-8';
362
+ return this.load(file, enc);
296
363
  };
297
364
 
298
365
  /**
@@ -333,6 +400,40 @@ function eachDir(list, dir, keyword) {
333
400
  }
334
401
  }
335
402
  }
403
+ /**
404
+ * 异步并行搜索目录下所有目录
405
+ * @param {Array} list 结果路径数组
406
+ * @param {string} dir 目录地址
407
+ * @param {string} keyword 搜索关键词
408
+ */
409
+ async function eachDirAsync(list, dir, keyword) {
410
+ var items;
411
+ try {
412
+ items = await fsp.readdir(dir, { with_file_types: true });
413
+ } catch {
414
+ return;
415
+ }
416
+
417
+ var subDirs = [];
418
+ for (var i = 0; i < items.length; i++) {
419
+ var item = items[i];
420
+ if (item.isDirectory()) {
421
+ var full_path = join(dir, item.name);
422
+ if (!keyword || item.name.has(keyword)) {
423
+ list.push(full_path);
424
+ }
425
+ subDirs.push(full_path);
426
+ }
427
+ }
428
+
429
+ // 并行遍历所有子目录
430
+ if (subDirs.length > 0) {
431
+ await Promise.all(subDirs.map((d) => {
432
+ return eachDirAsync(list, d, keyword);
433
+ }));
434
+ }
435
+ }
436
+
336
437
  /**
337
438
  * 获取当前目录下所有目录
338
439
  * @param {Array} list 结果路径数组
@@ -386,6 +487,7 @@ function getFile(list, dir, keyword) {
386
487
  class Dir extends Base {
387
488
  /**
388
489
  *
490
+ * @param config
389
491
  */
390
492
  constructor(config) {
391
493
  super(config);
@@ -412,6 +514,18 @@ Dir.prototype.getAll = function (dir, keyword) {
412
514
  return list;
413
515
  };
414
516
 
517
+ /**
518
+ * 搜索目录下所有目录(异步并行极速版)
519
+ * @param {string} dir 目录地址
520
+ * @param {string} keyword 搜索关键词
521
+ * @returns {Array} 目录路径数组
522
+ */
523
+ Dir.prototype.getAllAsync = async function (dir, keyword) {
524
+ var list = [];
525
+ await eachDirAsync(list, this.fullname(dir), keyword);
526
+ return list;
527
+ };
528
+
415
529
  /**
416
530
  * 搜索当前目录下所有目录
417
531
  * @param {string} dir 目录地址
@@ -597,10 +711,8 @@ String.prototype.saveJson = function (obj, format = true, space = 2) {
597
711
  String.prototype.loadText = function (encode) {
598
712
  var file = this.fullname();
599
713
  if (existsSync(file)) {
600
- if (!encode) {
601
- encode = 'utf-8';
602
- }
603
- return readFileSync(file, encode);
714
+ var enc = encode || 'utf-8';
715
+ return readFileSync(file, enc);
604
716
  } else {
605
717
  return undefined;
606
718
  }
@@ -660,7 +772,7 @@ String.prototype.hasFile = function (file) {
660
772
  * @param {string} file 当前路径
661
773
  */
662
774
  String.prototype.delFile = function (file) {
663
- unlinkSync(this.fullname(file), function (e) { });
775
+ unlinkSync(this.fullname(file), (e) => { });
664
776
  };
665
777
 
666
778
  /**
package/lib/global.js CHANGED
@@ -9,7 +9,7 @@ Map.prototype.del = Map.prototype.delete;
9
9
  Set.prototype.del = Set.prototype.delete;
10
10
 
11
11
  /**
12
- * @description 延迟执行(休眠)
12
+ * 延迟执行(休眠)
13
13
  * @param {number} milliSeconds 毫秒
14
14
  * @param milli_seconds
15
15
  * @param {object} obj 判断对象或函数
@@ -72,14 +72,12 @@ function sleepWithFunc(end_time, func) {
72
72
  }
73
73
 
74
74
  /**
75
- * @description 测试函数执行速度
75
+ * 测试函数执行速度
76
76
  * @param {Function()} func 测试的函数
77
77
  * @param {number} times = [value] 测试次数
78
78
  */
79
- function speed(func, times) {
80
- if (!times) {
81
- times = 1000000;
82
- }
79
+ function speed(func, _times) {
80
+ var times = _times || 1000000;
83
81
  var t1 = (new Date()).valueOf();
84
82
  for (var i = 0; i < times; i++) {
85
83
  func();
@@ -110,10 +108,10 @@ if (!global.$) {
110
108
  scope: 'sys'
111
109
  },
112
110
  /**
113
- * @description 字典,用于查询变量替换名
114
- * @property {string} session_id session的ID
115
- * @property {string} user_id 用户的ID,用于数据库时查询用户唯一标识
116
- */
111
+ * 字典,用于查询变量替换名
112
+ * @property {string} session_id session的ID
113
+ * @property {string} user_id 用户的ID,用于数据库时查询用户唯一标识
114
+ */
117
115
  dict: {
118
116
  session_id: 'mm:uuid',
119
117
  user_id: 'user_id',
@@ -130,19 +128,19 @@ if (!global.$) {
130
128
  });
131
129
 
132
130
  /**
133
- * 运行代码
134
- * @param {string} code 代码字符串
135
- * @param {object} cm 上下文参数
136
- * @returns {string} 返回运行结果
137
- */
131
+ * 运行代码
132
+ * @param {string} code 代码字符串
133
+ * @param {object} cm 上下文参数
134
+ * @returns {string} 返回运行结果
135
+ */
138
136
  $.runCode = async function (code, cm) {
139
137
  var script = `async function runCode() {
140
- var body;
141
-
142
- ${code}
143
-
144
- return body;
145
- }`;
138
+ var body;
139
+
140
+ ${code}
141
+
142
+ return body;
143
+ }`;
146
144
  eval(script);
147
145
  var func = eval('runCode');
148
146
  var result = await func();
@@ -150,11 +148,11 @@ if (!global.$) {
150
148
  };
151
149
 
152
150
  /**
153
- * 运行脚本
154
- * @param {string} file 文件路径
155
- * @param {object} cm common公共对象
156
- * @returns {string} 返回运行结果
157
- */
151
+ * 运行脚本
152
+ * @param {string} file 文件路径
153
+ * @param {object} cm common公共对象
154
+ * @returns {string} 返回运行结果
155
+ */
158
156
  $.runScript = async function (file, cm) {
159
157
  if (file.hasFile()) {
160
158
  return await $.runCode(file.loadText(), cm);
@@ -164,17 +162,17 @@ if (!global.$) {
164
162
  };
165
163
 
166
164
  /**
167
- * 加载模块
168
- * @param {string} file 文件路径
169
- */
165
+ * 加载模块
166
+ * @param {string} file 文件路径
167
+ */
170
168
  $.load = function (file) {
171
169
  return require(file);
172
170
  };
173
171
 
174
172
  /**
175
- * 卸载模块
176
- * @param {string} file 文件路径
177
- */
173
+ * 卸载模块
174
+ * @param {string} file 文件路径
175
+ */
178
176
  $.unload = function (file) {
179
177
  var f = require.resolve(file);
180
178
  delete require.cache[f];
@@ -182,33 +180,33 @@ if (!global.$) {
182
180
  };
183
181
 
184
182
  /**
185
- * 重载模块
186
- * @param {string} file 文件路径
187
- */
183
+ * 重载模块
184
+ * @param {string} file 文件路径
185
+ */
188
186
  $.reload = function (file) {
189
187
  var f = $.unload(file);
190
188
  return $.load(f);
191
189
  };
192
190
 
193
191
  /**
194
- * 获取对象详细信息
195
- * @param {object} obj 对象
196
- * @returns {string} 对象详细信息
197
- */
192
+ * 获取对象详细信息
193
+ * @param {object} obj 对象
194
+ * @returns {string} 对象详细信息
195
+ */
198
196
  $.info = function (obj) {
199
197
  const { inspect } = require('util');
200
198
  return inspect(obj, {
201
- showHidden: false,
199
+ show_hidden: false,
202
200
  depth: null
203
201
  });
204
202
  };
205
203
 
206
204
  /**
207
- * 判断基本类型是否相等
208
- * @param {*} source 被判断对象
209
- * @param {*} target 用作判断的对象
210
- * @returns {boolean} 相等返回true,否则返回false
211
- */
205
+ * 判断基本类型是否相等
206
+ * @param {*} source 被判断对象
207
+ * @param {*} target 用作判断的对象
208
+ * @returns {boolean} 相等返回true,否则返回false
209
+ */
212
210
  function compareBasic(source, target) {
213
211
  var type = typeof (source);
214
212
  if (type !== typeof (target)) {
@@ -224,13 +222,13 @@ if (!global.$) {
224
222
  }
225
223
 
226
224
  /**
227
- * 判断数组是否相似
228
- * @param {Array} source 被判断数组
229
- * @param {Array} target 用作判断的数组
230
- * @param {boolean} exact 是否完全相同
231
- * @param {number} depth 递归深度
232
- * @returns {boolean} 相似返回true,否则返回false
233
- */
225
+ * 判断数组是否相似
226
+ * @param {Array} source 被判断数组
227
+ * @param {Array} target 用作判断的数组
228
+ * @param {boolean} exact 是否完全相同
229
+ * @param {number} depth 递归深度
230
+ * @returns {boolean} 相似返回true,否则返回false
231
+ */
234
232
  function compareArray(source, target, exact, depth) {
235
233
  var length = source.length;
236
234
  if (exact && length !== target.length) {
@@ -280,7 +278,7 @@ if (!global.$) {
280
278
  }
281
279
 
282
280
  // 先判断基本类型
283
- var basic_result = compareBasic(source, target);
281
+ compareBasic(source, target);
284
282
  if (basic_result !== null) {
285
283
  return basic_result;
286
284
  }
package/lib/lang.js CHANGED
@@ -24,10 +24,10 @@ class Lang extends Base {
24
24
 
25
25
  // 复数规则配置
26
26
  this.rules = {
27
- 'en': function (count) {
27
+ en(count) {
28
28
  return count === 1 ? 0 : 1;
29
29
  },
30
- 'zh_cn': function () {
30
+ zhCn() {
31
31
  return 0;
32
32
  }
33
33
  };
@@ -310,7 +310,7 @@ Lang.prototype.tc = function (key, count, ...param) {
310
310
  * @param {string} [format='YYYY-MM-DD'] - 格式
311
311
  * @returns {string} 格式化后的日期字符串
312
312
  */
313
- Lang.prototype.formatDate = function (dt, format) {
313
+ Lang.prototype.formatDate = function (dt, _format) {
314
314
  // 参数校验
315
315
  if (!dt) {
316
316
  throw new TypeError('date不能为空');
@@ -321,7 +321,7 @@ Lang.prototype.formatDate = function (dt, format) {
321
321
  throw new TypeError('无效的日期');
322
322
  }
323
323
 
324
- format = format || 'YYYY-MM-DD';
324
+ var format = _format || 'YYYY-MM-DD';
325
325
  var year = d.getFullYear();
326
326
  var month = String(d.getMonth() + 1).padStart(2, '0');
327
327
  var day = String(d.getDate()).padStart(2, '0');
@@ -348,23 +348,23 @@ Lang.prototype.formatDate = function (dt, format) {
348
348
  * @param {string} [opt.thousandsSeparator] - 千位分隔符
349
349
  * @returns {string} 格式化后的数字字符串
350
350
  */
351
- Lang.prototype.formatNumber = function (num, opt) {
351
+ Lang.prototype.formatNumber = function (num, _opt) {
352
352
  // 参数校验
353
353
  if (typeof num !== 'number' || isNaN(num)) {
354
354
  throw new TypeError('num必须是有效的数字');
355
355
  }
356
356
 
357
- opt = opt || {};
357
+ var opt = _opt || {};
358
358
  var decimals = typeof opt.decimals === 'number' ? opt.decimals : 2;
359
359
 
360
360
  // 获取语言特定的分隔符
361
- var decimal_separator = opt.decimalSeparator || '.';
362
- var thousands_separator = opt.thousandsSeparator || ',';
361
+ var decimal_sepa = opt.decimalSeparator || '.';
362
+ var thou_sepa = opt.thousandsSeparator || ',';
363
363
 
364
364
  // 格式化数字
365
365
  var parts = num.toFixed(decimals).split('.');
366
- parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousands_separator);
367
- return parts.join(decimal_separator);
366
+ parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thou_sepa);
367
+ return parts.join(decimal_sepa);
368
368
  };
369
369
 
370
370
  /**
package/lib/logger.js CHANGED
@@ -22,8 +22,8 @@ class Logger extends Event {
22
22
  #logger = console;
23
23
 
24
24
  /**
25
- * 构造函数
26
- */
25
+ * 构造函数
26
+ */
27
27
  constructor() {
28
28
  super();
29
29
  /**
package/lib/number.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /* == 数字原型函数 == */
2
2
  /**
3
- * @description 去尾法
3
+ * 去尾法
4
4
  * @param {number} len 保留长度
5
5
  * @returns {number} 数值
6
6
  */
@@ -9,7 +9,7 @@ Number.prototype.toFloor = function (len) {
9
9
  return Math.floor(this * n) / n;
10
10
  };
11
11
  /**
12
- * @description 进一法
12
+ * 进一法
13
13
  * @param {number} len 保留长度
14
14
  * @returns {number} 数值
15
15
  */
@@ -18,7 +18,7 @@ Number.prototype.toCeil = function (len) {
18
18
  return Math.ceil(this * n) / n;
19
19
  };
20
20
  /**
21
- * @description 四舍五入法
21
+ * 四舍五入法
22
22
  * @param {number} len 保留长度
23
23
  * @returns {number} 数值
24
24
  */
@@ -28,7 +28,7 @@ Number.prototype.toRound = function (len) {
28
28
  };
29
29
 
30
30
  /**
31
- * @description 转为时间类型
31
+ * 转为时间类型
32
32
  * @returns {Date} 时间对象
33
33
  */
34
34
  Number.prototype.toTime = function () {
@@ -36,7 +36,7 @@ Number.prototype.toTime = function () {
36
36
  };
37
37
 
38
38
  /**
39
- * @description 转为时间戳
39
+ * 转为时间戳
40
40
  * @returns {number} 返回时间戳
41
41
  */
42
42
  Number.prototype.toTimestamp = function () {
@@ -44,7 +44,7 @@ Number.prototype.toTimestamp = function () {
44
44
  };
45
45
 
46
46
  /**
47
- * @description 转为时间
47
+ * 转为时间
48
48
  * @param {string} format 时间格式
49
49
  * @returns {string} 时间字符串
50
50
  */
@@ -53,7 +53,7 @@ Number.prototype.toTimeStr = function (format = 'yyyy-MM-dd hh:mm:ss') {
53
53
  };
54
54
 
55
55
  /**
56
- * @description 随机数生成
56
+ * 随机数生成
57
57
  * @param {number} min 最小值
58
58
  * @returns {number} 返回随机数值
59
59
  */
@@ -62,7 +62,7 @@ Number.prototype.random = function (min = 1) {
62
62
  };
63
63
 
64
64
  /**
65
- * @description 随机数范围生成
65
+ * 随机数范围生成
66
66
  * @param {number} margin 上下幅度
67
67
  * @returns {number} 返回范围随机数值
68
68
  */
@@ -73,8 +73,8 @@ Number.prototype.randomRange = function (margin = 5) {
73
73
  };
74
74
 
75
75
  /**
76
- * @description 导出Number原型扩展
76
+ * 导出Number原型扩展
77
77
  */
78
78
  module.exports = {
79
79
  Number
80
- };
80
+ };