@taicode/common-base 3.7.5 → 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,197 +0,0 @@
1
- /**
2
- * 环形缓存(Ring Cache)实现
3
- * 专门用于日志场景的FIFO(先进先出)缓存
4
- *
5
- * 特点:
6
- * - FIFO行为:新数据总是添加到尾部,当容量满时从头部移除旧数据
7
- * - 固定容量:避免内存无限增长
8
- * - 高效操作:O(1)的添加和访问操作
9
- * - 适用场景:日志记录、事件历史、时间序列数据等
10
- */
11
- /**
12
- * 环形缓存实现
13
- * 使用固定大小的数组实现FIFO行为的环形缓存
14
- */
15
- export class RingCache {
16
- buffer;
17
- head = 0; // 指向最旧的元素
18
- tail = 0; // 指向下一个要插入的位置
19
- size = 0;
20
- capacity;
21
- totalAdded = 0;
22
- nextId = 1;
23
- constructor(options = {}) {
24
- this.capacity = options.capacity ?? 1000;
25
- if (this.capacity <= 0) {
26
- throw new Error('Capacity must be greater than 0');
27
- }
28
- this.buffer = new Array(this.capacity);
29
- }
30
- /**
31
- * 添加新条目到缓存
32
- * 如果缓存已满,将覆盖最旧的条目
33
- */
34
- add(data) {
35
- const entry = {
36
- data,
37
- timestamp: new Date(),
38
- id: this.generateId()
39
- };
40
- this.buffer[this.tail] = entry;
41
- this.tail = (this.tail + 1) % this.capacity;
42
- this.totalAdded++;
43
- if (this.size < this.capacity) {
44
- this.size++;
45
- }
46
- else {
47
- // 缓存已满,头指针也需要移动
48
- this.head = (this.head + 1) % this.capacity;
49
- }
50
- return entry;
51
- }
52
- /**
53
- * 获取所有缓存条目(按添加顺序)
54
- */
55
- getAll() {
56
- if (this.size === 0) {
57
- return [];
58
- }
59
- const result = [];
60
- for (let i = 0; i < this.size; i++) {
61
- const index = (this.head + i) % this.capacity;
62
- const entry = this.buffer[index];
63
- if (entry) {
64
- result.push(entry);
65
- }
66
- }
67
- return result;
68
- }
69
- /**
70
- * 获取最近的N个条目
71
- */
72
- getRecent(count) {
73
- if (count <= 0 || this.size === 0) {
74
- return [];
75
- }
76
- const actualCount = Math.min(count, this.size);
77
- const result = [];
78
- // 从尾部向前获取
79
- for (let i = 0; i < actualCount; i++) {
80
- const index = (this.tail - 1 - i + this.capacity) % this.capacity;
81
- const entry = this.buffer[index];
82
- if (entry) {
83
- result.unshift(entry); // 保持时间顺序
84
- }
85
- }
86
- return result;
87
- }
88
- /**
89
- * 获取最旧的N个条目
90
- */
91
- getOldest(count) {
92
- if (count <= 0 || this.size === 0) {
93
- return [];
94
- }
95
- const actualCount = Math.min(count, this.size);
96
- const result = [];
97
- for (let i = 0; i < actualCount; i++) {
98
- const index = (this.head + i) % this.capacity;
99
- const entry = this.buffer[index];
100
- if (entry) {
101
- result.push(entry);
102
- }
103
- }
104
- return result;
105
- }
106
- /**
107
- * 根据时间范围获取条目
108
- */
109
- getByTimeRange(start, end) {
110
- return this.getAll().filter(entry => entry.timestamp >= start && entry.timestamp <= end);
111
- }
112
- /**
113
- * 搜索满足条件的条目
114
- */
115
- search(predicate) {
116
- return this.getAll().filter(predicate);
117
- }
118
- /**
119
- * 获取缓存统计信息
120
- */
121
- getStats() {
122
- const all = this.getAll();
123
- return {
124
- size: this.size,
125
- capacity: this.capacity,
126
- isFull: this.size >= this.capacity,
127
- oldestTimestamp: all.length > 0 ? all[0].timestamp : undefined,
128
- newestTimestamp: all.length > 0 ? all[all.length - 1].timestamp : undefined,
129
- totalAdded: this.totalAdded
130
- };
131
- }
132
- /**
133
- * 检查缓存是否为空
134
- */
135
- isEmpty() {
136
- return this.size === 0;
137
- }
138
- /**
139
- * 检查缓存是否已满
140
- */
141
- isFull() {
142
- return this.size >= this.capacity;
143
- }
144
- /**
145
- * 获取当前缓存大小
146
- */
147
- getSize() {
148
- return this.size;
149
- }
150
- /**
151
- * 获取缓存容量
152
- */
153
- getCapacity() {
154
- return this.capacity;
155
- }
156
- /**
157
- * 清空缓存
158
- */
159
- clear() {
160
- this.buffer = new Array(this.capacity);
161
- this.head = 0;
162
- this.tail = 0;
163
- this.size = 0;
164
- this.totalAdded = 0;
165
- this.nextId = 1;
166
- }
167
- /**
168
- * 导出为数组格式
169
- */
170
- toArray() {
171
- return this.getAll().map(entry => entry.data);
172
- }
173
- /**
174
- * 导出为JSON格式
175
- */
176
- toJSON() {
177
- return JSON.stringify(this.getAll(), null, 2);
178
- }
179
- /**
180
- * 生成唯一ID
181
- */
182
- generateId() {
183
- return `${Date.now()}-${this.nextId++}`;
184
- }
185
- /**
186
- * 创建迭代器,支持for...of语法
187
- */
188
- *[Symbol.iterator]() {
189
- for (let i = 0; i < this.size; i++) {
190
- const index = (this.head + i) % this.capacity;
191
- const entry = this.buffer[index];
192
- if (entry) {
193
- yield entry;
194
- }
195
- }
196
- }
197
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=ring-cache.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ring-cache.test.d.ts","sourceRoot":"","sources":["../../source/ring-cache/ring-cache.test.ts"],"names":[],"mappings":""}
@@ -1,196 +0,0 @@
1
- /**
2
- * RingCache 测试文件
3
- */
4
- import { describe, it, expect, beforeEach } from 'vitest';
5
- import { RingCache } from './ring-cache.js';
6
- describe('RingCache', () => {
7
- let cache;
8
- beforeEach(() => {
9
- cache = new RingCache({ capacity: 3 });
10
- });
11
- describe('基础功能', () => {
12
- it('应该正确初始化', () => {
13
- expect(cache.getSize()).toBe(0);
14
- expect(cache.getCapacity()).toBe(3);
15
- expect(cache.isEmpty()).toBe(true);
16
- expect(cache.isFull()).toBe(false);
17
- });
18
- it('应该正确添加条目', () => {
19
- const entry = cache.add('test1');
20
- expect(entry.data).toBe('test1');
21
- expect(entry.id).toBeDefined();
22
- expect(entry.timestamp).toBeInstanceOf(Date);
23
- expect(cache.getSize()).toBe(1);
24
- expect(cache.isEmpty()).toBe(false);
25
- });
26
- it('应该正确处理容量限制', () => {
27
- cache.add('test1');
28
- cache.add('test2');
29
- cache.add('test3');
30
- expect(cache.getSize()).toBe(3);
31
- expect(cache.isFull()).toBe(true);
32
- // 添加第4个条目,应该覆盖第1个
33
- cache.add('test4');
34
- expect(cache.getSize()).toBe(3);
35
- expect(cache.isFull()).toBe(true);
36
- const all = cache.getAll();
37
- expect(all.map(e => e.data)).toEqual(['test2', 'test3', 'test4']);
38
- });
39
- });
40
- describe('获取功能', () => {
41
- beforeEach(() => {
42
- cache.add('first');
43
- cache.add('second');
44
- cache.add('third');
45
- });
46
- it('应该正确获取所有条目', () => {
47
- const all = cache.getAll();
48
- expect(all.length).toBe(3);
49
- expect(all.map(e => e.data)).toEqual(['first', 'second', 'third']);
50
- });
51
- it('应该正确获取最近的条目', () => {
52
- const recent = cache.getRecent(2);
53
- expect(recent.length).toBe(2);
54
- expect(recent.map(e => e.data)).toEqual(['second', 'third']);
55
- });
56
- it('应该正确获取最旧的条目', () => {
57
- const oldest = cache.getOldest(2);
58
- expect(oldest.length).toBe(2);
59
- expect(oldest.map(e => e.data)).toEqual(['first', 'second']);
60
- });
61
- it('应该正确处理边界情况', () => {
62
- expect(cache.getRecent(0)).toEqual([]);
63
- expect(cache.getRecent(10)).toHaveLength(3);
64
- expect(cache.getOldest(0)).toEqual([]);
65
- expect(cache.getOldest(10)).toHaveLength(3);
66
- });
67
- });
68
- describe('搜索功能', () => {
69
- beforeEach(() => {
70
- cache.add('apple');
71
- cache.add('banana');
72
- cache.add('apricot');
73
- });
74
- it('应该正确搜索条目', () => {
75
- const results = cache.search(entry => entry.data.startsWith('ap'));
76
- expect(results.length).toBe(2);
77
- expect(results.map(e => e.data)).toEqual(['apple', 'apricot']);
78
- });
79
- it('应该支持时间范围搜索', () => {
80
- const entries = cache.getAll();
81
- const start = new Date(entries[0].timestamp.getTime() - 1);
82
- const end = new Date(entries[entries.length - 1].timestamp.getTime() + 1);
83
- const results = cache.getByTimeRange(start, end);
84
- expect(results.length).toBe(3);
85
- });
86
- });
87
- describe('统计功能', () => {
88
- it('应该正确返回空缓存的统计信息', () => {
89
- const stats = cache.getStats();
90
- expect(stats.size).toBe(0);
91
- expect(stats.capacity).toBe(3);
92
- expect(stats.isFull).toBe(false);
93
- expect(stats.oldestTimestamp).toBeUndefined();
94
- expect(stats.newestTimestamp).toBeUndefined();
95
- expect(stats.totalAdded).toBe(0);
96
- });
97
- it('应该正确返回有数据的统计信息', () => {
98
- cache.add('test1');
99
- cache.add('test2');
100
- const stats = cache.getStats();
101
- expect(stats.size).toBe(2);
102
- expect(stats.capacity).toBe(3);
103
- expect(stats.isFull).toBe(false);
104
- expect(stats.oldestTimestamp).toBeInstanceOf(Date);
105
- expect(stats.newestTimestamp).toBeInstanceOf(Date);
106
- expect(stats.totalAdded).toBe(2);
107
- });
108
- it('应该正确跟踪总添加数', () => {
109
- // 添加超过容量的条目
110
- cache.add('1');
111
- cache.add('2');
112
- cache.add('3');
113
- cache.add('4');
114
- cache.add('5');
115
- const stats = cache.getStats();
116
- expect(stats.size).toBe(3);
117
- expect(stats.totalAdded).toBe(5);
118
- });
119
- });
120
- describe('清空和导出功能', () => {
121
- beforeEach(() => {
122
- cache.add('test1');
123
- cache.add('test2');
124
- });
125
- it('应该正确清空缓存', () => {
126
- cache.clear();
127
- expect(cache.getSize()).toBe(0);
128
- expect(cache.isEmpty()).toBe(true);
129
- expect(cache.getStats().totalAdded).toBe(0);
130
- });
131
- it('应该正确导出为数组', () => {
132
- const array = cache.toArray();
133
- expect(array).toEqual(['test1', 'test2']);
134
- });
135
- it('应该正确导出为JSON', () => {
136
- const json = cache.toJSON();
137
- const parsed = JSON.parse(json);
138
- expect(parsed).toHaveLength(2);
139
- expect(parsed[0].data).toBe('test1');
140
- expect(parsed[1].data).toBe('test2');
141
- });
142
- });
143
- describe('迭代器功能', () => {
144
- it('应该支持for...of语法', () => {
145
- cache.add('a');
146
- cache.add('b');
147
- cache.add('c');
148
- const results = [];
149
- for (const entry of cache) {
150
- results.push(entry.data);
151
- }
152
- expect(results).toEqual(['a', 'b', 'c']);
153
- });
154
- });
155
- describe('边界条件', () => {
156
- it('应该拒绝无效的容量', () => {
157
- expect(() => new RingCache({ capacity: 0 })).toThrow('Capacity must be greater than 0');
158
- expect(() => new RingCache({ capacity: -1 })).toThrow('Capacity must be greater than 0');
159
- });
160
- it('应该正确处理空缓存的操作', () => {
161
- const emptyCache = new RingCache();
162
- expect(emptyCache.getAll()).toEqual([]);
163
- expect(emptyCache.getRecent(1)).toEqual([]);
164
- expect(emptyCache.getOldest(1)).toEqual([]);
165
- expect(emptyCache.toArray()).toEqual([]);
166
- });
167
- it('应该正确处理单个容量的缓存', () => {
168
- const singleCache = new RingCache({ capacity: 1 });
169
- singleCache.add('first');
170
- expect(singleCache.getAll().map(e => e.data)).toEqual(['first']);
171
- singleCache.add('second');
172
- expect(singleCache.getAll().map(e => e.data)).toEqual(['second']);
173
- });
174
- });
175
- describe('复杂数据类型', () => {
176
- it('应该支持对象数据', () => {
177
- const objectCache = new RingCache();
178
- const obj1 = { name: 'test1', value: 1 };
179
- const obj2 = { name: 'test2', value: 2 };
180
- objectCache.add(obj1);
181
- objectCache.add(obj2);
182
- const all = objectCache.getAll();
183
- expect(all[0].data).toEqual(obj1);
184
- expect(all[1].data).toEqual(obj2);
185
- });
186
- it('应该支持搜索对象属性', () => {
187
- const objectCache = new RingCache();
188
- objectCache.add({ name: 'apple', type: 'fruit' });
189
- objectCache.add({ name: 'carrot', type: 'vegetable' });
190
- objectCache.add({ name: 'banana', type: 'fruit' });
191
- const fruits = objectCache.search(entry => entry.data.type === 'fruit');
192
- expect(fruits.length).toBe(2);
193
- expect(fruits.map(e => e.data.name)).toEqual(['apple', 'banana']);
194
- });
195
- });
196
- });
@@ -1,2 +0,0 @@
1
- export { Service } from './service.js';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA"}
@@ -1 +0,0 @@
1
- export { Service } from './service.js';
@@ -1,6 +0,0 @@
1
- /** */
2
- export declare abstract class Service {
3
- abstract inited: boolean;
4
- abstract init(): Promise<boolean>;
5
- }
6
- //# sourceMappingURL=service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../source/service/service.ts"],"names":[],"mappings":"AAAA,MAAM;AACN,8BAAsB,OAAO;IAC3B,SAAgB,MAAM,EAAE,OAAO,CAAA;aACf,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC;CACzC"}
@@ -1,3 +0,0 @@
1
- /** */
2
- export class Service {
3
- }
@@ -1,2 +0,0 @@
1
- export * from './string.js';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/string/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA"}
@@ -1 +0,0 @@
1
- export * from './string.js';
@@ -1,3 +0,0 @@
1
- /** 生成随机字符串,可以指定字符集和长度 */
2
- export declare function randomString(length: number, charset?: string): string;
3
- //# sourceMappingURL=string.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../source/string/string.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,MAAyE,GAAG,MAAM,CAOvI"}
@@ -1,9 +0,0 @@
1
- /** 生成随机字符串,可以指定字符集和长度 */
2
- export function randomString(length, charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
3
- let result = '';
4
- const charsetLength = charset.length;
5
- for (let i = 0; i < length; i++) {
6
- result += charset.charAt(Math.floor(Math.random() * charsetLength));
7
- }
8
- return result;
9
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=string.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"string.test.d.ts","sourceRoot":"","sources":["../../source/string/string.test.ts"],"names":[],"mappings":""}
@@ -1,77 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import { randomString } from './string.js';
3
- describe('randomString 函数', () => {
4
- beforeEach(() => {
5
- // 重置随机数生成器的模拟
6
- vi.clearAllMocks();
7
- });
8
- it('应该生成指定长度的字符串', () => {
9
- const result = randomString(10);
10
- expect(result).toHaveLength(10);
11
- });
12
- it('应该为零长度生成空字符串', () => {
13
- const result = randomString(0);
14
- expect(result).toBe('');
15
- });
16
- it('应该在未指定时使用默认字符集', () => {
17
- const result = randomString(100);
18
- const defaultCharset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
19
- // 检查结果只包含默认字符集中的字符
20
- for (const char of result) {
21
- expect(defaultCharset).toContain(char);
22
- }
23
- });
24
- it('应该在提供时使用自定义字符集', () => {
25
- const customCharset = '123';
26
- const result = randomString(10, customCharset);
27
- // 检查结果只包含自定义字符集中的字符
28
- for (const char of result) {
29
- expect(customCharset).toContain(char);
30
- }
31
- });
32
- it('应该在多次调用时生成不同的字符串', () => {
33
- const result1 = randomString(20);
34
- const result2 = randomString(20);
35
- // 虽然理论上可能相同,但实际上几乎不可能
36
- expect(result1).not.toBe(result2);
37
- });
38
- it('应该对单字符字符集正常工作', () => {
39
- const result = randomString(5, 'A');
40
- expect(result).toBe('AAAAA');
41
- });
42
- it('应该对字符集中的特殊字符正常工作', () => {
43
- const specialCharset = '!@#$%^&*()';
44
- const result = randomString(10, specialCharset);
45
- expect(result).toHaveLength(10);
46
- // 检查结果只包含特殊字符集中的字符
47
- for (const char of result) {
48
- expect(specialCharset).toContain(char);
49
- }
50
- });
51
- it('应该对字符集中的Unicode字符正常工作', () => {
52
- const unicodeCharset = '你好世界🎉';
53
- const result = randomString(5, unicodeCharset);
54
- expect(result).toHaveLength(5);
55
- // 检查结果只包含Unicode字符集中的字符
56
- for (const char of result) {
57
- expect(unicodeCharset).toContain(char);
58
- }
59
- });
60
- it('应该处理大长度值', () => {
61
- const result = randomString(1000);
62
- expect(result).toHaveLength(1000);
63
- });
64
- it('应该对固定的Math.random具有确定性', () => {
65
- // 模拟Math.random返回固定值
66
- const mockRandom = vi.spyOn(Math, 'random');
67
- mockRandom.mockReturnValueOnce(0); // 第一个字符
68
- mockRandom.mockReturnValueOnce(0.5); // 第二个字符
69
- const charset = 'ABC';
70
- const result = randomString(2, charset);
71
- // 基于模拟的随机值,应该返回特定的字符
72
- // Math.floor(0 * 3) = 0 -> 'A'
73
- // Math.floor(0.5 * 3) = 1 -> 'B'
74
- expect(result).toBe('AB');
75
- mockRandom.mockRestore();
76
- });
77
- });
@@ -1,2 +0,0 @@
1
- export * from './throttle.js';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/throttle/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA"}
@@ -1 +0,0 @@
1
- export * from './throttle.js';
@@ -1,12 +0,0 @@
1
- /**
2
- * 节流装饰器 - 基于 TC39 Stage 3 Decorators 提案
3
- * 限制函数在指定时间内最多执行一次
4
- */
5
- interface ThrottleOptions {
6
- leading?: boolean;
7
- trailing?: boolean;
8
- }
9
- export declare function throttleFn<T extends (...args: any[]) => any>(fn: T, wait: number, options?: ThrottleOptions): T;
10
- export declare function throttle<T extends (...args: any[]) => any>(wait: number, options?: ThrottleOptions): (originalMethod: T, context: ClassMethodDecoratorContext) => T;
11
- export type { ThrottleOptions };
12
- //# sourceMappingURL=throttle.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../../source/throttle/throttle.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,UAAU,eAAe;IACvB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAGD,wBAAgB,UAAU,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAC1D,EAAE,EAAE,CAAC,EACL,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,eAAoB,GAC5B,CAAC,CAuCH;AAGD,wBAAgB,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EACxD,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,eAAe,IAER,gBAAgB,CAAC,EAAE,SAAS,2BAA2B,KAAG,CAAC,CAY7E;AAED,YAAY,EAAE,eAAe,EAAE,CAAA"}
@@ -1,55 +0,0 @@
1
- /**
2
- * 节流装饰器 - 基于 TC39 Stage 3 Decorators 提案
3
- * 限制函数在指定时间内最多执行一次
4
- */
5
- // 通用节流函数
6
- export function throttleFn(fn, wait, options = {}) {
7
- const { leading = true, trailing = true } = options;
8
- let timeout = null;
9
- let lastCallTime = 0;
10
- let lastArgs;
11
- let lastThis;
12
- let result;
13
- function throttled(...args) {
14
- const now = Date.now();
15
- // 如果是第一次调用且不允许 leading
16
- if (lastCallTime === 0 && !leading) {
17
- lastCallTime = now;
18
- }
19
- const remaining = wait - (now - lastCallTime);
20
- lastArgs = args;
21
- lastThis = this;
22
- if (remaining <= 0 || remaining > wait) {
23
- if (timeout) {
24
- clearTimeout(timeout);
25
- timeout = null;
26
- }
27
- lastCallTime = now;
28
- result = fn.apply(this, args);
29
- }
30
- else if (!timeout && trailing) {
31
- timeout = setTimeout(() => {
32
- lastCallTime = leading ? Date.now() : 0;
33
- timeout = null;
34
- result = fn.apply(lastThis, lastArgs);
35
- }, remaining);
36
- }
37
- return result;
38
- }
39
- return throttled;
40
- }
41
- // TC39 Stage 3 装饰器实现
42
- export function throttle(wait, options) {
43
- return function (originalMethod, context) {
44
- let methodKey = context.name;
45
- context.addInitializer(function () {
46
- const self = this;
47
- // 只 patch 原型上的原始方法,避免多层嵌套
48
- const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(self), methodKey);
49
- const method = descriptor?.value ?? originalMethod;
50
- const throttledMethod = throttleFn(method.bind(self), wait, options);
51
- self[methodKey] = throttledMethod;
52
- });
53
- return originalMethod;
54
- };
55
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=throttle.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"throttle.test.d.ts","sourceRoot":"","sources":["../../source/throttle/throttle.test.ts"],"names":[],"mappings":""}