@procedures/collections 0.46.0 → 0.47.2

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 (233) hide show
  1. package/CHANGELOG.md +42 -8
  2. package/README.md +30 -0
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -58
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -94
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/index.d.ts +2 -2
  9. package/dist/storage/index.d.ts.map +1 -1
  10. package/dist/storage/index.js +1 -1
  11. package/dist/storage/index.js.map +1 -1
  12. package/dist/storage/memory.d.ts +1 -1
  13. package/dist/storage/memory.d.ts.map +1 -1
  14. package/dist/storage/memory.js +1 -1
  15. package/dist/storage/memory.js.map +1 -1
  16. package/dist/testing/index.d.ts +1 -1
  17. package/dist/testing/index.d.ts.map +1 -1
  18. package/dist/testing/index.js +1 -1
  19. package/dist/testing/index.js.map +1 -1
  20. package/dist/testing/storage-contract.js +1 -1
  21. package/dist/testing/storage-contract.js.map +1 -1
  22. package/dist/undo/index.d.ts +1 -1
  23. package/dist/undo/index.js +1 -1
  24. package/dist/undo/undo-log.d.ts +1 -1
  25. package/dist/undo/undo-log.js +1 -1
  26. package/package.json +6 -6
  27. package/src/index.ts +338 -213
  28. package/src/storage/index.ts +2 -2
  29. package/src/storage/memory.ts +1 -1
  30. package/src/testing/index.ts +1 -1
  31. package/src/testing/storage-contract.ts +1 -1
  32. package/src/undo/index.ts +1 -1
  33. package/src/undo/undo-log.ts +1 -1
  34. package/dist/async/async-queue.d.ts +0 -170
  35. package/dist/async/async-queue.d.ts.map +0 -1
  36. package/dist/async/async-queue.js +0 -404
  37. package/dist/async/async-queue.js.map +0 -1
  38. package/dist/async/channels.d.ts +0 -211
  39. package/dist/async/channels.d.ts.map +0 -1
  40. package/dist/async/channels.js +0 -393
  41. package/dist/async/channels.js.map +0 -1
  42. package/dist/async/settled.d.ts +0 -23
  43. package/dist/async/settled.d.ts.map +0 -1
  44. package/dist/async/settled.js +0 -32
  45. package/dist/async/settled.js.map +0 -1
  46. package/dist/behaviors/bounded.d.ts +0 -79
  47. package/dist/behaviors/bounded.d.ts.map +0 -1
  48. package/dist/behaviors/bounded.js +0 -393
  49. package/dist/behaviors/bounded.js.map +0 -1
  50. package/dist/behaviors/composite.d.ts +0 -17
  51. package/dist/behaviors/composite.d.ts.map +0 -1
  52. package/dist/behaviors/composite.js +0 -93
  53. package/dist/behaviors/composite.js.map +0 -1
  54. package/dist/behaviors/evented.d.ts +0 -154
  55. package/dist/behaviors/evented.d.ts.map +0 -1
  56. package/dist/behaviors/evented.js +0 -299
  57. package/dist/behaviors/evented.js.map +0 -1
  58. package/dist/behaviors/lru.d.ts +0 -84
  59. package/dist/behaviors/lru.d.ts.map +0 -1
  60. package/dist/behaviors/lru.js +0 -376
  61. package/dist/behaviors/lru.js.map +0 -1
  62. package/dist/behaviors/readonly.d.ts +0 -73
  63. package/dist/behaviors/readonly.d.ts.map +0 -1
  64. package/dist/behaviors/readonly.js +0 -189
  65. package/dist/behaviors/readonly.js.map +0 -1
  66. package/dist/behaviors/safe.d.ts +0 -163
  67. package/dist/behaviors/safe.d.ts.map +0 -1
  68. package/dist/behaviors/safe.js +0 -176
  69. package/dist/behaviors/safe.js.map +0 -1
  70. package/dist/behaviors/synchronized.d.ts +0 -88
  71. package/dist/behaviors/synchronized.d.ts.map +0 -1
  72. package/dist/behaviors/synchronized.js +0 -285
  73. package/dist/behaviors/synchronized.js.map +0 -1
  74. package/dist/behaviors/ttl.d.ts +0 -118
  75. package/dist/behaviors/ttl.d.ts.map +0 -1
  76. package/dist/behaviors/ttl.js +0 -354
  77. package/dist/behaviors/ttl.js.map +0 -1
  78. package/dist/contracts/model.d.ts +0 -29
  79. package/dist/contracts/model.d.ts.map +0 -1
  80. package/dist/contracts/model.js +0 -26
  81. package/dist/contracts/model.js.map +0 -1
  82. package/dist/core/effects.d.ts +0 -176
  83. package/dist/core/effects.d.ts.map +0 -1
  84. package/dist/core/effects.js +0 -180
  85. package/dist/core/effects.js.map +0 -1
  86. package/dist/core/events.d.ts +0 -192
  87. package/dist/core/events.d.ts.map +0 -1
  88. package/dist/core/events.js +0 -291
  89. package/dist/core/events.js.map +0 -1
  90. package/dist/core/middleware.d.ts +0 -231
  91. package/dist/core/middleware.d.ts.map +0 -1
  92. package/dist/core/middleware.js +0 -161
  93. package/dist/core/middleware.js.map +0 -1
  94. package/dist/core/policies.d.ts +0 -220
  95. package/dist/core/policies.d.ts.map +0 -1
  96. package/dist/core/policies.js +0 -48
  97. package/dist/core/policies.js.map +0 -1
  98. package/dist/core/traits.d.ts +0 -142
  99. package/dist/core/traits.d.ts.map +0 -1
  100. package/dist/core/traits.js +0 -8
  101. package/dist/core/traits.js.map +0 -1
  102. package/dist/fx/collectors.d.ts +0 -193
  103. package/dist/fx/collectors.d.ts.map +0 -1
  104. package/dist/fx/collectors.js +0 -375
  105. package/dist/fx/collectors.js.map +0 -1
  106. package/dist/fx/iter.d.ts +0 -166
  107. package/dist/fx/iter.d.ts.map +0 -1
  108. package/dist/fx/iter.js +0 -421
  109. package/dist/fx/iter.js.map +0 -1
  110. package/dist/impl/array-deque.d.ts +0 -112
  111. package/dist/impl/array-deque.d.ts.map +0 -1
  112. package/dist/impl/array-deque.js +0 -380
  113. package/dist/impl/array-deque.js.map +0 -1
  114. package/dist/impl/array-list.d.ts +0 -101
  115. package/dist/impl/array-list.d.ts.map +0 -1
  116. package/dist/impl/array-list.js +0 -387
  117. package/dist/impl/array-list.js.map +0 -1
  118. package/dist/impl/hash-map.d.ts +0 -149
  119. package/dist/impl/hash-map.d.ts.map +0 -1
  120. package/dist/impl/hash-map.js +0 -473
  121. package/dist/impl/hash-map.js.map +0 -1
  122. package/dist/impl/hash-set.d.ts +0 -125
  123. package/dist/impl/hash-set.d.ts.map +0 -1
  124. package/dist/impl/hash-set.js +0 -264
  125. package/dist/impl/hash-set.js.map +0 -1
  126. package/dist/impl/linked-hash-map.d.ts +0 -141
  127. package/dist/impl/linked-hash-map.d.ts.map +0 -1
  128. package/dist/impl/linked-hash-map.js +0 -442
  129. package/dist/impl/linked-hash-map.js.map +0 -1
  130. package/dist/impl/linked-list.d.ts +0 -109
  131. package/dist/impl/linked-list.d.ts.map +0 -1
  132. package/dist/impl/linked-list.js +0 -494
  133. package/dist/impl/linked-list.js.map +0 -1
  134. package/dist/impl/priority-queue.d.ts +0 -139
  135. package/dist/impl/priority-queue.d.ts.map +0 -1
  136. package/dist/impl/priority-queue.js +0 -343
  137. package/dist/impl/priority-queue.js.map +0 -1
  138. package/dist/impl/tree-map.d.ts +0 -153
  139. package/dist/impl/tree-map.d.ts.map +0 -1
  140. package/dist/impl/tree-map.js +0 -788
  141. package/dist/impl/tree-map.js.map +0 -1
  142. package/dist/impl/tree-set.d.ts +0 -112
  143. package/dist/impl/tree-set.d.ts.map +0 -1
  144. package/dist/impl/tree-set.js +0 -374
  145. package/dist/impl/tree-set.js.map +0 -1
  146. package/dist/interfaces/collection.d.ts +0 -119
  147. package/dist/interfaces/collection.d.ts.map +0 -1
  148. package/dist/interfaces/collection.js +0 -8
  149. package/dist/interfaces/collection.js.map +0 -1
  150. package/dist/interfaces/list.d.ts +0 -247
  151. package/dist/interfaces/list.d.ts.map +0 -1
  152. package/dist/interfaces/list.js +0 -8
  153. package/dist/interfaces/list.js.map +0 -1
  154. package/dist/interfaces/map.d.ts +0 -344
  155. package/dist/interfaces/map.d.ts.map +0 -1
  156. package/dist/interfaces/map.js +0 -8
  157. package/dist/interfaces/map.js.map +0 -1
  158. package/dist/interfaces/queue.d.ts +0 -304
  159. package/dist/interfaces/queue.d.ts.map +0 -1
  160. package/dist/interfaces/queue.js +0 -8
  161. package/dist/interfaces/queue.js.map +0 -1
  162. package/dist/interfaces/set.d.ts +0 -137
  163. package/dist/interfaces/set.d.ts.map +0 -1
  164. package/dist/interfaces/set.js +0 -8
  165. package/dist/interfaces/set.js.map +0 -1
  166. package/dist/order/index.d.ts +0 -7
  167. package/dist/order/index.d.ts.map +0 -1
  168. package/dist/order/index.js +0 -7
  169. package/dist/order/index.js.map +0 -1
  170. package/dist/order/rank.d.ts +0 -100
  171. package/dist/order/rank.d.ts.map +0 -1
  172. package/dist/order/rank.js +0 -286
  173. package/dist/order/rank.js.map +0 -1
  174. package/dist/storage/listeners.d.ts +0 -39
  175. package/dist/storage/listeners.d.ts.map +0 -1
  176. package/dist/storage/listeners.js +0 -65
  177. package/dist/storage/listeners.js.map +0 -1
  178. package/dist/testing/check.d.ts +0 -25
  179. package/dist/testing/check.d.ts.map +0 -1
  180. package/dist/testing/check.js +0 -85
  181. package/dist/testing/check.js.map +0 -1
  182. package/dist/utils/defaults.d.ts +0 -135
  183. package/dist/utils/defaults.d.ts.map +0 -1
  184. package/dist/utils/defaults.js +0 -322
  185. package/dist/utils/defaults.js.map +0 -1
  186. package/dist/utils/factories.d.ts +0 -144
  187. package/dist/utils/factories.d.ts.map +0 -1
  188. package/dist/utils/factories.js +0 -293
  189. package/dist/utils/factories.js.map +0 -1
  190. package/dist/utils/helpers.d.ts +0 -137
  191. package/dist/utils/helpers.d.ts.map +0 -1
  192. package/dist/utils/helpers.js +0 -221
  193. package/dist/utils/helpers.js.map +0 -1
  194. package/src/async/async-queue.ts +0 -481
  195. package/src/async/channels.ts +0 -450
  196. package/src/async/settled.ts +0 -41
  197. package/src/behaviors/bounded.ts +0 -485
  198. package/src/behaviors/composite.ts +0 -99
  199. package/src/behaviors/evented.ts +0 -464
  200. package/src/behaviors/lru.ts +0 -447
  201. package/src/behaviors/readonly.ts +0 -236
  202. package/src/behaviors/safe.ts +0 -372
  203. package/src/behaviors/synchronized.ts +0 -316
  204. package/src/behaviors/ttl.ts +0 -462
  205. package/src/contracts/model.ts +0 -44
  206. package/src/core/effects.ts +0 -323
  207. package/src/core/events.ts +0 -356
  208. package/src/core/middleware.ts +0 -415
  209. package/src/core/policies.ts +0 -316
  210. package/src/core/traits.ts +0 -164
  211. package/src/fx/collectors.ts +0 -500
  212. package/src/fx/iter.ts +0 -536
  213. package/src/impl/array-deque.ts +0 -463
  214. package/src/impl/array-list.ts +0 -490
  215. package/src/impl/hash-map.ts +0 -623
  216. package/src/impl/hash-set.ts +0 -342
  217. package/src/impl/linked-hash-map.ts +0 -572
  218. package/src/impl/linked-list.ts +0 -606
  219. package/src/impl/priority-queue.ts +0 -448
  220. package/src/impl/tree-map.ts +0 -912
  221. package/src/impl/tree-set.ts +0 -469
  222. package/src/interfaces/collection.ts +0 -141
  223. package/src/interfaces/list.ts +0 -281
  224. package/src/interfaces/map.ts +0 -405
  225. package/src/interfaces/queue.ts +0 -353
  226. package/src/interfaces/set.ts +0 -158
  227. package/src/order/index.ts +0 -6
  228. package/src/order/rank.ts +0 -271
  229. package/src/storage/listeners.ts +0 -72
  230. package/src/testing/check.ts +0 -81
  231. package/src/utils/defaults.ts +0 -345
  232. package/src/utils/factories.ts +0 -357
  233. package/src/utils/helpers.ts +0 -248
@@ -1,447 +0,0 @@
1
- /**
2
- * LRU (Least Recently Used) eviction behavior.
3
- *
4
- * Tracks access times and automatically evicts the least recently used
5
- * elements when capacity is reached. Perfect for caching.
6
- *
7
- * @example
8
- * const cache = compose(
9
- * lruMap({ capacity: 100 })
10
- * )(hashMap<string, Data>())
11
- *
12
- * cache.set('key1', data1) // Accessed
13
- * cache.get('key1') // Accessed again (moves to front)
14
- * // When full, least recently accessed keys are evicted
15
- */
16
-
17
- import type { Middleware } from "../core/middleware.js";
18
- import type { MapLike } from "../interfaces/map.js";
19
- import { compositeMapMethod } from "./composite.js";
20
-
21
- /**
22
- * Options for LRU behavior.
23
- */
24
- export interface LRUOptions {
25
- /**
26
- * Maximum capacity. When exceeded, least recently used items are evicted.
27
- */
28
- capacity: number;
29
-
30
- /**
31
- * Optional callback invoked when an item is evicted.
32
- */
33
- onEvict?: (info: { key: any; value: any; timestamp: number }) => void;
34
- }
35
-
36
- /**
37
- * Metadata for tracking LRU access.
38
- */
39
- interface LRUNode<K, V> {
40
- key: K;
41
- value: V;
42
- lastAccessed: number;
43
- prev: LRUNode<K, V> | null;
44
- next: LRUNode<K, V> | null;
45
- }
46
-
47
- /**
48
- * Creates an LRU Map middleware.
49
- *
50
- * Maintains a doubly-linked list to track access order.
51
- * Most recently accessed items are at the head.
52
- * When capacity is exceeded, items at the tail are evicted.
53
- */
54
- export function lruMap<K, V>(
55
- options: LRUOptions
56
- ): Middleware<MapLike<K, V>, MapLike<K, V> & { readonly isFull: boolean }> {
57
- const { capacity, onEvict } = options;
58
-
59
- return (next: MapLike<K, V>): MapLike<K, V> & { readonly isFull: boolean } => {
60
- // Doubly-linked list for LRU tracking
61
- let head: LRUNode<K, V> | null = null;
62
- let tail: LRUNode<K, V> | null = null;
63
- const nodeMap = new Map<K, LRUNode<K, V>>();
64
-
65
- const moveToHead = (node: LRUNode<K, V>): void => {
66
- // Remove from current position
67
- if (node.prev) {
68
- node.prev.next = node.next;
69
- } else {
70
- head = node.next;
71
- }
72
-
73
- if (node.next) {
74
- node.next.prev = node.prev;
75
- } else {
76
- tail = node.prev;
77
- }
78
-
79
- // Add to head
80
- node.prev = null;
81
- node.next = head;
82
- if (head) {
83
- head.prev = node;
84
- }
85
- head = node;
86
-
87
- if (!tail) {
88
- tail = node;
89
- }
90
-
91
- node.lastAccessed = Date.now();
92
- };
93
-
94
- const addNode = (key: K, value: V): void => {
95
- const node: LRUNode<K, V> = {
96
- key,
97
- value,
98
- lastAccessed: Date.now(),
99
- prev: null,
100
- next: head,
101
- };
102
-
103
- if (head) {
104
- head.prev = node;
105
- }
106
- head = node;
107
-
108
- if (!tail) {
109
- tail = node;
110
- }
111
-
112
- nodeMap.set(key, node);
113
- };
114
-
115
- const removeNode = (node: LRUNode<K, V>): void => {
116
- if (node.prev) {
117
- node.prev.next = node.next;
118
- } else {
119
- head = node.next;
120
- }
121
-
122
- if (node.next) {
123
- node.next.prev = node.prev;
124
- } else {
125
- tail = node.prev;
126
- }
127
-
128
- nodeMap.delete(node.key);
129
- };
130
-
131
- // A layer below this one (for example ttlMap, in compose(lruMap, ttlMap)) can remove an
132
- // entry without this layer seeing it. Its node is then stale: the list must not keep it
133
- // (it holds the value, and it would grow without bound) and evicting it is not an eviction.
134
- const dropStaleNodes = (): void => {
135
- for (const node of [...nodeMap.values()]) {
136
- if (!next.has(node.key)) {
137
- removeNode(node);
138
- }
139
- }
140
- };
141
-
142
- const evictLRU = (): void => {
143
- while (tail) {
144
- const candidate = tail;
145
- removeNode(candidate);
146
- if (!next.has(candidate.key)) {
147
- // Already removed below: skip it and evict the next least recently used entry
148
- continue;
149
- }
150
- next.delete(candidate.key);
151
-
152
- if (onEvict) {
153
- onEvict({
154
- key: candidate.key,
155
- value: candidate.value,
156
- timestamp: candidate.lastAccessed,
157
- });
158
- }
159
- return;
160
- }
161
- };
162
-
163
- return new Proxy(next, {
164
- get(target, prop, receiver) {
165
- // Add isFull property
166
- if (prop === "isFull") {
167
- return target.size >= capacity;
168
- }
169
-
170
- // Composite methods (setIfAbsent, compute, merge, putAll and others) go through this
171
- // proxy's own primitives, so they keep this behavior's rules (BUGS-2026-07 L13)
172
- const composite = compositeMapMethod(receiver as MapLike<K, V>, prop);
173
- if (composite) {
174
- return composite;
175
- }
176
-
177
- const value = Reflect.get(target, prop, receiver);
178
-
179
- if (typeof value === "function") {
180
- switch (prop) {
181
- case "get":
182
- return function (key: K): V {
183
- let result: V;
184
- try {
185
- result = (value as Function).call(target, key);
186
- } catch (error) {
187
- // The entry is gone below (for example expired): forget its node
188
- const stale = nodeMap.get(key);
189
- if (stale) {
190
- removeNode(stale);
191
- }
192
- throw error;
193
- }
194
- // Update access time
195
- const node = nodeMap.get(key);
196
- if (node) {
197
- moveToHead(node);
198
- }
199
- return result;
200
- };
201
-
202
- case "has":
203
- return function (key: K): boolean {
204
- const result = (value as Function).call(target, key);
205
- const node = nodeMap.get(key);
206
- if (node) {
207
- if (result) {
208
- // Update access time
209
- moveToHead(node);
210
- } else {
211
- // The entry is gone below (for example expired): forget its node
212
- removeNode(node);
213
- }
214
- }
215
- return result;
216
- };
217
-
218
- case "set":
219
- return function (key: K, val: V): V | undefined {
220
- const hadKey = target.has(key);
221
-
222
- if (hadKey) {
223
- // Update existing
224
- const oldValue = (value as Function).call(target, key, val);
225
- const node = nodeMap.get(key);
226
- if (node) {
227
- node.value = val;
228
- moveToHead(node);
229
- }
230
- return oldValue;
231
- } else {
232
- // Add new. A stale node for this key (its entry was removed below) must not
233
- // stay in the list: evicting it later would delete the new node's map entry.
234
- const stale = nodeMap.get(key);
235
- if (stale) {
236
- removeNode(stale);
237
- }
238
- // Keep the node list bounded: when it reaches capacity, drop stale nodes first
239
- if (nodeMap.size >= capacity) {
240
- dropStaleNodes();
241
- }
242
- if (target.size >= capacity) {
243
- evictLRU();
244
- }
245
- const result = (value as Function).call(target, key, val);
246
- addNode(key, val);
247
- return result;
248
- }
249
- };
250
-
251
- case "delete":
252
- return function (key: K): V | undefined {
253
- const result = (value as Function).call(target, key);
254
- const node = nodeMap.get(key);
255
- if (node) {
256
- removeNode(node);
257
- }
258
- return result;
259
- };
260
-
261
- case "clear":
262
- return function (): void {
263
- (value as Function).call(target);
264
- head = null;
265
- tail = null;
266
- nodeMap.clear();
267
- };
268
- }
269
- }
270
-
271
- return value;
272
- },
273
- }) as MapLike<K, V> & { readonly isFull: boolean };
274
- };
275
- }
276
-
277
- /**
278
- * Creates an LRU cache with a simpler API.
279
- * This is a standalone LRU cache implementation.
280
- *
281
- * @example
282
- * const cache = lruCache<string, Data>(100)
283
- * cache.set('key', value)
284
- * cache.get('key') // Moves to front
285
- */
286
- export class LRUCache<K, V> {
287
- private map: Map<K, { value: V; node: LRUNode<K, V> }> = new Map();
288
- private head: LRUNode<K, V> | null = null;
289
- private tail: LRUNode<K, V> | null = null;
290
-
291
- constructor(
292
- private readonly capacity: number,
293
- private readonly onEvict?: (key: K, value: V) => void
294
- ) {}
295
-
296
- get size(): number {
297
- return this.map.size;
298
- }
299
-
300
- get isFull(): boolean {
301
- return this.map.size >= this.capacity;
302
- }
303
-
304
- get(key: K): V | undefined {
305
- const entry = this.map.get(key);
306
- if (!entry) return undefined;
307
-
308
- this.moveToHead(entry.node);
309
- return entry.value;
310
- }
311
-
312
- set(key: K, value: V): void {
313
- const entry = this.map.get(key);
314
-
315
- if (entry) {
316
- // Update existing
317
- entry.value = value;
318
- entry.node.value = value;
319
- this.moveToHead(entry.node);
320
- } else {
321
- // Add new
322
- if (this.map.size >= this.capacity) {
323
- this.evictLRU();
324
- }
325
-
326
- const node: LRUNode<K, V> = {
327
- key,
328
- value,
329
- lastAccessed: Date.now(),
330
- prev: null,
331
- next: this.head,
332
- };
333
-
334
- if (this.head) {
335
- this.head.prev = node;
336
- }
337
- this.head = node;
338
-
339
- if (!this.tail) {
340
- this.tail = node;
341
- }
342
-
343
- this.map.set(key, { value, node });
344
- }
345
- }
346
-
347
- has(key: K): boolean {
348
- return this.map.has(key);
349
- }
350
-
351
- delete(key: K): boolean {
352
- const entry = this.map.get(key);
353
- if (!entry) return false;
354
-
355
- this.removeNode(entry.node);
356
- this.map.delete(key);
357
- return true;
358
- }
359
-
360
- clear(): void {
361
- this.map.clear();
362
- this.head = null;
363
- this.tail = null;
364
- }
365
-
366
- private moveToHead(node: LRUNode<K, V>): void {
367
- if (node === this.head) return;
368
-
369
- // Remove from current position
370
- if (node.prev) {
371
- node.prev.next = node.next;
372
- }
373
- if (node.next) {
374
- node.next.prev = node.prev;
375
- } else {
376
- this.tail = node.prev;
377
- }
378
-
379
- // Add to head
380
- node.prev = null;
381
- node.next = this.head;
382
- if (this.head) {
383
- this.head.prev = node;
384
- }
385
- this.head = node;
386
-
387
- node.lastAccessed = Date.now();
388
- }
389
-
390
- private removeNode(node: LRUNode<K, V>): void {
391
- if (node.prev) {
392
- node.prev.next = node.next;
393
- } else {
394
- this.head = node.next;
395
- }
396
-
397
- if (node.next) {
398
- node.next.prev = node.prev;
399
- } else {
400
- this.tail = node.prev;
401
- }
402
- }
403
-
404
- private evictLRU(): void {
405
- if (!this.tail) return;
406
-
407
- const evicted = this.tail;
408
- this.removeNode(evicted);
409
- this.map.delete(evicted.key);
410
-
411
- if (this.onEvict) {
412
- this.onEvict(evicted.key, evicted.value);
413
- }
414
- }
415
-
416
- /**
417
- * Returns keys in LRU order (most recent first).
418
- */
419
- *keys(): IterableIterator<K> {
420
- let node = this.head;
421
- while (node) {
422
- yield node.key;
423
- node = node.next;
424
- }
425
- }
426
-
427
- /**
428
- * Returns entries in LRU order (most recent first).
429
- */
430
- *entries(): IterableIterator<[K, V]> {
431
- let node = this.head;
432
- while (node) {
433
- yield [node.key, node.value];
434
- node = node.next;
435
- }
436
- }
437
- }
438
-
439
- /**
440
- * Factory function to create an LRU cache.
441
- */
442
- export function lruCache<K, V>(
443
- capacity: number,
444
- onEvict?: (key: K, value: V) => void
445
- ): LRUCache<K, V> {
446
- return new LRUCache(capacity, onEvict);
447
- }
@@ -1,236 +0,0 @@
1
- /**
2
- * Readonly behavior - Creates immutable views of collections.
3
- *
4
- * Wraps collections to prevent modifications by throwing errors
5
- * on any mutation attempt. Mirrors Java's Collections.unmodifiable*() methods.
6
- *
7
- * @example
8
- * const list = compose(
9
- * readonlyList()
10
- * )(arrayList([1, 2, 3]))
11
- *
12
- * list.get(0) // OK: 1
13
- * list.add(4) // Error: Cannot modify readonly list
14
- */
15
-
16
- import type { Middleware } from "../core/middleware.js";
17
- import type { ReadonlyList } from "../interfaces/list.js";
18
- import type { ReadonlyQueue } from "../interfaces/queue.js";
19
- import type { ReadonlyMapLike } from "../interfaces/map.js";
20
- import type { ReadonlyCollection } from "../interfaces/collection.js";
21
-
22
- /**
23
- * Options for readonly behavior.
24
- */
25
- export interface ReadonlyOptions {
26
- /**
27
- * Custom error message to throw on mutation attempts.
28
- * @default 'Cannot modify readonly collection'
29
- */
30
- errorMessage?: string;
31
- }
32
-
33
- /**
34
- * Set of method names that mutate collections.
35
- */
36
- const MUTATING_METHODS = new Set([
37
- // Collection mutations
38
- "add",
39
- "addAll",
40
- "remove",
41
- "removeAll",
42
- "retainAll",
43
- "clear",
44
-
45
- // List mutations
46
- "set",
47
- "insert",
48
- "insertAll",
49
- "removeAt",
50
- "removeRange",
51
- "push",
52
- "pop",
53
- "shift",
54
- "unshift",
55
- "sort",
56
- "reverse",
57
-
58
- // Queue/Deque mutations
59
- "offer",
60
- "offerFirst",
61
- "offerLast",
62
- "poll",
63
- "pollFirst",
64
- "pollLast",
65
- "enqueue",
66
- "dequeue",
67
- "addFirst",
68
- "addLast",
69
- "removeFirst",
70
- "removeLast",
71
-
72
- // Map mutations
73
- "set",
74
- "setIfAbsent",
75
- "forcePut",
76
- "pollFirstEntry",
77
- "pollLastEntry",
78
- "delete",
79
- "deleteEntry",
80
- "replace",
81
- "replaceEntry",
82
- "computeIfAbsent",
83
- "computeIfPresent",
84
- "compute",
85
- "merge",
86
- "putAll",
87
- ]);
88
-
89
- /** Methods that return a view of the collection: the view is readonly too. */
90
- const VIEW_METHODS = new Set([
91
- "subList",
92
- "subMap",
93
- "subSet",
94
- "headMap",
95
- "tailMap",
96
- "headSet",
97
- "tailSet",
98
- "descendingMap",
99
- "descendingSet",
100
- "inverse",
101
- ]);
102
-
103
- /**
104
- * Creates a readonly middleware for any collection type.
105
- */
106
- function createReadonly<C extends object>(
107
- errorMessage: string = "Cannot modify readonly collection"
108
- ): Middleware<C> {
109
- return (next: C): C => {
110
- // The read methods, bound to the collection: one function for each name
111
- const bound = new Map<PropertyKey, { value: Function; method: Function }>();
112
- return new Proxy(next, {
113
- get(target, prop) {
114
- // A read runs on the collection itself, not on the proxy: a read that changes internal
115
- // state (a LinkedHashMap with accessOrder moves the entry) does not hit the set trap
116
- const value = Reflect.get(target, prop, target);
117
-
118
- // Block mutating methods
119
- if (typeof prop === "string" && MUTATING_METHODS.has(prop)) {
120
- if (typeof value === "function") {
121
- return () => {
122
- throw new Error(errorMessage);
123
- };
124
- }
125
- }
126
-
127
- // A view (a sublist, a submap, a head or tail set, a descending map) is readonly too.
128
- // Before, only subList and subMap were wrapped, and setIfAbsent, pollFirstEntry and
129
- // pollLastEntry were not blocked (BUGS-2026-07 L14).
130
- if (typeof value === "function" && typeof prop === "string" && VIEW_METHODS.has(prop)) {
131
- return function (this: any, ...args: any[]) {
132
- const result = value.apply(target, args);
133
- // Recursively apply readonly to the result
134
- return createReadonly<typeof result>(errorMessage)(result);
135
- };
136
- }
137
-
138
- if (typeof value === "function") {
139
- const known = bound.get(prop);
140
- if (known !== undefined && known.value === value) return known.method;
141
- const method = (value as Function).bind(target) as Function;
142
- bound.set(prop, { value, method });
143
- return method;
144
- }
145
-
146
- return value;
147
- },
148
-
149
- set(_target, _prop, _value, _receiver) {
150
- // Block property assignments
151
- throw new Error(errorMessage);
152
- },
153
-
154
- deleteProperty(_target, _prop) {
155
- // Block property deletions
156
- throw new Error(errorMessage);
157
- },
158
- });
159
- };
160
- }
161
-
162
- /**
163
- * Creates a readonly List middleware.
164
- *
165
- * All mutation methods will throw an error.
166
- * Read operations work normally.
167
- *
168
- * @example
169
- * const readonlyNumbers = compose(
170
- * readonlyList()
171
- * )(arrayList([1, 2, 3]))
172
- */
173
- export function readonlyList<T>(
174
- options: ReadonlyOptions = {}
175
- ): Middleware<ReadonlyList<T>> {
176
- const errorMessage = options.errorMessage ?? "Cannot modify readonly list";
177
- return createReadonly<any>(errorMessage);
178
- }
179
-
180
- /**
181
- * Creates a readonly Queue middleware.
182
- */
183
- export function readonlyQueue<T>(
184
- options: ReadonlyOptions = {}
185
- ): Middleware<ReadonlyQueue<T>> {
186
- const errorMessage = options.errorMessage ?? "Cannot modify readonly queue";
187
- return createReadonly<any>(errorMessage);
188
- }
189
-
190
- /**
191
- * Creates a readonly Map middleware.
192
- */
193
- export function readonlyMap<K, V>(
194
- options: ReadonlyOptions = {}
195
- ): Middleware<ReadonlyMapLike<K, V>> {
196
- const errorMessage = options.errorMessage ?? "Cannot modify readonly map";
197
- return createReadonly<any>(errorMessage);
198
- }
199
-
200
- /**
201
- * Creates a readonly Collection middleware.
202
- *
203
- * Generic version that works with any collection type.
204
- */
205
- export function readonlyCollection<T>(
206
- options: ReadonlyOptions = {}
207
- ): Middleware<ReadonlyCollection<T>> {
208
- const errorMessage = options.errorMessage ?? "Cannot modify readonly collection";
209
- return createReadonly<any>(errorMessage);
210
- }
211
-
212
- /**
213
- * Generic readonly middleware.
214
- * Alias for readonlyCollection.
215
- */
216
- export function readonly<C extends object>(
217
- options: ReadonlyOptions = {}
218
- ): Middleware<C> {
219
- const errorMessage = options.errorMessage ?? "Cannot modify readonly collection";
220
- return createReadonly<C>(errorMessage);
221
- }
222
-
223
- /**
224
- * Alias for Java compatibility: unmodifiableList
225
- */
226
- export const unmodifiableList : typeof readonlyList = readonlyList;
227
-
228
- /**
229
- * Alias for Java compatibility: unmodifiableMap
230
- */
231
- export const unmodifiableMap : typeof readonlyMap = readonlyMap;
232
-
233
- /**
234
- * Alias for Java compatibility: unmodifiableCollection
235
- */
236
- export const unmodifiableCollection : typeof readonlyCollection = readonlyCollection;