better-console-group 1.0.2 → 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.
@@ -1,5 +1,11 @@
1
- type ConsoleMethod = 'log' | 'warn' | 'error' | 'debug' | 'info';
2
- type ConsoleMethodWithParams = [ConsoleMethod, unknown, ...unknown[]] | ['group', string] | ['groupEnd'];
1
+ type ConsoleMethod = 'log' | 'warn' | 'error' | 'debug' | 'info' | 'table' | 'trace' | 'assert' | 'time' | 'timeEnd' | 'dir';
2
+ type ConsoleMethodParams = {
3
+ [Method in ConsoleMethod]: Parameters<Console[Method]>;
4
+ };
5
+ type ConsoleMethodCall = {
6
+ [Method in ConsoleMethod]: [Method, ...ConsoleMethodParams[Method]];
7
+ }[ConsoleMethod];
8
+ type ConsoleMethodWithParams = ConsoleMethodCall | ['group', string] | ['groupEnd'];
3
9
  type AsyncConsoleGroupBuffer = Array<ConsoleMethodWithParams>;
4
10
  /**
5
11
  * A private class. Each of its instance represents a grouping of console messages.
@@ -41,13 +47,51 @@ declare class AsyncConsoleGroup {
41
47
  */
42
48
  error(message?: unknown, ...optionalParams: unknown[]): void;
43
49
  /**
44
- * Alias for group.log.
50
+ * Equivalent of console.debug when used inside a group.
51
+ * @param message The message to log.
52
+ * @param optionalParams Optional parameters to log.
45
53
  */
46
54
  debug(message?: unknown, ...optionalParams: unknown[]): void;
47
55
  /**
48
- * Alias for group.log.
56
+ * Equivalent of console.info when used inside a group.
57
+ * @param message The message to log.
58
+ * @param optionalParams Optional parameters to log.
49
59
  */
50
60
  info(message?: unknown, ...optionalParams: unknown[]): void;
61
+ /**
62
+ * Equivalent of console.table when used inside a group.
63
+ * @param tabularData The data to display as a table.
64
+ * @param properties Optional list of property names to include in the table.
65
+ */
66
+ table(tabularData?: unknown, properties?: string[]): void;
67
+ /**
68
+ * Equivalent of console.trace when used inside a group.
69
+ * @param data Optional values to include with the stack trace output.
70
+ */
71
+ trace(...data: unknown[]): void;
72
+ /**
73
+ * Equivalent of console.assert when used inside a group.
74
+ * @param condition Condition to assert.
75
+ * @param message Optional message to log when the assertion fails.
76
+ * @param optionalParams Optional parameters to log when the assertion fails.
77
+ */
78
+ assert(condition?: boolean, message?: string, ...optionalParams: unknown[]): void;
79
+ /**
80
+ * Equivalent of console.time when used inside a group.
81
+ * @param label Optional timer label.
82
+ */
83
+ time(label?: string): void;
84
+ /**
85
+ * Equivalent of console.timeEnd when used inside a group.
86
+ * @param label Optional timer label.
87
+ */
88
+ timeEnd(label?: string): void;
89
+ /**
90
+ * Equivalent of console.dir when used inside a group.
91
+ * @param item The value to inspect.
92
+ * @param options Optional inspection options.
93
+ */
94
+ dir(item?: unknown, options?: object): void;
51
95
  /**
52
96
  * You should not call this method directly. It is called automatically when the async callback finishes.
53
97
  * @private
package/lib/asyncGroup.js CHANGED
@@ -18,7 +18,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
18
18
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
19
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
20
  };
21
- var _AsyncConsoleGroup_buffer, _AsyncConsoleGroup_ended;
21
+ var _AsyncConsoleGroup_instances, _AsyncConsoleGroup_buffer, _AsyncConsoleGroup_ended, _AsyncConsoleGroup_push;
22
22
  /**
23
23
  * A private class. Each of its instance represents a grouping of console messages.
24
24
  * You will receive an instance of this class when you call asyncGroup.
@@ -30,6 +30,7 @@ var _AsyncConsoleGroup_buffer, _AsyncConsoleGroup_ended;
30
30
  */
31
31
  class AsyncConsoleGroup {
32
32
  constructor(buffer) {
33
+ _AsyncConsoleGroup_instances.add(this);
33
34
  _AsyncConsoleGroup_buffer.set(this, void 0);
34
35
  _AsyncConsoleGroup_ended.set(this, false);
35
36
  __classPrivateFieldSet(this, _AsyncConsoleGroup_buffer, buffer, "f");
@@ -59,9 +60,7 @@ class AsyncConsoleGroup {
59
60
  * @param optionalParams Optional parameters to log.
60
61
  */
61
62
  log(message, ...optionalParams) {
62
- if (!__classPrivateFieldGet(this, _AsyncConsoleGroup_ended, "f")) {
63
- __classPrivateFieldGet(this, _AsyncConsoleGroup_buffer, "f").push(['log', message, ...optionalParams]);
64
- }
63
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'log', message, ...optionalParams);
65
64
  }
66
65
  /**
67
66
  * Equivalent of console.warn when used inside a group.
@@ -69,9 +68,7 @@ class AsyncConsoleGroup {
69
68
  * @param optionalParams Optional parameters to log.
70
69
  */
71
70
  warn(message, ...optionalParams) {
72
- if (!__classPrivateFieldGet(this, _AsyncConsoleGroup_ended, "f")) {
73
- __classPrivateFieldGet(this, _AsyncConsoleGroup_buffer, "f").push(['warn', message, ...optionalParams]);
74
- }
71
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'warn', message, ...optionalParams);
75
72
  }
76
73
  /**
77
74
  * Equivalent of console.error when used inside a group.
@@ -79,21 +76,69 @@ class AsyncConsoleGroup {
79
76
  * @param optionalParams Optional parameters to log.
80
77
  */
81
78
  error(message, ...optionalParams) {
82
- if (!__classPrivateFieldGet(this, _AsyncConsoleGroup_ended, "f")) {
83
- __classPrivateFieldGet(this, _AsyncConsoleGroup_buffer, "f").push(['error', message, ...optionalParams]);
84
- }
79
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'error', message, ...optionalParams);
85
80
  }
86
81
  /**
87
- * Alias for group.log.
82
+ * Equivalent of console.debug when used inside a group.
83
+ * @param message The message to log.
84
+ * @param optionalParams Optional parameters to log.
88
85
  */
89
86
  debug(message, ...optionalParams) {
90
- this.log(message, ...optionalParams);
87
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'debug', message, ...optionalParams);
91
88
  }
92
89
  /**
93
- * Alias for group.log.
90
+ * Equivalent of console.info when used inside a group.
91
+ * @param message The message to log.
92
+ * @param optionalParams Optional parameters to log.
94
93
  */
95
94
  info(message, ...optionalParams) {
96
- this.log(message, ...optionalParams);
95
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'info', message, ...optionalParams);
96
+ }
97
+ /**
98
+ * Equivalent of console.table when used inside a group.
99
+ * @param tabularData The data to display as a table.
100
+ * @param properties Optional list of property names to include in the table.
101
+ */
102
+ table(tabularData, properties) {
103
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'table', tabularData, properties);
104
+ }
105
+ /**
106
+ * Equivalent of console.trace when used inside a group.
107
+ * @param data Optional values to include with the stack trace output.
108
+ */
109
+ trace(...data) {
110
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'trace', ...data);
111
+ }
112
+ /**
113
+ * Equivalent of console.assert when used inside a group.
114
+ * @param condition Condition to assert.
115
+ * @param message Optional message to log when the assertion fails.
116
+ * @param optionalParams Optional parameters to log when the assertion fails.
117
+ */
118
+ assert(condition, message, ...optionalParams) {
119
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'assert', condition, message, ...optionalParams);
120
+ }
121
+ /**
122
+ * Equivalent of console.time when used inside a group.
123
+ * @param label Optional timer label.
124
+ */
125
+ time(label) {
126
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'time', label);
127
+ }
128
+ /**
129
+ * Equivalent of console.timeEnd when used inside a group.
130
+ * @param label Optional timer label.
131
+ */
132
+ timeEnd(label) {
133
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'timeEnd', label);
134
+ }
135
+ /**
136
+ * Equivalent of console.dir when used inside a group.
137
+ * @param item The value to inspect.
138
+ * @param options Optional inspection options.
139
+ */
140
+ dir(item, options) {
141
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_instances, "m", _AsyncConsoleGroup_push).call(this, 'dir', item, options);
97
142
  }
98
143
  /**
99
144
  * You should not call this method directly. It is called automatically when the async callback finishes.
@@ -105,7 +150,11 @@ class AsyncConsoleGroup {
105
150
  return __classPrivateFieldGet(this, _AsyncConsoleGroup_buffer, "f");
106
151
  }
107
152
  }
108
- _AsyncConsoleGroup_buffer = new WeakMap(), _AsyncConsoleGroup_ended = new WeakMap();
153
+ _AsyncConsoleGroup_buffer = new WeakMap(), _AsyncConsoleGroup_ended = new WeakMap(), _AsyncConsoleGroup_instances = new WeakSet(), _AsyncConsoleGroup_push = function _AsyncConsoleGroup_push(method, ...params) {
154
+ if (!__classPrivateFieldGet(this, _AsyncConsoleGroup_ended, "f")) {
155
+ __classPrivateFieldGet(this, _AsyncConsoleGroup_buffer, "f").push([method, ...params]);
156
+ }
157
+ };
109
158
  /**
110
159
  * The async version of betterGroup, which is a better version of console.group and console.groupEnd pair.
111
160
  * It allows you to use async/await inside the group and still have the console output correctly grouped together.
@@ -126,15 +175,71 @@ export function asyncGroup(label, callbackFn, thisArg) {
126
175
  const buffer = group.end();
127
176
  console.group(label);
128
177
  while (buffer.length > 0) {
129
- const [method, message, ...optionalParams] = buffer.shift(); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
130
- if (method === 'group') {
131
- console.group(message);
132
- }
133
- else if (method === 'groupEnd') {
134
- console.groupEnd();
135
- }
136
- else {
137
- console[method](message, ...optionalParams);
178
+ const [...methodAndParams] = buffer.shift(); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
179
+ switch (methodAndParams[0]) {
180
+ case 'group': {
181
+ const [, nestedLabel] = methodAndParams;
182
+ console.group(nestedLabel);
183
+ break;
184
+ }
185
+ case 'groupEnd':
186
+ console.groupEnd();
187
+ break;
188
+ case 'log': {
189
+ const [, ...params] = methodAndParams;
190
+ console.log(...params);
191
+ break;
192
+ }
193
+ case 'warn': {
194
+ const [, ...params] = methodAndParams;
195
+ console.warn(...params);
196
+ break;
197
+ }
198
+ case 'error': {
199
+ const [, ...params] = methodAndParams;
200
+ console.error(...params);
201
+ break;
202
+ }
203
+ case 'debug': {
204
+ const [, ...params] = methodAndParams;
205
+ console.debug(...params);
206
+ break;
207
+ }
208
+ case 'info': {
209
+ const [, ...params] = methodAndParams;
210
+ console.info(...params);
211
+ break;
212
+ }
213
+ case 'table': {
214
+ const [, ...params] = methodAndParams;
215
+ console.table(...params);
216
+ break;
217
+ }
218
+ case 'trace': {
219
+ const [, ...params] = methodAndParams;
220
+ console.trace(...params);
221
+ break;
222
+ }
223
+ case 'assert': {
224
+ const [, ...params] = methodAndParams;
225
+ console.assert(...params);
226
+ break;
227
+ }
228
+ case 'time': {
229
+ const [, ...params] = methodAndParams;
230
+ console.time(...params);
231
+ break;
232
+ }
233
+ case 'timeEnd': {
234
+ const [, ...params] = methodAndParams;
235
+ console.timeEnd(...params);
236
+ break;
237
+ }
238
+ case 'dir': {
239
+ const [, ...params] = methodAndParams;
240
+ console.dir(...params);
241
+ break;
242
+ }
138
243
  }
139
244
  }
140
245
  console.groupEnd();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-console-group",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Better API for console.group.",
5
5
  "funding": "https://github.com/CatChen/versioned-storage?sponsor=1",
6
6
  "type": "module",