mm_expand 2.1.2 → 2.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/lib/file.js CHANGED
@@ -21,7 +21,7 @@ const {
21
21
  rimrafSync
22
22
  } = require('rimraf');
23
23
  var ncp = require('ncp').ncp;
24
- require('./global.js');
24
+ const { Base } = require('./base.js');
25
25
 
26
26
  /**
27
27
  * 判断是否为文件
@@ -121,7 +121,7 @@ function fullname(file, dir) {
121
121
  // 处理相对路径
122
122
  if (file.startsWith('./') || file.startsWith('../')
123
123
 
124
- || (!isAbsolute(file) && !file.startsWith('/'))) {
124
+ || (!isAbsolute(file) && !file.startsWith('/'))) {
125
125
  return handleRelativePath(file, dir);
126
126
  }
127
127
 
@@ -132,36 +132,15 @@ function fullname(file, dir) {
132
132
  /**
133
133
  * 文件类函数
134
134
  */
135
- class File {
135
+ class File extends Base {
136
136
  /**
137
- * @description 构造函数,用于初始化文件类
138
- */
139
- constructor() {
140
- // 日志对象
141
- this._logger = $.log || console;
137
+ * @description 构造函数,用于初始化文件类
138
+ */
139
+ constructor(config) {
140
+ super(config);
142
141
  }
143
142
  }
144
143
 
145
- /**
146
- * 初始化
147
- * @param {object} logger - 日志对象
148
- */
149
- File.prototype.init = function(logger) {
150
- if (logger) {
151
- this._logger = logger;
152
- }
153
- };
154
-
155
- /**
156
- * 日志输出
157
- * @param {string} level 日志级别
158
- * @param {string} message 日志消息
159
- * @param {...any} args 日志参数
160
- */
161
- File.prototype.log = function (level, message, ...args) {
162
- this._logger[level](`[${this.constructor.name}] ${message}`, ...args);
163
- };
164
-
165
144
  /**
166
145
  * 补全文件路径
167
146
  * @param {string} file 文件路径
@@ -377,6 +356,7 @@ function getDir(list, dir, keyword) {
377
356
  }
378
357
  }
379
358
  }
359
+
380
360
  /**
381
361
  * @description 添加文件路径
382
362
  * @param {Array} list 结果路径数组
@@ -403,25 +383,22 @@ function getFile(list, dir, keyword) {
403
383
  /**
404
384
  * 目录类函数
405
385
  */
406
- class Dir {
386
+ class Dir extends Base {
407
387
  /**
408
388
  *
409
389
  */
410
- constructor() {
411
- // 构造函数,仅用于初始化
390
+ constructor(config) {
391
+ super(config);
412
392
  }
413
393
  }
414
394
 
415
395
  /**
416
396
  * 补全目录路径
417
397
  * @param {string} dir 目录路径
418
- * @param {string} baseDir 基础目录
419
- * @param base_dir
398
+ * @param {string} base_dir 基础目录
420
399
  * @returns {string} 完整路径
421
400
  */
422
- Dir.prototype.fullname = function (dir, base_dir) {
423
- return fullname(dir, base_dir);
424
- };
401
+ Dir.prototype.fullname = fullname;
425
402
 
426
403
  /**
427
404
  * @description 搜索目录下所有目录
@@ -606,7 +583,7 @@ String.prototype.saveJson = function (obj, format = true) {
606
583
  writeFileSync(this.fullname(), text);
607
584
  return true;
608
585
  } catch (e) {
609
- this._logger.error('保存JSON文件失败:', e);
586
+ $.log.error('保存JSON文件失败:', e);
610
587
  return false;
611
588
  }
612
589
  };
package/lib/global.js CHANGED
@@ -11,7 +11,7 @@ Set.prototype.del = Set.prototype.delete;
11
11
  /**
12
12
  * @description 延迟执行(休眠)
13
13
  * @param {number} milliSeconds 毫秒
14
- * @param milli_seconds
14
+ * @param milli_seconds
15
15
  * @param {object} obj 判断对象或函数
16
16
  * @param {string} key 判断的对象属性, 为存在的情况下提成退出循环
17
17
  * @example var obj = {ok: false}; sleep(2000, obj, 'ok');
@@ -34,7 +34,7 @@ function sleep(milli_seconds, obj, key) {
34
34
  /**
35
35
  * 简单休眠
36
36
  * @param {number} endTime 结束时间
37
- * @param end_time
37
+ * @param end_time
38
38
  */
39
39
  function simpleSleep(end_time) {
40
40
  while (new Date().getTime() < end_time) {
@@ -45,7 +45,7 @@ function simpleSleep(end_time) {
45
45
  /**
46
46
  * 带键值检查的休眠
47
47
  * @param {number} endTime 结束时间
48
- * @param end_time
48
+ * @param end_time
49
49
  * @param {object} obj 判断对象
50
50
  * @param {string} key 判断的对象属性
51
51
  */
@@ -60,7 +60,7 @@ function sleepWithKey(end_time, obj, key) {
60
60
  /**
61
61
  * 带函数检查的休眠
62
62
  * @param {number} endTime 结束时间
63
- * @param end_time
63
+ * @param end_time
64
64
  * @param {Function} func 判断函数
65
65
  */
66
66
  function sleepWithFunc(end_time, func) {
@@ -245,13 +245,13 @@ if (!global.$) {
245
245
  }
246
246
 
247
247
  /**
248
- * 判断对象是否相似
249
- * @param {object} source 被判断对象
250
- * @param {object} target 用作判断的对象
251
- * @param {boolean} exact 是否完全相同
252
- * @param {number} depth 递归深度
253
- * @returns {boolean} 相似返回true,否则返回false
254
- */
248
+ * 判断对象是否相似
249
+ * @param {object} source 被判断对象
250
+ * @param {object} target 用作判断的对象
251
+ * @param {boolean} exact 是否完全相同
252
+ * @param {number} depth 递归深度
253
+ * @returns {boolean} 相似返回true,否则返回false
254
+ */
255
255
  function compareObject(source, target, exact, depth) {
256
256
  if (exact &&
257
257
  Object.getOwnPropertyNames(source).length !== Object.getOwnPropertyNames(target).length) {
@@ -266,13 +266,13 @@ if (!global.$) {
266
266
  }
267
267
 
268
268
  /**
269
- * 判断对象是否相似
270
- * @param {object} source 被判断对象
271
- * @param {object} target 用作判断的对象
272
- * @param {boolean} exact 是否完全相同
273
- * @param {number} depth 递归深度,防止无限递归
274
- * @returns {boolean} 相似返回true,否则返回false
275
- */
269
+ * 判断对象是否相似
270
+ * @param {object} source 被判断对象
271
+ * @param {object} target 用作判断的对象
272
+ * @param {boolean} exact 是否完全相同
273
+ * @param {number} depth 递归深度,防止无限递归
274
+ * @returns {boolean} 相似返回true,否则返回false
275
+ */
276
276
  $.as = function (source, target, exact, depth = 0) {
277
277
  // 防止无限递归,最大递归深度为10
278
278
  if (depth > 10) {
@@ -295,10 +295,10 @@ if (!global.$) {
295
295
  };
296
296
 
297
297
  /**
298
- * 将对象转换为查询字符串
299
- * @param {object} obj 要转换的对象
300
- * @returns {string} 查询字符串
301
- */
298
+ * 将对象转换为查询字符串
299
+ * @param {object} obj 要转换的对象
300
+ * @returns {string} 查询字符串
301
+ */
302
302
  $.toQuery = function (obj) {
303
303
  if (typeof obj !== 'object' || obj === null) {
304
304
  return '';
package/lib/lang.js CHANGED
@@ -1,24 +1,23 @@
1
- require('./global.js');
1
+ const { Base } = require('./base.js');
2
2
 
3
3
  /**
4
4
  * 语言类 - 国际化(i18n)支持
5
5
  * @class
6
6
  */
7
- class Lang {
8
- static config() {
9
- return {
10
- zhCn: {},
11
- en: {},
12
- default: 'zh_cn',
13
- fallback: true
14
- };
15
- }
7
+ class Lang extends Base {
8
+ static config = {
9
+ zhCn: {},
10
+ en: {},
11
+ default: 'zh_cn',
12
+ fallback: true
13
+ };
14
+
16
15
  /**
17
- * 构造函数
18
- * @param {object} config - 配置参数
19
- */
16
+ * 构造函数
17
+ * @param {object} config - 配置参数
18
+ */
20
19
  constructor(config) {
21
- this.config = {...Lang.config, ...config || {}};
20
+ super(config);
22
21
 
23
22
  // 是否为插件
24
23
  this.is_plugin = false;
@@ -34,67 +33,36 @@ class Lang {
34
33
 
35
34
  // 复数规则配置
36
35
  this.rules = {
37
- 'en': function(count) {
36
+ 'en': function (count) {
38
37
  return count === 1 ? 0 : 1;
39
38
  },
40
- 'zhCn': function() {
39
+ 'zh_cn': function () {
41
40
  return 0;
42
41
  }
43
42
  };
44
-
45
- // 初始化默认语言
46
- this.now = this.config.default || 'zh_cn';
47
-
48
- // 日志对象
49
- this._logger = $.log || console;
50
-
51
- this.setConfig(config);
52
43
  }
53
44
  }
54
45
 
55
46
  /**
56
- * 设置配置参数
57
- * @param {object} config - 配置参数
58
- */
59
- Lang.prototype.setConfig = function(config) {
60
- if (config) {
61
- // 合并配置
62
- Object.assign(this.config, config);
63
-
64
- // 设置区域映射表
65
- if (config.locale) {
66
- this.locale = Object.assign(this.locale, config.locale);
67
- }
68
- }
69
- };
70
-
71
- /**
72
- * 初始化
73
- * @param {object} logger - 日志对象
47
+ * 预设值
74
48
  */
75
- Lang.prototype.init = function(logger) {
76
- if (logger) {
77
- this._logger = logger;
49
+ Lang.prototype._preset = function () {
50
+ // 初始化默认语言
51
+ this.now = this.config.default || 'zh_cn';
52
+ let config = this.config;
53
+ // 设置区域映射表
54
+ if (config.locale) {
55
+ this.locale = Object.assign(this.locale || {}, config.locale);
78
56
  }
79
57
  };
80
58
 
81
- /**
82
- * 日志输出
83
- * @param {string} level - 日志级别
84
- * @param {string} message - 日志消息
85
- * @param {...any} args - 日志参数
86
- */
87
- Lang.prototype.log = function(level, message, ...args) {
88
- this._logger[level](`[${this.constructor.name}] ${message}`, ...args);
89
- };
90
-
91
59
  /**
92
60
  * 获取翻译文本
93
61
  * @param {string} key - 键
94
62
  * @param {...*} param - 参数
95
63
  * @returns {string} 返回翻译后的文本
96
64
  */
97
- Lang.prototype.get = function(key, ...param) {
65
+ Lang.prototype.get = function (key, ...param) {
98
66
  // 参数校验
99
67
  if (!key || typeof key !== 'string') {
100
68
  throw new TypeError('key必须是字符串');
@@ -103,7 +71,7 @@ Lang.prototype.get = function(key, ...param) {
103
71
  var val = this._getVal(key);
104
72
 
105
73
  // 参数替换
106
- if (typeof(val) === 'string' && param.length > 0) {
74
+ if (typeof (val) === 'string' && param.length > 0) {
107
75
  for (var i = 0; i < param.length; i++) {
108
76
  var reg = new RegExp('\\{' + i + '\\}', 'ig');
109
77
  val = val.replace(reg, param[i]);
@@ -118,7 +86,7 @@ Lang.prototype.get = function(key, ...param) {
118
86
  * @param {string} key - 键
119
87
  * @returns {string | undefined} 返回语言值
120
88
  */
121
- Lang.prototype._getVal = function(key) {
89
+ Lang.prototype._getVal = function (key) {
122
90
  // 当前语言获取
123
91
  var val = this._getFrom(this.now, key);
124
92
 
@@ -151,7 +119,7 @@ Lang.prototype._getVal = function(key) {
151
119
  * @param {string} key - 键
152
120
  * @returns {*} 返回值
153
121
  */
154
- Lang.prototype._getFrom = function(lang, key) {
122
+ Lang.prototype._getFrom = function (lang, key) {
155
123
  var lang_cg = this.config[lang];
156
124
  if (!lang_cg) return undefined;
157
125
 
@@ -160,7 +128,7 @@ Lang.prototype._getFrom = function(lang, key) {
160
128
  for (var i = 0; i < arr.length; i++) {
161
129
  if (val === undefined) break;
162
130
  val = val[arr[i]];
163
- if (val === undefined || typeof(val) === 'string') {
131
+ if (val === undefined || typeof (val) === 'string') {
164
132
  break;
165
133
  }
166
134
  }
@@ -173,7 +141,7 @@ Lang.prototype._getFrom = function(lang, key) {
173
141
  * @param {string} lang - 完整语言代码
174
142
  * @returns {string} 基础语言代码
175
143
  */
176
- Lang.prototype._getBase = function(lang) {
144
+ Lang.prototype._getBase = function (lang) {
177
145
  // 检查区域映射表
178
146
  if (this.locale[lang]) {
179
147
  return this.locale[lang];
@@ -190,7 +158,7 @@ Lang.prototype._getBase = function(lang) {
190
158
  * @param {*} val - 值
191
159
  * @returns {*} 返回设置的值
192
160
  */
193
- Lang.prototype.set = function(key, val) {
161
+ Lang.prototype.set = function (key, val) {
194
162
  // 参数校验
195
163
  if (!key || typeof key !== 'string') {
196
164
  throw new TypeError('key必须是字符串');
@@ -205,7 +173,7 @@ Lang.prototype.set = function(key, val) {
205
173
  * @param {*} val - 值
206
174
  * @returns {*} 返回设置的值
207
175
  */
208
- Lang.prototype._set = function(key, val) {
176
+ Lang.prototype._set = function (key, val) {
209
177
  var lang = this.config[this.now];
210
178
  if (!lang) {
211
179
  this.log('error', '当前语言未初始化: ' + this.now);
@@ -225,7 +193,7 @@ Lang.prototype._set = function(key, val) {
225
193
  // 创建中间对象
226
194
  obj[k] = {};
227
195
  obj = obj[k];
228
- } else if (typeof(v) === 'object' && !Array.isArray(v)) {
196
+ } else if (typeof (v) === 'object' && !Array.isArray(v)) {
229
197
  obj = v;
230
198
  } else {
231
199
  // 遇到非对象值,创建新对象覆盖
@@ -241,7 +209,7 @@ Lang.prototype._set = function(key, val) {
241
209
  * @param {string} lang - 语言代码
242
210
  * @returns {string} 返回切换后的语言代码
243
211
  */
244
- Lang.prototype.setLang = function(lang) {
212
+ Lang.prototype.setLang = function (lang) {
245
213
  // 参数校验
246
214
  if (!lang || typeof lang !== 'string') {
247
215
  throw new TypeError('lang必须是字符串');
@@ -269,7 +237,7 @@ Lang.prototype.setLang = function(lang) {
269
237
  * @param {string} lang - 语言代码
270
238
  * @returns {boolean} 是否删除成功
271
239
  */
272
- Lang.prototype.del = function(lang) {
240
+ Lang.prototype.del = function (lang) {
273
241
  // 参数校验
274
242
  if (!lang || typeof lang !== 'string') {
275
243
  throw new TypeError('lang必须是字符串');
@@ -288,7 +256,7 @@ Lang.prototype.del = function(lang) {
288
256
  * @param {...*} param - 参数
289
257
  * @returns {string} 返回翻译后的文本
290
258
  */
291
- Lang.prototype.t = function(key, ...param) {
259
+ Lang.prototype.t = function (key, ...param) {
292
260
  return this.get(key, ...param);
293
261
  };
294
262
 
@@ -299,7 +267,7 @@ Lang.prototype.t = function(key, ...param) {
299
267
  * @param {...*} param - 额外参数
300
268
  * @returns {string} 返回翻译后的文本
301
269
  */
302
- Lang.prototype.tc = function(key, count, ...param) {
270
+ Lang.prototype.tc = function (key, count, ...param) {
303
271
  // 参数校验
304
272
  if (!key || typeof key !== 'string') {
305
273
  throw new TypeError('key必须是字符串');
@@ -323,7 +291,7 @@ Lang.prototype.tc = function(key, count, ...param) {
323
291
  }
324
292
 
325
293
  // 替换参数,第一个参数是数量
326
- if (typeof(val) === 'string') {
294
+ if (typeof (val) === 'string') {
327
295
  // 先替换 {count} 占位符
328
296
  val = val.replace(/\{count\}/ig, count);
329
297
 
@@ -343,7 +311,7 @@ Lang.prototype.tc = function(key, count, ...param) {
343
311
  * @param {string} [format='YYYY-MM-DD'] - 格式
344
312
  * @returns {string} 格式化后的日期字符串
345
313
  */
346
- Lang.prototype.formatDate = function(dt, format) {
314
+ Lang.prototype.formatDate = function (dt, format) {
347
315
  // 参数校验
348
316
  if (!dt) {
349
317
  throw new TypeError('date不能为空');
@@ -381,7 +349,7 @@ Lang.prototype.formatDate = function(dt, format) {
381
349
  * @param {string} [opt.thousandsSeparator] - 千位分隔符
382
350
  * @returns {string} 格式化后的数字字符串
383
351
  */
384
- Lang.prototype.formatNumber = function(num, opt) {
352
+ Lang.prototype.formatNumber = function (num, opt) {
385
353
  // 参数校验
386
354
  if (typeof num !== 'number' || isNaN(num)) {
387
355
  throw new TypeError('num必须是有效的数字');
@@ -406,7 +374,7 @@ Lang.prototype.formatNumber = function(num, opt) {
406
374
  * @param {string} file - 文件路径
407
375
  * @returns {Promise<object>} 加载的语言包
408
376
  */
409
- Lang.prototype.load = async function(lang, file) {
377
+ Lang.prototype.load = async function (lang, file) {
410
378
  // 参数校验
411
379
  if (!lang || typeof lang !== 'string') {
412
380
  throw new TypeError('lang必须是字符串');
@@ -439,7 +407,7 @@ Lang.prototype.load = async function(lang, file) {
439
407
  * @param {Function} rule - 复数规则函数
440
408
  * @returns {boolean} 是否注册成功
441
409
  */
442
- Lang.prototype.registerPlural = function(lang, rule) {
410
+ Lang.prototype.registerPlural = function (lang, rule) {
443
411
  // 参数校验
444
412
  if (!lang || typeof lang !== 'string') {
445
413
  throw new TypeError('lang必须是字符串');
@@ -456,12 +424,12 @@ Lang.prototype.registerPlural = function(lang, rule) {
456
424
  * 获取支持的语言列表
457
425
  * @returns {Array<string>} 语言代码数组
458
426
  */
459
- Lang.prototype.getLangs = function() {
427
+ Lang.prototype.getLangs = function () {
460
428
  var langs = [];
461
429
  for (var key in this.config) {
462
430
  // 排除非语言配置项
463
431
  if (key !== 'default' && key !== 'fallback' &&
464
- key !== 'options' && typeof this.config[key] === 'object') {
432
+ key !== 'options' && typeof this.config[key] === 'object') {
465
433
  langs.push(key);
466
434
  }
467
435
  }
@@ -473,7 +441,7 @@ Lang.prototype.getLangs = function() {
473
441
  * @param {string} lang - 语言代码
474
442
  * @returns {boolean} 是否支持
475
443
  */
476
- Lang.prototype.isSupported = function(lang) {
444
+ Lang.prototype.isSupported = function (lang) {
477
445
  return !!this.config[lang] || !!this.config[this._getBase(lang)];
478
446
  };
479
447
 
package/lib/logger.js CHANGED
@@ -15,21 +15,38 @@ if (!console.fatal) {
15
15
  * 日志类
16
16
  */
17
17
  class Logger extends Event {
18
+ /**
19
+ * 日志对象
20
+ * @type {object} - 日志对象, 默认值: console
21
+ */
22
+ #logger = console;
23
+
18
24
  /**
19
25
  * 构造函数
20
26
  */
21
27
  constructor() {
22
28
  super();
23
29
  /**
24
- * 是否为插件日志
25
- * @type {boolean} - 是否为插件日志, 默认值: false
26
- */
27
- this.is_plugin = false;
28
- /**
29
- * 日志对象
30
- * @type {object} - 日志对象, 默认值: console
30
+ * 是否为插件日志
31
+ * @type {boolean} - 是否为插件日志, 默认值: false
31
32
  */
32
- this._logger = console;
33
+ this.is_plugin = false;
34
+ }
35
+
36
+ /**
37
+ * 设置日志记录器
38
+ * @param {object} logger 日志记录器对象
39
+ */
40
+ setLogger(logger) {
41
+ this.#logger = logger;
42
+ }
43
+
44
+ /**
45
+ * 获取日志记录器
46
+ * @returns {object} 日志记录器对象
47
+ */
48
+ getLogger() {
49
+ return this.#logger;
33
50
  }
34
51
  }
35
52
 
@@ -46,7 +63,7 @@ Logger.prototype.debug = function (message, ...args) {
46
63
  };
47
64
  try {
48
65
  this.emit('debug:before', ctx);
49
- this._logger.debug(ctx.message, ...ctx.args);
66
+ this.getLogger().debug(ctx.message, ...ctx.args);
50
67
  this.emit('debug:after', ctx);
51
68
  } catch (error) {
52
69
  // 日志错误处理
@@ -67,11 +84,11 @@ Logger.prototype.info = function (message, ...args) {
67
84
  };
68
85
  try {
69
86
  this.emit('info:before', ctx);
70
- this._logger.info(ctx.message.blue, ...ctx.args);
87
+ this.getLogger().info(ctx.message.blue, ...ctx.args);
71
88
  this.emit('info:after', ctx);
72
89
  } catch (error) {
73
90
  // 日志错误处理
74
- this._logger.error(error);
91
+ this.getLogger().error(error);
75
92
  }
76
93
  };
77
94
 
@@ -88,11 +105,11 @@ Logger.prototype.success = function (message, ...args) {
88
105
  };
89
106
  try {
90
107
  this.emit('success:before', ctx);
91
- this._logger.success(ctx.message.green, ...ctx.args);
108
+ this.getLogger().success(ctx.message.green, ...ctx.args);
92
109
  this.emit('success:after', ctx);
93
110
  } catch (error) {
94
111
  // 日志错误处理
95
- this._logger.error(error);
112
+ this.getLogger().error(error);
96
113
  }
97
114
  };
98
115
 
@@ -109,7 +126,7 @@ Logger.prototype.warn = function (message, ...args) {
109
126
  };
110
127
  try {
111
128
  this.emit('warn:before', ctx);
112
- this._logger.warn(ctx.message.yellow, ...ctx.args);
129
+ this.getLogger().warn(ctx.message.yellow, ...ctx.args);
113
130
  this.emit('warn:after', ctx);
114
131
  } catch (error) {
115
132
  // 日志错误处理
@@ -130,11 +147,11 @@ Logger.prototype.error = function (message, ...args) {
130
147
  };
131
148
  try {
132
149
  this.emit('error:before', ctx);
133
- this._logger.error(ctx.message.red, ...ctx.args);
150
+ this.getLogger().error(ctx.message.red, ...ctx.args);
134
151
  this.emit('error:after', ctx);
135
152
  } catch (error) {
136
153
  // 日志错误处理
137
- this._logger.error(error);
154
+ this.getLogger().error(error);
138
155
  }
139
156
  };
140
157
 
@@ -151,11 +168,11 @@ Logger.prototype.fatal = function (message, ...args) {
151
168
  };
152
169
  try {
153
170
  this.emit('fatal:before', ctx);
154
- this._logger.fatal(ctx.message.magenta, ...ctx.args);
171
+ this.getLogger().fatal(ctx.message.magenta, ...ctx.args);
155
172
  this.emitAsync('fatal:after', ctx);
156
173
  } catch (error) {
157
174
  // 日志错误处理
158
- this._logger.error(error);
175
+ this.getLogger().error(error);
159
176
  }
160
177
  };
161
178