mm_expand 2.1.1 → 2.1.3

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
@@ -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
 
@@ -133,34 +133,39 @@ function fullname(file, dir) {
133
133
  * 文件类函数
134
134
  */
135
135
  class File {
136
+ /**
137
+ * @description 日志对象
138
+ */
139
+ #logger = $.log || console;
136
140
  /**
137
141
  * @description 构造函数,用于初始化文件类
138
142
  */
139
143
  constructor() {
140
- // 日志对象
141
- this._logger = $.log || console;
142
144
  }
143
- }
144
-
145
- /**
146
- * 初始化
147
- * @param {object} logger - 日志对象
148
- */
149
- File.prototype.init = function(logger) {
150
- if (logger) {
151
- this._logger = logger;
145
+ /**
146
+ * @description 设置日志对象
147
+ * @param {object} logger 日志对象
148
+ */
149
+ setLogger(logger) {
150
+ this.#logger = logger;
152
151
  }
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
- };
152
+ /**
153
+ * @description 获取日志对象
154
+ * @returns {object} 日志对象
155
+ */
156
+ getLogger() {
157
+ return this.#logger;
158
+ }
159
+ /**
160
+ * @description 日志输出
161
+ * @param {string} level 日志级别
162
+ * @param {string} message 日志消息
163
+ * @param {...any} args 日志参数
164
+ */
165
+ log(level, message, ...args) {
166
+ this.getLogger()[level](`[${this.constructor.name}] ${message}`, ...args);
167
+ }
168
+ }
164
169
 
165
170
  /**
166
171
  * 补全文件路径
@@ -606,7 +611,7 @@ String.prototype.saveJson = function (obj, format = true) {
606
611
  writeFileSync(this.fullname(), text);
607
612
  return true;
608
613
  } catch (e) {
609
- this._logger.error('保存JSON文件失败:', e);
614
+ $.log.error('保存JSON文件失败:', e);
610
615
  return false;
611
616
  }
612
617
  };
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,52 @@ 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
43
 
45
44
  // 初始化默认语言
46
45
  this.now = this.config.default || 'zh_cn';
47
-
48
- // 日志对象
49
- this._logger = $.log || console;
50
-
51
- this.setConfig(config);
52
46
  }
53
47
  }
54
48
 
55
- /**
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
- };
49
+ // /**
50
+ // * 设置配置参数
51
+ // * @param {object} config - 配置参数
52
+ // */
53
+ // Lang.prototype.setConfig = function(config) {
54
+ // if (config) {
55
+ // // 合并配置
56
+ // Object.assign(this.config, config);
57
+
58
+ // // 设置区域映射表
59
+ // if (config.locale) {
60
+ // this.locale = Object.assign(this.locale, config.locale);
61
+ // }
62
+ // }
63
+ // };
70
64
 
71
65
  /**
72
66
  * 初始化
73
67
  * @param {object} logger - 日志对象
74
68
  */
75
- Lang.prototype.init = function(logger) {
69
+ Lang.prototype.init = function (logger) {
76
70
  if (logger) {
77
71
  this._logger = logger;
78
72
  }
79
73
  };
80
74
 
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
75
  /**
92
76
  * 获取翻译文本
93
77
  * @param {string} key - 键
94
78
  * @param {...*} param - 参数
95
79
  * @returns {string} 返回翻译后的文本
96
80
  */
97
- Lang.prototype.get = function(key, ...param) {
81
+ Lang.prototype.get = function (key, ...param) {
98
82
  // 参数校验
99
83
  if (!key || typeof key !== 'string') {
100
84
  throw new TypeError('key必须是字符串');
@@ -103,7 +87,7 @@ Lang.prototype.get = function(key, ...param) {
103
87
  var val = this._getVal(key);
104
88
 
105
89
  // 参数替换
106
- if (typeof(val) === 'string' && param.length > 0) {
90
+ if (typeof (val) === 'string' && param.length > 0) {
107
91
  for (var i = 0; i < param.length; i++) {
108
92
  var reg = new RegExp('\\{' + i + '\\}', 'ig');
109
93
  val = val.replace(reg, param[i]);
@@ -118,7 +102,7 @@ Lang.prototype.get = function(key, ...param) {
118
102
  * @param {string} key - 键
119
103
  * @returns {string | undefined} 返回语言值
120
104
  */
121
- Lang.prototype._getVal = function(key) {
105
+ Lang.prototype._getVal = function (key) {
122
106
  // 当前语言获取
123
107
  var val = this._getFrom(this.now, key);
124
108
 
@@ -151,7 +135,7 @@ Lang.prototype._getVal = function(key) {
151
135
  * @param {string} key - 键
152
136
  * @returns {*} 返回值
153
137
  */
154
- Lang.prototype._getFrom = function(lang, key) {
138
+ Lang.prototype._getFrom = function (lang, key) {
155
139
  var lang_cg = this.config[lang];
156
140
  if (!lang_cg) return undefined;
157
141
 
@@ -160,7 +144,7 @@ Lang.prototype._getFrom = function(lang, key) {
160
144
  for (var i = 0; i < arr.length; i++) {
161
145
  if (val === undefined) break;
162
146
  val = val[arr[i]];
163
- if (val === undefined || typeof(val) === 'string') {
147
+ if (val === undefined || typeof (val) === 'string') {
164
148
  break;
165
149
  }
166
150
  }
@@ -173,7 +157,7 @@ Lang.prototype._getFrom = function(lang, key) {
173
157
  * @param {string} lang - 完整语言代码
174
158
  * @returns {string} 基础语言代码
175
159
  */
176
- Lang.prototype._getBase = function(lang) {
160
+ Lang.prototype._getBase = function (lang) {
177
161
  // 检查区域映射表
178
162
  if (this.locale[lang]) {
179
163
  return this.locale[lang];
@@ -190,7 +174,7 @@ Lang.prototype._getBase = function(lang) {
190
174
  * @param {*} val - 值
191
175
  * @returns {*} 返回设置的值
192
176
  */
193
- Lang.prototype.set = function(key, val) {
177
+ Lang.prototype.set = function (key, val) {
194
178
  // 参数校验
195
179
  if (!key || typeof key !== 'string') {
196
180
  throw new TypeError('key必须是字符串');
@@ -205,7 +189,7 @@ Lang.prototype.set = function(key, val) {
205
189
  * @param {*} val - 值
206
190
  * @returns {*} 返回设置的值
207
191
  */
208
- Lang.prototype._set = function(key, val) {
192
+ Lang.prototype._set = function (key, val) {
209
193
  var lang = this.config[this.now];
210
194
  if (!lang) {
211
195
  this.log('error', '当前语言未初始化: ' + this.now);
@@ -225,7 +209,7 @@ Lang.prototype._set = function(key, val) {
225
209
  // 创建中间对象
226
210
  obj[k] = {};
227
211
  obj = obj[k];
228
- } else if (typeof(v) === 'object' && !Array.isArray(v)) {
212
+ } else if (typeof (v) === 'object' && !Array.isArray(v)) {
229
213
  obj = v;
230
214
  } else {
231
215
  // 遇到非对象值,创建新对象覆盖
@@ -241,7 +225,7 @@ Lang.prototype._set = function(key, val) {
241
225
  * @param {string} lang - 语言代码
242
226
  * @returns {string} 返回切换后的语言代码
243
227
  */
244
- Lang.prototype.setLang = function(lang) {
228
+ Lang.prototype.setLang = function (lang) {
245
229
  // 参数校验
246
230
  if (!lang || typeof lang !== 'string') {
247
231
  throw new TypeError('lang必须是字符串');
@@ -269,7 +253,7 @@ Lang.prototype.setLang = function(lang) {
269
253
  * @param {string} lang - 语言代码
270
254
  * @returns {boolean} 是否删除成功
271
255
  */
272
- Lang.prototype.del = function(lang) {
256
+ Lang.prototype.del = function (lang) {
273
257
  // 参数校验
274
258
  if (!lang || typeof lang !== 'string') {
275
259
  throw new TypeError('lang必须是字符串');
@@ -288,7 +272,7 @@ Lang.prototype.del = function(lang) {
288
272
  * @param {...*} param - 参数
289
273
  * @returns {string} 返回翻译后的文本
290
274
  */
291
- Lang.prototype.t = function(key, ...param) {
275
+ Lang.prototype.t = function (key, ...param) {
292
276
  return this.get(key, ...param);
293
277
  };
294
278
 
@@ -299,7 +283,7 @@ Lang.prototype.t = function(key, ...param) {
299
283
  * @param {...*} param - 额外参数
300
284
  * @returns {string} 返回翻译后的文本
301
285
  */
302
- Lang.prototype.tc = function(key, count, ...param) {
286
+ Lang.prototype.tc = function (key, count, ...param) {
303
287
  // 参数校验
304
288
  if (!key || typeof key !== 'string') {
305
289
  throw new TypeError('key必须是字符串');
@@ -323,7 +307,7 @@ Lang.prototype.tc = function(key, count, ...param) {
323
307
  }
324
308
 
325
309
  // 替换参数,第一个参数是数量
326
- if (typeof(val) === 'string') {
310
+ if (typeof (val) === 'string') {
327
311
  // 先替换 {count} 占位符
328
312
  val = val.replace(/\{count\}/ig, count);
329
313
 
@@ -343,7 +327,7 @@ Lang.prototype.tc = function(key, count, ...param) {
343
327
  * @param {string} [format='YYYY-MM-DD'] - 格式
344
328
  * @returns {string} 格式化后的日期字符串
345
329
  */
346
- Lang.prototype.formatDate = function(dt, format) {
330
+ Lang.prototype.formatDate = function (dt, format) {
347
331
  // 参数校验
348
332
  if (!dt) {
349
333
  throw new TypeError('date不能为空');
@@ -381,7 +365,7 @@ Lang.prototype.formatDate = function(dt, format) {
381
365
  * @param {string} [opt.thousandsSeparator] - 千位分隔符
382
366
  * @returns {string} 格式化后的数字字符串
383
367
  */
384
- Lang.prototype.formatNumber = function(num, opt) {
368
+ Lang.prototype.formatNumber = function (num, opt) {
385
369
  // 参数校验
386
370
  if (typeof num !== 'number' || isNaN(num)) {
387
371
  throw new TypeError('num必须是有效的数字');
@@ -406,7 +390,7 @@ Lang.prototype.formatNumber = function(num, opt) {
406
390
  * @param {string} file - 文件路径
407
391
  * @returns {Promise<object>} 加载的语言包
408
392
  */
409
- Lang.prototype.load = async function(lang, file) {
393
+ Lang.prototype.load = async function (lang, file) {
410
394
  // 参数校验
411
395
  if (!lang || typeof lang !== 'string') {
412
396
  throw new TypeError('lang必须是字符串');
@@ -439,7 +423,7 @@ Lang.prototype.load = async function(lang, file) {
439
423
  * @param {Function} rule - 复数规则函数
440
424
  * @returns {boolean} 是否注册成功
441
425
  */
442
- Lang.prototype.registerPlural = function(lang, rule) {
426
+ Lang.prototype.registerPlural = function (lang, rule) {
443
427
  // 参数校验
444
428
  if (!lang || typeof lang !== 'string') {
445
429
  throw new TypeError('lang必须是字符串');
@@ -456,12 +440,12 @@ Lang.prototype.registerPlural = function(lang, rule) {
456
440
  * 获取支持的语言列表
457
441
  * @returns {Array<string>} 语言代码数组
458
442
  */
459
- Lang.prototype.getLangs = function() {
443
+ Lang.prototype.getLangs = function () {
460
444
  var langs = [];
461
445
  for (var key in this.config) {
462
446
  // 排除非语言配置项
463
447
  if (key !== 'default' && key !== 'fallback' &&
464
- key !== 'options' && typeof this.config[key] === 'object') {
448
+ key !== 'options' && typeof this.config[key] === 'object') {
465
449
  langs.push(key);
466
450
  }
467
451
  }
@@ -473,7 +457,7 @@ Lang.prototype.getLangs = function() {
473
457
  * @param {string} lang - 语言代码
474
458
  * @returns {boolean} 是否支持
475
459
  */
476
- Lang.prototype.isSupported = function(lang) {
460
+ Lang.prototype.isSupported = function (lang) {
477
461
  return !!this.config[lang] || !!this.config[this._getBase(lang)];
478
462
  };
479
463
 
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