@taicode/common-base 3.7.4 → 3.7.7

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.
Files changed (167) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +7 -8
  3. package/output/batching-buffer/batching-buffer.d.ts +0 -133
  4. package/output/batching-buffer/batching-buffer.d.ts.map +0 -1
  5. package/output/batching-buffer/batching-buffer.js +0 -264
  6. package/output/batching-buffer/batching-buffer.test.d.ts +0 -2
  7. package/output/batching-buffer/batching-buffer.test.d.ts.map +0 -1
  8. package/output/batching-buffer/batching-buffer.test.js +0 -358
  9. package/output/batching-buffer/index.d.ts +0 -3
  10. package/output/batching-buffer/index.d.ts.map +0 -1
  11. package/output/batching-buffer/index.js +0 -1
  12. package/output/catch/catch.d.ts +0 -46
  13. package/output/catch/catch.d.ts.map +0 -1
  14. package/output/catch/catch.js +0 -48
  15. package/output/catch/catch.test.d.ts +0 -2
  16. package/output/catch/catch.test.d.ts.map +0 -1
  17. package/output/catch/catch.test.js +0 -43
  18. package/output/catch/index.d.ts +0 -2
  19. package/output/catch/index.d.ts.map +0 -1
  20. package/output/catch/index.js +0 -1
  21. package/output/color/color.d.ts +0 -6
  22. package/output/color/color.d.ts.map +0 -1
  23. package/output/color/color.js +0 -56
  24. package/output/color/color.test.d.ts +0 -2
  25. package/output/color/color.test.d.ts.map +0 -1
  26. package/output/color/color.test.js +0 -110
  27. package/output/color/index.d.ts +0 -2
  28. package/output/color/index.d.ts.map +0 -1
  29. package/output/color/index.js +0 -1
  30. package/output/debounce/debounce.d.ts +0 -7
  31. package/output/debounce/debounce.d.ts.map +0 -1
  32. package/output/debounce/debounce.js +0 -36
  33. package/output/debounce/debounce.test.d.ts +0 -2
  34. package/output/debounce/debounce.test.d.ts.map +0 -1
  35. package/output/debounce/debounce.test.js +0 -135
  36. package/output/debounce/index.d.ts +0 -2
  37. package/output/debounce/index.d.ts.map +0 -1
  38. package/output/debounce/index.js +0 -1
  39. package/output/disposer/disposer.d.ts +0 -8
  40. package/output/disposer/disposer.d.ts.map +0 -1
  41. package/output/disposer/disposer.js +0 -21
  42. package/output/disposer/disposer.test.d.ts +0 -2
  43. package/output/disposer/disposer.test.d.ts.map +0 -1
  44. package/output/disposer/disposer.test.js +0 -216
  45. package/output/disposer/index.d.ts +0 -2
  46. package/output/disposer/index.d.ts.map +0 -1
  47. package/output/disposer/index.js +0 -1
  48. package/output/error/error.d.ts +0 -128
  49. package/output/error/error.d.ts.map +0 -1
  50. package/output/error/error.js +0 -141
  51. package/output/error/error.test.d.ts +0 -2
  52. package/output/error/error.test.d.ts.map +0 -1
  53. package/output/error/error.test.js +0 -420
  54. package/output/error/index.d.ts +0 -3
  55. package/output/error/index.d.ts.map +0 -1
  56. package/output/error/index.js +0 -1
  57. package/output/event-emitter/event-emitter.d.ts +0 -33
  58. package/output/event-emitter/event-emitter.d.ts.map +0 -1
  59. package/output/event-emitter/event-emitter.js +0 -66
  60. package/output/event-emitter/event-emitter.test.d.ts +0 -2
  61. package/output/event-emitter/event-emitter.test.d.ts.map +0 -1
  62. package/output/event-emitter/event-emitter.test.js +0 -177
  63. package/output/event-emitter/index.d.ts +0 -2
  64. package/output/event-emitter/index.d.ts.map +0 -1
  65. package/output/event-emitter/index.js +0 -1
  66. package/output/flow-queue/flow-queue.d.ts +0 -486
  67. package/output/flow-queue/flow-queue.d.ts.map +0 -1
  68. package/output/flow-queue/flow-queue.js +0 -654
  69. package/output/flow-queue/flow-queue.test.d.ts +0 -2
  70. package/output/flow-queue/flow-queue.test.d.ts.map +0 -1
  71. package/output/flow-queue/flow-queue.test.js +0 -1205
  72. package/output/flow-queue/index.d.ts +0 -3
  73. package/output/flow-queue/index.d.ts.map +0 -1
  74. package/output/flow-queue/index.js +0 -3
  75. package/output/index.d.ts +0 -18
  76. package/output/index.d.ts.map +0 -1
  77. package/output/index.js +0 -26
  78. package/output/logger/formatter.d.ts +0 -70
  79. package/output/logger/formatter.d.ts.map +0 -1
  80. package/output/logger/formatter.js +0 -206
  81. package/output/logger/formatter.test.d.ts +0 -2
  82. package/output/logger/formatter.test.d.ts.map +0 -1
  83. package/output/logger/formatter.test.js +0 -296
  84. package/output/logger/index.d.ts +0 -4
  85. package/output/logger/index.d.ts.map +0 -1
  86. package/output/logger/index.js +0 -3
  87. package/output/logger/logger.d.ts +0 -64
  88. package/output/logger/logger.d.ts.map +0 -1
  89. package/output/logger/logger.js +0 -179
  90. package/output/logger/logger.test.d.ts +0 -2
  91. package/output/logger/logger.test.d.ts.map +0 -1
  92. package/output/logger/logger.test.js +0 -299
  93. package/output/logger/transport.d.ts +0 -124
  94. package/output/logger/transport.d.ts.map +0 -1
  95. package/output/logger/transport.js +0 -153
  96. package/output/logger/transport.test.d.ts +0 -2
  97. package/output/logger/transport.test.d.ts.map +0 -1
  98. package/output/logger/transport.test.js +0 -309
  99. package/output/lru-cache/index.d.ts +0 -2
  100. package/output/lru-cache/index.d.ts.map +0 -1
  101. package/output/lru-cache/index.js +0 -1
  102. package/output/lru-cache/lru-cache.d.ts +0 -22
  103. package/output/lru-cache/lru-cache.d.ts.map +0 -1
  104. package/output/lru-cache/lru-cache.js +0 -147
  105. package/output/lru-cache/lru-cache.test.d.ts +0 -2
  106. package/output/lru-cache/lru-cache.test.d.ts.map +0 -1
  107. package/output/lru-cache/lru-cache.test.js +0 -239
  108. package/output/number/index.d.ts +0 -2
  109. package/output/number/index.d.ts.map +0 -1
  110. package/output/number/index.js +0 -1
  111. package/output/number/number.d.ts +0 -2
  112. package/output/number/number.d.ts.map +0 -1
  113. package/output/number/number.js +0 -23
  114. package/output/number/number.test.d.ts +0 -2
  115. package/output/number/number.test.d.ts.map +0 -1
  116. package/output/number/number.test.js +0 -50
  117. package/output/object/index.d.ts +0 -2
  118. package/output/object/index.d.ts.map +0 -1
  119. package/output/object/index.js +0 -1
  120. package/output/object/object.d.ts +0 -7
  121. package/output/object/object.d.ts.map +0 -1
  122. package/output/object/object.js +0 -27
  123. package/output/object/object.test.d.ts +0 -2
  124. package/output/object/object.test.d.ts.map +0 -1
  125. package/output/object/object.test.js +0 -42
  126. package/output/ring-cache/index.d.ts +0 -6
  127. package/output/ring-cache/index.d.ts.map +0 -1
  128. package/output/ring-cache/index.js +0 -5
  129. package/output/ring-cache/ring-cache.d.ts +0 -125
  130. package/output/ring-cache/ring-cache.d.ts.map +0 -1
  131. package/output/ring-cache/ring-cache.js +0 -197
  132. package/output/ring-cache/ring-cache.test.d.ts +0 -2
  133. package/output/ring-cache/ring-cache.test.d.ts.map +0 -1
  134. package/output/ring-cache/ring-cache.test.js +0 -196
  135. package/output/service/index.d.ts +0 -2
  136. package/output/service/index.d.ts.map +0 -1
  137. package/output/service/index.js +0 -1
  138. package/output/service/service.d.ts +0 -6
  139. package/output/service/service.d.ts.map +0 -1
  140. package/output/service/service.js +0 -3
  141. package/output/string/index.d.ts +0 -2
  142. package/output/string/index.d.ts.map +0 -1
  143. package/output/string/index.js +0 -1
  144. package/output/string/string.d.ts +0 -3
  145. package/output/string/string.d.ts.map +0 -1
  146. package/output/string/string.js +0 -9
  147. package/output/string/string.test.d.ts +0 -2
  148. package/output/string/string.test.d.ts.map +0 -1
  149. package/output/string/string.test.js +0 -77
  150. package/output/throttle/index.d.ts +0 -2
  151. package/output/throttle/index.d.ts.map +0 -1
  152. package/output/throttle/index.js +0 -1
  153. package/output/throttle/throttle.d.ts +0 -12
  154. package/output/throttle/throttle.d.ts.map +0 -1
  155. package/output/throttle/throttle.js +0 -55
  156. package/output/throttle/throttle.test.d.ts +0 -2
  157. package/output/throttle/throttle.test.d.ts.map +0 -1
  158. package/output/throttle/throttle.test.js +0 -177
  159. package/output/ttl-cache/index.d.ts +0 -2
  160. package/output/ttl-cache/index.d.ts.map +0 -1
  161. package/output/ttl-cache/index.js +0 -1
  162. package/output/ttl-cache/ttl-cache.d.ts +0 -148
  163. package/output/ttl-cache/ttl-cache.d.ts.map +0 -1
  164. package/output/ttl-cache/ttl-cache.js +0 -313
  165. package/output/ttl-cache/ttl-cache.test.d.ts +0 -2
  166. package/output/ttl-cache/ttl-cache.test.d.ts.map +0 -1
  167. package/output/ttl-cache/ttl-cache.test.js +0 -222
@@ -1,3 +0,0 @@
1
- export { FlowQueue } from './flow-queue.js';
2
- export type { FlowQueueOptions, FlowQueueProcessor, FlowQueueRetryStrategy, FlowQueueRetryConfig, FlowQueueDelayConfig, FlowQueueOnGenerate } from './flow-queue.js';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/flow-queue/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,YAAY,EACV,gBAAgB,EAChB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,iBAAiB,CAAA"}
@@ -1,3 +0,0 @@
1
- // Flow Queue 模块入口
2
- // 提供流式处理队列功能,支持重试、延迟等高级特性
3
- export { FlowQueue } from './flow-queue.js';
package/output/index.d.ts DELETED
@@ -1,18 +0,0 @@
1
- export * from './disposer/index.js';
2
- export * from './event-emitter/index.js';
3
- export * from './logger/index.js';
4
- export * from './error/index.js';
5
- export * from './service/index.js';
6
- export * from './ttl-cache/index.js';
7
- export * from './lru-cache/index.js';
8
- export * from './ring-cache/index.js';
9
- export * from './batching-buffer/index.js';
10
- export * from './string/index.js';
11
- export * from './color/index.js';
12
- export * from './object/index.js';
13
- export * from './number/index.js';
14
- export * from './catch/index.js';
15
- export * from './debounce/index.js';
16
- export * from './throttle/index.js';
17
- export * from './flow-queue/index.js';
18
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AACA,cAAc,qBAAqB,CAAA;AAGnC,cAAc,0BAA0B,CAAA;AAGxC,cAAc,mBAAmB,CAAA;AAGjC,cAAc,kBAAkB,CAAA;AAGhC,cAAc,oBAAoB,CAAA;AAGlC,cAAc,sBAAsB,CAAA;AACpC,cAAc,sBAAsB,CAAA;AACpC,cAAc,uBAAuB,CAAA;AAGrC,cAAc,4BAA4B,CAAA;AAG1C,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA;AAGhC,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AAEnC,cAAc,uBAAuB,CAAA"}
package/output/index.js DELETED
@@ -1,26 +0,0 @@
1
- // === 资源管理 ===
2
- export * from './disposer/index.js';
3
- // === 事件系统 ===
4
- export * from './event-emitter/index.js';
5
- // === 日志系统 ===
6
- export * from './logger/index.js';
7
- // === 错误处理 ===
8
- export * from './error/index.js';
9
- // === 服务管理 ===
10
- export * from './service/index.js';
11
- // === 缓存系统 ===
12
- export * from './ttl-cache/index.js';
13
- export * from './lru-cache/index.js';
14
- export * from './ring-cache/index.js';
15
- // === 数据管理 ===
16
- export * from './batching-buffer/index.js';
17
- // === 基础工具 ===
18
- export * from './string/index.js';
19
- export * from './color/index.js';
20
- export * from './object/index.js';
21
- export * from './number/index.js';
22
- export * from './catch/index.js';
23
- // === 函数增强 ===
24
- export * from './debounce/index.js';
25
- export * from './throttle/index.js';
26
- export * from './flow-queue/index.js';
@@ -1,70 +0,0 @@
1
- export interface FormatterConfig {
2
- /** 错误对象格式化器 */
3
- error?: (error: unknown) => string;
4
- /** 日期格式化器 */
5
- date?: (date: Date) => string;
6
- /** 对象格式化器 */
7
- object?: (obj: any) => string;
8
- /** 数组格式化器 */
9
- array?: (arr: any[]) => string;
10
- /** 函数格式化器 */
11
- function?: (fn: Function) => string;
12
- /** 其他类型格式化器 */
13
- other?: (value: any) => string;
14
- }
15
- /**
16
- * 日志格式化器接口
17
- * 定义格式化器的核心方法
18
- */
19
- export interface LogFormatter {
20
- /** 格式化单个参数 */
21
- format(arg: any): string;
22
- /** 格式化参数数组 */
23
- formatArguments(args: any[]): string[];
24
- }
25
- export interface ErrorFormatterOptions {
26
- /** 是否显示堆栈信息 */
27
- showStack?: boolean;
28
- /** 是否显示错误上下文 */
29
- showContext?: boolean;
30
- /** 是否使用颜色(在支持的终端中) */
31
- useColors?: boolean;
32
- }
33
- /**
34
- * 日志格式化器
35
- * 负责根据配置格式化各种类型的参数
36
- */
37
- export declare class DefaultLogFormatter implements LogFormatter {
38
- private errorOptions;
39
- private config;
40
- constructor(config?: FormatterConfig, errorOptions?: ErrorFormatterOptions);
41
- /**
42
- * 格式化单个参数
43
- */
44
- format(arg: any): string;
45
- /**
46
- * 格式化参数数组
47
- */
48
- formatArguments(args: any[]): string[];
49
- /**
50
- * 格式化错误对象
51
- */
52
- private formatError;
53
- private formatUserError;
54
- private formatSystemError;
55
- private formatGenericError;
56
- private formatUnknownValue;
57
- private formatContext;
58
- private colorize;
59
- private formatDate;
60
- private formatObject;
61
- private formatArray;
62
- private formatFunction;
63
- private formatSimple;
64
- private formatOther;
65
- }
66
- /**
67
- * 默认的日志格式化器实例
68
- */
69
- export declare const defaultLogFormatter: DefaultLogFormatter;
70
- //# sourceMappingURL=formatter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../source/logger/formatter.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,eAAe;IAC9B,eAAe;IACf,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAA;IAClC,aAAa;IACb,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,MAAM,CAAA;IAC7B,aAAa;IACb,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,MAAM,CAAA;IAC7B,aAAa;IACb,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,MAAM,CAAA;IAC9B,aAAa;IACb,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,KAAK,MAAM,CAAA;IACnC,eAAe;IACf,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,MAAM,CAAA;CAC/B;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,cAAc;IACd,MAAM,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAAA;IACxB,cAAc;IACd,eAAe,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,EAAE,CAAA;CACvC;AAED,MAAM,WAAW,qBAAqB;IACpC,eAAe;IACf,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,gBAAgB;IAChB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,sBAAsB;IACtB,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,YAAY;IACtD,OAAO,CAAC,YAAY,CAAuB;IAC3C,OAAO,CAAC,MAAM,CAA0C;gBAE5C,MAAM,GAAE,eAAoB,EAAE,YAAY,GAAE,qBAA0B;IAgBlF;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM;IAwBxB;;OAEG;IACH,eAAe,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,EAAE;IAItC;;OAEG;IACH,OAAO,CAAC,WAAW;IAsBnB,OAAO,CAAC,eAAe;IAiBvB,OAAO,CAAC,iBAAiB;IAiBzB,OAAO,CAAC,kBAAkB;IAuB1B,OAAO,CAAC,kBAAkB;IAI1B,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,QAAQ;IAehB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,WAAW;CAWpB;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,qBAA4B,CAAA"}
@@ -1,206 +0,0 @@
1
- /**
2
- * 格式化器配置模块
3
- * 提供各种类型数据的格式化功能
4
- */
5
- import { UserError, SystemError } from '../error/index.js';
6
- /**
7
- * 日志格式化器
8
- * 负责根据配置格式化各种类型的参数
9
- */
10
- export class DefaultLogFormatter {
11
- errorOptions;
12
- config;
13
- constructor(config = {}, errorOptions = {}) {
14
- this.errorOptions = {
15
- showStack: errorOptions.showStack ?? true,
16
- showContext: errorOptions.showContext ?? true,
17
- useColors: errorOptions.useColors ?? true,
18
- };
19
- this.config = {
20
- date: config.date || this.formatDate.bind(this),
21
- object: config.object || this.formatObject.bind(this),
22
- array: config.array || this.formatArray.bind(this),
23
- function: config.function || this.formatFunction.bind(this),
24
- other: config.other || this.formatOther.bind(this),
25
- };
26
- }
27
- /**
28
- * 格式化单个参数
29
- */
30
- format(arg) {
31
- if (arg instanceof Error) {
32
- return this.formatError(arg, this.errorOptions);
33
- }
34
- if (arg instanceof Date) {
35
- return this.config.date(arg);
36
- }
37
- if (Array.isArray(arg)) {
38
- return this.config.array(arg);
39
- }
40
- if (typeof arg === 'function') {
41
- return this.config.function(arg);
42
- }
43
- if (typeof arg === 'object' && arg !== null) {
44
- return this.config.object(arg);
45
- }
46
- return this.config.other(arg);
47
- }
48
- /**
49
- * 格式化参数数组
50
- */
51
- formatArguments(args) {
52
- return args.map(arg => this.format(arg));
53
- }
54
- /**
55
- * 格式化错误对象
56
- */
57
- formatError(error, options = {}) {
58
- const opts = {
59
- showStack: options.showStack ?? true,
60
- showContext: options.showContext ?? true,
61
- useColors: options.useColors ?? true,
62
- };
63
- if (UserError.is(error)) {
64
- return this.formatUserError(error, opts);
65
- }
66
- if (SystemError.is(error)) {
67
- return this.formatSystemError(error, opts);
68
- }
69
- if (error instanceof Error) {
70
- return this.formatGenericError(error, opts);
71
- }
72
- return this.formatUnknownValue(error);
73
- }
74
- formatUserError(error, opts) {
75
- const lines = [
76
- this.colorize(`🚫 User Error [${error.type}]`, 'red', opts.useColors),
77
- `Message: ${error.message}`,
78
- ];
79
- if (opts.showContext && error.context) {
80
- lines.push(`Context: ${this.formatContext(error.context)}`);
81
- }
82
- if (opts.showStack && error.stack) {
83
- lines.push(`Stack: ${error.stack}`);
84
- }
85
- return lines.join('\n ');
86
- }
87
- formatSystemError(error, opts) {
88
- const lines = [
89
- this.colorize(`⚠️ System Error [${error.type}]`, 'yellow', opts.useColors),
90
- `Message: ${error.message}`,
91
- ];
92
- if (opts.showContext && error.context) {
93
- lines.push(`Context: ${this.formatContext(error.context)}`);
94
- }
95
- if (opts.showStack && error.stack) {
96
- lines.push(`Stack: ${error.stack}`);
97
- }
98
- return lines.join('\n ');
99
- }
100
- formatGenericError(error, opts) {
101
- const lines = [
102
- this.colorize(`💥 ${error.name}`, 'red', opts.useColors),
103
- `Message: ${error.message}`,
104
- ];
105
- // 处理额外的错误属性
106
- const additionalProps = Object.getOwnPropertyNames(error)
107
- .filter(prop => !['name', 'message', 'stack'].includes(prop))
108
- .map(prop => `${prop}: ${this.formatSimple(error[prop])}`)
109
- .filter(Boolean);
110
- if (additionalProps.length > 0) {
111
- lines.push(`Additional: ${additionalProps.join(', ')}`);
112
- }
113
- if (opts.showStack && error.stack) {
114
- lines.push(`Stack: ${error.stack}`);
115
- }
116
- return lines.join('\n ');
117
- }
118
- formatUnknownValue(value) {
119
- return `Unknown Error: ${this.formatOther(value)}`;
120
- }
121
- formatContext(context) {
122
- if (!context || typeof context !== 'object') {
123
- return String(context);
124
- }
125
- try {
126
- return JSON.stringify(context, null, 2);
127
- }
128
- catch {
129
- return String(context);
130
- }
131
- }
132
- colorize(text, color, useColors) {
133
- if (!useColors) {
134
- return text;
135
- }
136
- const colors = {
137
- red: '\x1b[31m',
138
- yellow: '\x1b[33m',
139
- magenta: '\x1b[35m',
140
- reset: '\x1b[0m',
141
- };
142
- return `${colors[color] || ''}${text}${colors.reset}`;
143
- }
144
- formatDate(date) {
145
- return date.toISOString();
146
- }
147
- formatObject(obj) {
148
- if (obj === null)
149
- return 'null';
150
- if (obj === undefined)
151
- return 'undefined';
152
- try {
153
- return JSON.stringify(obj, null, 2);
154
- }
155
- catch {
156
- return String(obj);
157
- }
158
- }
159
- formatArray(arr) {
160
- try {
161
- return JSON.stringify(arr, null, 2);
162
- }
163
- catch {
164
- return String(arr);
165
- }
166
- }
167
- formatFunction(fn) {
168
- return `[Function: ${fn.name || 'anonymous'}]`;
169
- }
170
- formatSimple(value) {
171
- if (value === null)
172
- return 'null';
173
- if (value === undefined)
174
- return 'undefined';
175
- if (typeof value === 'string')
176
- return value;
177
- if (typeof value === 'number' || typeof value === 'boolean')
178
- return String(value);
179
- if (typeof value === 'symbol')
180
- return value.toString();
181
- if (typeof value === 'bigint')
182
- return value.toString() + 'n';
183
- return String(value);
184
- }
185
- formatOther(value) {
186
- if (value === null)
187
- return '[null]';
188
- if (value === undefined)
189
- return '[undefined]';
190
- if (typeof value === 'string')
191
- return value; // 字符串保持原样,不添加类型标识
192
- if (typeof value === 'number')
193
- return `${value}`;
194
- if (typeof value === 'boolean')
195
- return `${value}`;
196
- if (typeof value === 'symbol')
197
- return `[symbol] ${value.toString()}`;
198
- if (typeof value === 'bigint')
199
- return `[bigint] ${value.toString()}n`;
200
- return String(value);
201
- }
202
- }
203
- /**
204
- * 默认的日志格式化器实例
205
- */
206
- export const defaultLogFormatter = new DefaultLogFormatter();
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=formatter.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"formatter.test.d.ts","sourceRoot":"","sources":["../../source/logger/formatter.test.ts"],"names":[],"mappings":""}
@@ -1,296 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import { DefaultLogFormatter } from './formatter.js';
3
- import { UserError, SystemError } from '../error/index.js';
4
- describe('DefaultLogFormatter', () => {
5
- let formatter;
6
- beforeEach(() => {
7
- formatter = new DefaultLogFormatter();
8
- });
9
- describe('基本格式化功能', () => {
10
- it('应该格式化字符串', () => {
11
- const result = formatter.format('测试字符串');
12
- expect(result).toBe('测试字符串');
13
- });
14
- it('应该格式化数字', () => {
15
- const result = formatter.format(123);
16
- expect(result).toBe("123");
17
- });
18
- it('应该格式化布尔值', () => {
19
- const trueResult = formatter.format(true);
20
- const falseResult = formatter.format(false);
21
- expect(trueResult).toBe("true");
22
- expect(falseResult).toBe("false");
23
- });
24
- it('应该格式化 null', () => {
25
- const result = formatter.format(null);
26
- expect(result).toBe('[null]');
27
- });
28
- it('应该格式化 undefined', () => {
29
- const result = formatter.format(undefined);
30
- expect(result).toBe('[undefined]');
31
- });
32
- it('应该格式化 Symbol', () => {
33
- const symbol = Symbol('test');
34
- const result = formatter.format(symbol);
35
- expect(result).toBe(`[symbol] ${symbol.toString()}`);
36
- });
37
- it('应该格式化 BigInt', () => {
38
- const bigint = BigInt(123);
39
- const result = formatter.format(bigint);
40
- expect(result).toBe('[bigint] 123n');
41
- });
42
- });
43
- describe('日期格式化', () => {
44
- it('应该格式化日期为 ISO 字符串', () => {
45
- const date = new Date('2023-01-01T00:00:00.000Z');
46
- const result = formatter.format(date);
47
- expect(result).toBe('2023-01-01T00:00:00.000Z');
48
- });
49
- it('应该支持自定义日期格式化器', () => {
50
- const customFormatter = new DefaultLogFormatter({
51
- date: (date) => date.toLocaleDateString()
52
- });
53
- const date = new Date('2023-01-01T00:00:00.000Z');
54
- const result = customFormatter.format(date);
55
- expect(result).toBe(date.toLocaleDateString());
56
- });
57
- });
58
- describe('对象格式化', () => {
59
- it('应该格式化普通对象', () => {
60
- const obj = { name: '测试', age: 25 };
61
- const result = formatter.format(obj);
62
- expect(result).toBe(JSON.stringify(obj, null, 2));
63
- });
64
- it('应该处理循环引用对象', () => {
65
- const obj = { name: '测试' };
66
- obj.self = obj;
67
- const result = formatter.format(obj);
68
- expect(typeof result).toBe('string');
69
- expect(result).toBe('[object Object]');
70
- });
71
- it('应该支持自定义对象格式化器', () => {
72
- const customFormatter = new DefaultLogFormatter({
73
- object: (obj) => `Custom: ${JSON.stringify(obj)}`
74
- });
75
- const obj = { name: '测试' };
76
- const result = customFormatter.format(obj);
77
- expect(result).toBe('Custom: {"name":"测试"}');
78
- });
79
- });
80
- describe('数组格式化', () => {
81
- it('应该格式化数组', () => {
82
- const arr = [1, '测试', true];
83
- const result = formatter.format(arr);
84
- expect(result).toBe(JSON.stringify(arr, null, 2));
85
- });
86
- it('应该格式化空数组', () => {
87
- const arr = [];
88
- const result = formatter.format(arr);
89
- expect(result).toBe('[]');
90
- });
91
- it('应该支持自定义数组格式化器', () => {
92
- const customFormatter = new DefaultLogFormatter({
93
- array: (arr) => `Array[${arr.length}]: ${arr.join(', ')}`
94
- });
95
- const arr = [1, 2, 3];
96
- const result = customFormatter.format(arr);
97
- expect(result).toBe('Array[3]: 1, 2, 3');
98
- });
99
- });
100
- describe('函数格式化', () => {
101
- it('应该格式化命名函数', () => {
102
- function testFunction() { }
103
- const result = formatter.format(testFunction);
104
- expect(result).toBe('[Function: testFunction]');
105
- });
106
- it('应该格式化匿名函数', () => {
107
- const result = formatter.format((() => { }));
108
- expect(result).toBe('[Function: anonymous]');
109
- });
110
- it('应该支持自定义函数格式化器', () => {
111
- const customFormatter = new DefaultLogFormatter({
112
- function: (fn) => `Custom Function: ${fn.name || 'unnamed'}`
113
- });
114
- function namedFunction() { }
115
- const result = customFormatter.format(namedFunction);
116
- expect(result).toBe('Custom Function: namedFunction');
117
- });
118
- });
119
- describe('错误格式化', () => {
120
- describe('UserError 格式化', () => {
121
- it('应该格式化带有完整信息的 UserError', () => {
122
- const error = new UserError('validation-failed', {
123
- field: 'email',
124
- value: 'invalid-email',
125
- rule: 'email-format'
126
- }, '验证失败');
127
- const result = formatter.format(error);
128
- expect(result).toContain('🚫 User Error [validation-failed]');
129
- expect(result).toContain('Message: 验证失败');
130
- expect(result).toContain('Context:');
131
- expect(result).toContain('Stack:');
132
- });
133
- it('应该在禁用颜色时不包含 ANSI 代码', () => {
134
- const formatterWithoutColors = new DefaultLogFormatter({}, { useColors: false });
135
- const error = new UserError('test-error', undefined, '测试错误');
136
- const result = formatterWithoutColors.format(error);
137
- expect(result).not.toContain('\x1b[');
138
- expect(result).toContain('🚫 User Error [test-error]');
139
- expect(result).toContain('Message: 测试错误');
140
- });
141
- it('应该在禁用堆栈时不显示堆栈信息', () => {
142
- const formatterWithoutStack = new DefaultLogFormatter({}, { showStack: false });
143
- const error = new UserError('test-error', undefined, '测试错误');
144
- const result = formatterWithoutStack.format(error);
145
- expect(result).not.toContain('Stack:');
146
- expect(result).toContain('Message: 测试错误');
147
- });
148
- it('应该在禁用上下文时不显示上下文信息', () => {
149
- const formatterWithoutContext = new DefaultLogFormatter({}, { showContext: false });
150
- const error = new UserError('test-error', { field: 'test' }, '测试错误');
151
- const result = formatterWithoutContext.format(error);
152
- expect(result).not.toContain('Context:');
153
- expect(result).toContain('Message: 测试错误');
154
- });
155
- });
156
- describe('SystemError 格式化', () => {
157
- it('应该格式化带有完整信息的 SystemError', () => {
158
- const error = new SystemError('database-error', {
159
- table: 'users',
160
- query: 'SELECT * FROM users',
161
- operation: 'select'
162
- }, '数据库连接失败');
163
- const result = formatter.format(error);
164
- expect(result).toContain('⚠️ System Error [database-error]');
165
- expect(result).toContain('Message: 数据库连接失败');
166
- expect(result).toContain('Context:');
167
- expect(result).toContain('Stack:');
168
- });
169
- it('应该在禁用颜色时不包含 ANSI 代码', () => {
170
- const formatterWithoutColors = new DefaultLogFormatter({}, { useColors: false });
171
- const error = new SystemError('network-error', undefined, '网络错误');
172
- const result = formatterWithoutColors.format(error);
173
- expect(result).not.toContain('\x1b[');
174
- expect(result).toContain('⚠️ System Error [network-error]');
175
- });
176
- });
177
- describe('通用 Error 格式化', () => {
178
- it('应该格式化普通 Error', () => {
179
- const error = new Error('普通错误');
180
- const result = formatter.format(error);
181
- expect(result).toContain('💥 Error');
182
- expect(result).toContain('Message: 普通错误');
183
- expect(result).toContain('Stack:');
184
- });
185
- it('应该格式化带有额外属性的 Error', () => {
186
- const error = new Error('测试错误');
187
- error.code = 'TEST_ERROR';
188
- error.statusCode = 500;
189
- const result = formatter.format(error);
190
- expect(result).toContain('💥 Error');
191
- expect(result).toContain('Message: 测试错误');
192
- expect(result).toContain('Additional:');
193
- expect(result).toContain('code: TEST_ERROR');
194
- expect(result).toContain('statusCode: 500');
195
- });
196
- it('应该格式化自定义错误类型', () => {
197
- class CustomError extends Error {
198
- code;
199
- constructor(message, code) {
200
- super(message);
201
- this.code = code;
202
- this.name = 'CustomError';
203
- }
204
- }
205
- const error = new CustomError('自定义错误', 'CUSTOM_001');
206
- const result = formatter.format(error);
207
- expect(result).toContain('💥 CustomError');
208
- expect(result).toContain('Message: 自定义错误');
209
- expect(result).toContain('Additional:');
210
- expect(result).toContain('code: CUSTOM_001');
211
- });
212
- });
213
- describe('未知值格式化', () => {
214
- it('应该格式化非错误对象', () => {
215
- const unknownValue = { message: '这不是错误对象' };
216
- const result = formatter.format(unknownValue);
217
- expect(result).toBe(JSON.stringify(unknownValue, null, 2));
218
- });
219
- });
220
- });
221
- describe('参数数组格式化', () => {
222
- it('应该格式化混合类型参数数组', () => {
223
- const args = [
224
- '字符串',
225
- 123,
226
- true,
227
- { name: '对象' },
228
- [1, 2, 3],
229
- new Date('2023-01-01T00:00:00.000Z'),
230
- new Error('测试错误')
231
- ];
232
- const result = formatter.formatArguments(args);
233
- expect(result).toHaveLength(7);
234
- expect(result[0]).toBe('字符串');
235
- expect(result[1]).toBe('123');
236
- expect(result[2]).toBe('true');
237
- expect(result[3]).toBe(JSON.stringify({ name: '对象' }, null, 2));
238
- expect(result[4]).toBe(JSON.stringify([1, 2, 3], null, 2));
239
- expect(result[5]).toBe('2023-01-01T00:00:00.000Z');
240
- expect(result[6]).toContain('💥 Error');
241
- });
242
- it('应该格式化空参数数组', () => {
243
- const result = formatter.formatArguments([]);
244
- expect(result).toEqual([]);
245
- });
246
- });
247
- describe('自定义格式化器配置', () => {
248
- it('应该支持完全自定义的格式化器', () => {
249
- const customFormatter = new DefaultLogFormatter({
250
- date: (date) => `日期: ${date.getFullYear()}`,
251
- object: (obj) => `对象: ${Object.keys(obj).join(', ')}`,
252
- array: (arr) => `数组长度: ${arr.length}`,
253
- function: (fn) => `函数名: ${fn.name}`,
254
- other: (value) => `其他类型: ${typeof value}`
255
- });
256
- const date = new Date('2023-01-01');
257
- const obj = { a: 1, b: 2 };
258
- const arr = [1, 2, 3];
259
- const fn = function testFn() { };
260
- const symbol = Symbol('test');
261
- expect(customFormatter.format(date)).toBe('日期: 2023');
262
- expect(customFormatter.format(obj)).toBe('对象: a, b');
263
- expect(customFormatter.format(arr)).toBe('数组长度: 3');
264
- expect(customFormatter.format(fn)).toBe('函数名: testFn');
265
- expect(customFormatter.format(symbol)).toBe('其他类型: symbol');
266
- });
267
- });
268
- describe('错误选项配置', () => {
269
- it('应该支持完全禁用错误格式化选项', () => {
270
- const minimalistFormatter = new DefaultLogFormatter({}, {
271
- showStack: false,
272
- showContext: false,
273
- useColors: false
274
- });
275
- const error = new UserError('test-error', { field: 'test' }, '测试错误');
276
- const result = minimalistFormatter.format(error);
277
- expect(result).not.toContain('Stack:');
278
- expect(result).not.toContain('Context:');
279
- expect(result).not.toContain('\x1b[');
280
- expect(result).toContain('🚫 User Error [test-error]');
281
- expect(result).toContain('Message: 测试错误');
282
- });
283
- it('应该支持只显示特定错误信息', () => {
284
- const stackOnlyFormatter = new DefaultLogFormatter({}, {
285
- showStack: true,
286
- showContext: false,
287
- useColors: false
288
- });
289
- const error = new SystemError('database-error', { table: 'users' }, '数据库错误');
290
- const result = stackOnlyFormatter.format(error);
291
- expect(result).toContain('Stack:');
292
- expect(result).not.toContain('Context:');
293
- expect(result).not.toContain('\x1b[');
294
- });
295
- });
296
- });
@@ -1,4 +0,0 @@
1
- export * from './logger.js';
2
- export * from './formatter.js';
3
- export * from './transport.js';
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/logger/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,gBAAgB,CAAA"}
@@ -1,3 +0,0 @@
1
- export * from './logger.js';
2
- export * from './formatter.js';
3
- export * from './transport.js';