@qqi/log 1.0.0 → 1.1.0

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/README.md CHANGED
@@ -32,6 +32,16 @@ dog('你好'); // 该值是否打印还依赖于环境变量中有没有配置 `
32
32
  a_node_tools_dev=true npm run dev
33
33
  ```
34
34
 
35
+ ### 折叠同名方法打印消息
36
+
37
+ 使用 `fold` 参数 :
38
+
39
+ ```js
40
+ import { Dog } from '@qqi/log';
41
+
42
+ const dog = Dog();
43
+ ```
44
+
35
45
  ## 文档地址
36
46
 
37
47
  参看 [https://earthnut.dev/npm/qqi/log](https://earthnut.dev/npm/qqi/log)
package/cjs/index.js CHANGED
@@ -1,92 +1,137 @@
1
1
  'use strict';
2
2
 
3
- var aJsTools = require('a-js-tools');
4
- var setType = require('./setType.js');
5
- var blankCall = require('./blankCall.js');
6
- var managePrint = require('./managePrint.js');
7
- var aTypeOfJs = require('a-type-of-js');
8
- var platform = require('./platform.js');
9
3
  var _static = require('@color-pen/static');
4
+ var aJsTools = require('a-js-tools');
5
+ var colorPen = require('color-pen');
6
+ var util = require('./util.js');
10
7
 
11
8
  /**
12
9
  *
13
- * 创建 dev log 工厂函数
10
+ * @param options 配置项
11
+ * @returns 函数对象
14
12
  *
13
+ */
14
+ var DogConstructor = aJsTools.createConstructor(Dog);
15
+ /**
16
+ * ## 创建 dev log 工厂函数
15
17
  * @param options - 配置项
16
18
  * @returns - dev log 工厂函数
17
19
  */
18
20
  function Dog(options) {
19
- if (aTypeOfJs.isUndefined(options))
20
- options = {
21
- name: aJsTools.getRandomString(10),
22
- type: false,
23
- };
24
- if (aTypeOfJs.isBoolean(options))
25
- options = {
26
- name: aJsTools.getRandomString(10),
27
- type: options,
28
- };
29
- if (aTypeOfJs.isString(options))
30
- options = {
31
- name: options,
32
- type: false,
33
- };
34
- let { name = '', type = false } = options;
35
- // 处理 name
36
- name = name.trim().replace(/\s+/g, '_');
37
- /** 当前获取环境值 */
38
- const _env = (platform.platform === 'node' &&
39
- (globalThis?.process.env[name.toUpperCase().concat('_DEV')] ??
40
- globalThis?.process.env[name.toLowerCase().concat('_dev')])) ||
41
- false;
42
- const env = _env === 'false' ? false : _env === 'true' ? true : _env;
21
+ var _this = this;
22
+ var _p = util.parseOption(options);
23
+ this.name = _p.name || aJsTools.getRandomString(12);
24
+ this.fold = Boolean(_p.fold);
25
+ var env = util.getEnv(this.name);
26
+ var type = _p.type || false;
27
+ this.mark = '';
43
28
  /** 默认 node 环境以获取到的环境值为准,而非 node 环境默认开启,并通过自定义的 @qqi/babel-plugin-remove-dog-calls 来进行过滤正式环境(环境值需要自定义) */
44
- type = platform.platform === 'node' ? setType.setType(env ?? type) : true;
45
- /// 原始的调用方法,在 type 值变化时会触发该值的更替
46
- // 私有方法 error
47
- const _privateFunc = {
48
- error: blankCall.blankCall,
49
- warn: blankCall.blankCall,
50
- info: blankCall.blankCall,
29
+ this.type = util.platform === 'node' ? util.setType(env !== null && env !== void 0 ? env : type) : true;
30
+ /**
31
+ * ## 解析 error
32
+ * @param type
33
+ */
34
+ var prefix = function (type) {
35
+ var _a, _b, _c, _d, _e, _f, _g;
36
+ try {
37
+ throw new Error();
38
+ }
39
+ catch (error) {
40
+ var parseErrorResult = (((_a = error.stack) === null || _a === void 0 ? void 0 : _a.split('\n')) || []).map(function (item) {
41
+ var reg = /at\s(.*)\s\((.*):(\d*):(\d*)\)/;
42
+ var res = reg.exec(item);
43
+ if (res) {
44
+ return {
45
+ name: res[1],
46
+ path: res[2],
47
+ line: res[3],
48
+ column: res[4],
49
+ };
50
+ }
51
+ return {
52
+ name: '',
53
+ };
54
+ });
55
+ var result = parseErrorResult.filter(function (e) { return e.name !== '' && e.path !== undefined; });
56
+ var res = (_d = (_c = (_b = result[3]) !== null && _b !== void 0 ? _b : result[2]) !== null && _c !== void 0 ? _c : result[1]) !== null && _d !== void 0 ? _d : result[0];
57
+ var startStr = " ".concat(type === 'info' ? '💡' : type === 'error' ? '❌' : '⚠️ ', " ").concat(new Date().toLocaleString(), " ");
58
+ var printStartPenStr = (type === 'info'
59
+ ? colorPen.bgCyanPen.brightWhite
60
+ : type === 'error'
61
+ ? colorPen.bgBlackPen.red
62
+ : colorPen.bgBrightYellowPen.brightGreen)(startStr);
63
+ var mark = (_e = res === null || res === void 0 ? void 0 : res.name) !== null && _e !== void 0 ? _e : '';
64
+ if (_this.fold && mark) {
65
+ if (mark === _this.mark) ;
66
+ else {
67
+ if (_this.mark) {
68
+ console.groupEnd();
69
+ }
70
+ console.groupCollapsed(mark);
71
+ }
72
+ }
73
+ _this.mark = mark;
74
+ return "".concat(printStartPenStr, " ").concat(mark, " ").concat((_f = res === null || res === void 0 ? void 0 : res.line) === null || _f === void 0 ? void 0 : _f.concat(' 行'), " ").concat((_g = res === null || res === void 0 ? void 0 : res.column) === null || _g === void 0 ? void 0 : _g.concat(' 列'));
75
+ }
76
+ };
77
+ this.info = function () {
78
+ var msg = [];
79
+ for (var _i = 0; _i < arguments.length; _i++) {
80
+ msg[_i] = arguments[_i];
81
+ }
82
+ if (_this.type === 'all' || _this.type === 'info' || _this.type === true) {
83
+ var _prefix = prefix('info');
84
+ msg.unshift(_prefix);
85
+ console.log.apply(console, msg);
86
+ }
87
+ };
88
+ /**
89
+ * @param msg
90
+ */
91
+ this.warn = function () {
92
+ var msg = [];
93
+ for (var _i = 0; _i < arguments.length; _i++) {
94
+ msg[_i] = arguments[_i];
95
+ }
96
+ if (_this.type === 'all' || _this.type === 'warn' || _this.type === true) {
97
+ var _prefix = prefix('warn');
98
+ msg.unshift(_prefix);
99
+ console.warn.apply(console, msg);
100
+ }
51
101
  };
52
- managePrint.managePrint(type, _privateFunc, name);
53
- /** 本体方法 */
54
- const dog = (...str) => {
55
- Reflect.apply(_privateFunc.info, this, str);
102
+ /**
103
+ *
104
+ * @param msg
105
+ */
106
+ this.error = function () {
107
+ var msg = [];
108
+ for (var _i = 0; _i < arguments.length; _i++) {
109
+ msg[_i] = arguments[_i];
110
+ }
111
+ if (_this.type === 'all' || _this.type === 'error' || _this.type === true) {
112
+ var _prefix = prefix('error');
113
+ msg.unshift(_prefix);
114
+ console.error.apply(console, msg);
115
+ }
116
+ };
117
+ /**
118
+ * 本体方法
119
+ * @param str
120
+ */
121
+ var dog = function () {
122
+ var str = [];
123
+ for (var _i = 0; _i < arguments.length; _i++) {
124
+ str[_i] = arguments[_i];
125
+ }
126
+ _this.info(str);
56
127
  };
57
128
  // 设置 prototype
58
129
  Object.setPrototypeOf(dog, this);
59
- // 设置属性和方法
60
- Object.defineProperties(this, {
61
- type: {
62
- get() {
63
- return type || false;
64
- },
65
- set(value) {
66
- const new_type = setType.setType(value);
67
- if (new_type !== type) {
68
- type = new_type;
69
- managePrint.managePrint(type, _privateFunc, name);
70
- }
71
- },
72
- },
73
- error: {
74
- value: (...str) => Reflect.apply(_privateFunc.error, this, str),
75
- configurable: false,
76
- enumerable: false,
77
- writable: false,
78
- },
79
- warn: {
80
- value: (...str) => Reflect.apply(_privateFunc.warn, this, str),
81
- configurable: false,
82
- enumerable: false,
83
- writable: false,
84
- },
85
- });
86
130
  return dog;
87
131
  }
88
- Dog.prototype.clear = () => {
89
- if (platform.platform === 'browser') {
132
+ /** 原型上添加 clear 方法 */
133
+ Dog.prototype.clear = function () {
134
+ if (util.platform === 'browser') {
90
135
  console.clear();
91
136
  }
92
137
  else {
@@ -94,11 +139,47 @@ Dog.prototype.clear = () => {
94
139
  }
95
140
  };
96
141
  /**
97
- *
98
- * @param options 配置项
99
- * @returns 函数对象
100
142
  *
101
143
  */
102
- const DogConstructor = aJsTools.createConstructor(Dog);
144
+ function DogVirtualImt() {
145
+ /**
146
+ * 模拟类的构建
147
+ * @param _arg
148
+ */
149
+ var _dev = function () {
150
+ };
151
+ Object.setPrototypeOf(_dev, this);
152
+ Object.defineProperties(this, {
153
+ info: {
154
+ value: function () {
155
+ },
156
+ configurable: false,
157
+ writable: false,
158
+ },
159
+ warn: {
160
+ value: function () {
161
+ },
162
+ configurable: false,
163
+ writable: false,
164
+ },
165
+ error: {
166
+ value: function () {
167
+ },
168
+ configurable: false,
169
+ writable: false,
170
+ },
171
+ type: {
172
+ get: function () {
173
+ return false;
174
+ },
175
+ set: function (_) { },
176
+ },
177
+ });
178
+ return _dev;
179
+ }
180
+ DogVirtualImt.prototype.clear = console.clear;
181
+ /** 虚拟狗,没有实现不打印 */
182
+ var DogVirtualConstructor = aJsTools.createConstructor(DogVirtualImt);
103
183
 
104
184
  exports.Dog = DogConstructor;
185
+ exports.DogVirtual = DogVirtualConstructor;
package/cjs/util.js ADDED
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+
3
+ var aJsTools = require('a-js-tools');
4
+ var aTypeOfJs = require('a-type-of-js');
5
+
6
+ /**
7
+ * @packageDocumentation
8
+ * @module @qqi/log/util
9
+ * @file util.ts
10
+ * @description 工具
11
+ * @author MrMudBean <Mr.MudBean@outlook.com>
12
+ * @license MIT
13
+ * @copyright 2026 ©️ MrMudBean
14
+ * @since 2026-01-28 03:11
15
+ * @version 1.0.1
16
+ * @lastModified 2026-01-28 04:34
17
+ */
18
+ var typeList = [
19
+ false,
20
+ true,
21
+ 'all',
22
+ 'info',
23
+ 'error',
24
+ 'warn',
25
+ ];
26
+ /**
27
+ * ## 解析参数
28
+ * @param options
29
+ */
30
+ function parseOption(options) {
31
+ var result = {
32
+ name: aJsTools.getRandomString(10),
33
+ type: false,
34
+ fold: false,
35
+ };
36
+ if (aTypeOfJs.isUndefined(options))
37
+ return result;
38
+ if (aTypeOfJs.isBoolean(options)) {
39
+ result.type = options;
40
+ return result;
41
+ }
42
+ if (aTypeOfJs.isString(options)) {
43
+ // 处理 name
44
+ result.name = options.trim().replace(/\s+/g, '_');
45
+ return result;
46
+ }
47
+ if (aTypeOfJs.isString(options.name)) {
48
+ result.name = options.name.trim().replace(/\s+/g, '_');
49
+ return result;
50
+ }
51
+ return options;
52
+ }
53
+ /**
54
+ * 获取当前的环境变量
55
+ * @param name
56
+ */
57
+ function getEnv(name) {
58
+ var _a;
59
+ if (name === void 0) { name = aJsTools.getRandomString(10); }
60
+ /** 当前获取环境值 */
61
+ var _env = false;
62
+ if (platform === 'node' && ((_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.process) === null || _a === void 0 ? void 0 : _a.env)) {
63
+ var processEnv = process.env;
64
+ _env =
65
+ processEnv[name.toUpperCase().concat('_DEV')] ||
66
+ processEnv[name.toLowerCase().concat('_dev')] ||
67
+ false;
68
+ }
69
+ var env = _env === 'false' ? false : _env === 'true' ? true : _env;
70
+ return env;
71
+ }
72
+ var platform = aJsTools.isNode() ? 'node' : 'browser';
73
+ /**
74
+ * 设置 type 的类型
75
+ * @param type 新的类型
76
+ */
77
+ function setType(type) {
78
+ if (typeList.includes(type))
79
+ return type;
80
+ return false;
81
+ }
82
+
83
+ exports.getEnv = getEnv;
84
+ exports.parseOption = parseOption;
85
+ exports.platform = platform;
86
+ exports.setType = setType;
87
+ exports.typeList = typeList;