@procedures/collections 0.0.0-stage → 0.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.
Files changed (205) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +394 -2
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/async/async-queue.d.ts +155 -0
  6. package/dist/async/async-queue.d.ts.map +1 -0
  7. package/dist/async/async-queue.js +342 -0
  8. package/dist/async/async-queue.js.map +1 -0
  9. package/dist/async/channels.d.ts +188 -0
  10. package/dist/async/channels.d.ts.map +1 -0
  11. package/dist/async/channels.js +314 -0
  12. package/dist/async/channels.js.map +1 -0
  13. package/dist/behaviors/bounded.d.ts +79 -0
  14. package/dist/behaviors/bounded.d.ts.map +1 -0
  15. package/dist/behaviors/bounded.js +384 -0
  16. package/dist/behaviors/bounded.js.map +1 -0
  17. package/dist/behaviors/composite.d.ts +17 -0
  18. package/dist/behaviors/composite.d.ts.map +1 -0
  19. package/dist/behaviors/composite.js +93 -0
  20. package/dist/behaviors/composite.js.map +1 -0
  21. package/dist/behaviors/evented.d.ts +154 -0
  22. package/dist/behaviors/evented.d.ts.map +1 -0
  23. package/dist/behaviors/evented.js +299 -0
  24. package/dist/behaviors/evented.js.map +1 -0
  25. package/dist/behaviors/lru.d.ts +84 -0
  26. package/dist/behaviors/lru.d.ts.map +1 -0
  27. package/dist/behaviors/lru.js +376 -0
  28. package/dist/behaviors/lru.js.map +1 -0
  29. package/dist/behaviors/readonly.d.ts +73 -0
  30. package/dist/behaviors/readonly.d.ts.map +1 -0
  31. package/dist/behaviors/readonly.js +177 -0
  32. package/dist/behaviors/readonly.js.map +1 -0
  33. package/dist/behaviors/safe.d.ts +163 -0
  34. package/dist/behaviors/safe.d.ts.map +1 -0
  35. package/dist/behaviors/safe.js +176 -0
  36. package/dist/behaviors/safe.js.map +1 -0
  37. package/dist/behaviors/synchronized.d.ts +88 -0
  38. package/dist/behaviors/synchronized.d.ts.map +1 -0
  39. package/dist/behaviors/synchronized.js +285 -0
  40. package/dist/behaviors/synchronized.js.map +1 -0
  41. package/dist/behaviors/ttl.d.ts +118 -0
  42. package/dist/behaviors/ttl.d.ts.map +1 -0
  43. package/dist/behaviors/ttl.js +354 -0
  44. package/dist/behaviors/ttl.js.map +1 -0
  45. package/dist/contracts/model.d.ts +29 -0
  46. package/dist/contracts/model.d.ts.map +1 -0
  47. package/dist/contracts/model.js +26 -0
  48. package/dist/contracts/model.js.map +1 -0
  49. package/dist/core/effects.d.ts +176 -0
  50. package/dist/core/effects.d.ts.map +1 -0
  51. package/dist/core/effects.js +180 -0
  52. package/dist/core/effects.js.map +1 -0
  53. package/dist/core/events.d.ts +192 -0
  54. package/dist/core/events.d.ts.map +1 -0
  55. package/dist/core/events.js +291 -0
  56. package/dist/core/events.js.map +1 -0
  57. package/dist/core/middleware.d.ts +231 -0
  58. package/dist/core/middleware.d.ts.map +1 -0
  59. package/dist/core/middleware.js +161 -0
  60. package/dist/core/middleware.js.map +1 -0
  61. package/dist/core/policies.d.ts +220 -0
  62. package/dist/core/policies.d.ts.map +1 -0
  63. package/dist/core/policies.js +48 -0
  64. package/dist/core/policies.js.map +1 -0
  65. package/dist/core/traits.d.ts +142 -0
  66. package/dist/core/traits.d.ts.map +1 -0
  67. package/dist/core/traits.js +8 -0
  68. package/dist/core/traits.js.map +1 -0
  69. package/dist/fx/collectors.d.ts +193 -0
  70. package/dist/fx/collectors.d.ts.map +1 -0
  71. package/dist/fx/collectors.js +375 -0
  72. package/dist/fx/collectors.js.map +1 -0
  73. package/dist/fx/iter.d.ts +166 -0
  74. package/dist/fx/iter.d.ts.map +1 -0
  75. package/dist/fx/iter.js +421 -0
  76. package/dist/fx/iter.js.map +1 -0
  77. package/dist/impl/array-deque.d.ts +112 -0
  78. package/dist/impl/array-deque.d.ts.map +1 -0
  79. package/dist/impl/array-deque.js +380 -0
  80. package/dist/impl/array-deque.js.map +1 -0
  81. package/dist/impl/array-list.d.ts +101 -0
  82. package/dist/impl/array-list.d.ts.map +1 -0
  83. package/dist/impl/array-list.js +387 -0
  84. package/dist/impl/array-list.js.map +1 -0
  85. package/dist/impl/hash-map.d.ts +149 -0
  86. package/dist/impl/hash-map.d.ts.map +1 -0
  87. package/dist/impl/hash-map.js +473 -0
  88. package/dist/impl/hash-map.js.map +1 -0
  89. package/dist/impl/hash-set.d.ts +125 -0
  90. package/dist/impl/hash-set.d.ts.map +1 -0
  91. package/dist/impl/hash-set.js +264 -0
  92. package/dist/impl/hash-set.js.map +1 -0
  93. package/dist/impl/linked-hash-map.d.ts +141 -0
  94. package/dist/impl/linked-hash-map.d.ts.map +1 -0
  95. package/dist/impl/linked-hash-map.js +442 -0
  96. package/dist/impl/linked-hash-map.js.map +1 -0
  97. package/dist/impl/linked-list.d.ts +109 -0
  98. package/dist/impl/linked-list.d.ts.map +1 -0
  99. package/dist/impl/linked-list.js +494 -0
  100. package/dist/impl/linked-list.js.map +1 -0
  101. package/dist/impl/priority-queue.d.ts +139 -0
  102. package/dist/impl/priority-queue.d.ts.map +1 -0
  103. package/dist/impl/priority-queue.js +343 -0
  104. package/dist/impl/priority-queue.js.map +1 -0
  105. package/dist/impl/tree-map.d.ts +153 -0
  106. package/dist/impl/tree-map.d.ts.map +1 -0
  107. package/dist/impl/tree-map.js +788 -0
  108. package/dist/impl/tree-map.js.map +1 -0
  109. package/dist/impl/tree-set.d.ts +112 -0
  110. package/dist/impl/tree-set.d.ts.map +1 -0
  111. package/dist/impl/tree-set.js +374 -0
  112. package/dist/impl/tree-set.js.map +1 -0
  113. package/dist/index.d.ts +106 -0
  114. package/dist/index.d.ts.map +1 -0
  115. package/dist/index.js +139 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/interfaces/collection.d.ts +119 -0
  118. package/dist/interfaces/collection.d.ts.map +1 -0
  119. package/dist/interfaces/collection.js +8 -0
  120. package/dist/interfaces/collection.js.map +1 -0
  121. package/dist/interfaces/list.d.ts +247 -0
  122. package/dist/interfaces/list.d.ts.map +1 -0
  123. package/dist/interfaces/list.js +8 -0
  124. package/dist/interfaces/list.js.map +1 -0
  125. package/dist/interfaces/map.d.ts +344 -0
  126. package/dist/interfaces/map.d.ts.map +1 -0
  127. package/dist/interfaces/map.js +8 -0
  128. package/dist/interfaces/map.js.map +1 -0
  129. package/dist/interfaces/queue.d.ts +304 -0
  130. package/dist/interfaces/queue.d.ts.map +1 -0
  131. package/dist/interfaces/queue.js +8 -0
  132. package/dist/interfaces/queue.js.map +1 -0
  133. package/dist/interfaces/set.d.ts +137 -0
  134. package/dist/interfaces/set.d.ts.map +1 -0
  135. package/dist/interfaces/set.js +8 -0
  136. package/dist/interfaces/set.js.map +1 -0
  137. package/dist/register.d.ts +8 -0
  138. package/dist/register.d.ts.map +1 -0
  139. package/dist/register.js +8 -0
  140. package/dist/register.js.map +1 -0
  141. package/dist/storage/index.d.ts +15 -0
  142. package/dist/storage/index.d.ts.map +1 -0
  143. package/dist/storage/index.js +14 -0
  144. package/dist/storage/index.js.map +1 -0
  145. package/dist/storage/interface.d.ts +141 -0
  146. package/dist/storage/interface.d.ts.map +1 -0
  147. package/dist/storage/interface.js +23 -0
  148. package/dist/storage/interface.js.map +1 -0
  149. package/dist/storage/memory.d.ts +68 -0
  150. package/dist/storage/memory.d.ts.map +1 -0
  151. package/dist/storage/memory.js +154 -0
  152. package/dist/storage/memory.js.map +1 -0
  153. package/dist/utils/defaults.d.ts +135 -0
  154. package/dist/utils/defaults.d.ts.map +1 -0
  155. package/dist/utils/defaults.js +322 -0
  156. package/dist/utils/defaults.js.map +1 -0
  157. package/dist/utils/factories.d.ts +144 -0
  158. package/dist/utils/factories.d.ts.map +1 -0
  159. package/dist/utils/factories.js +293 -0
  160. package/dist/utils/factories.js.map +1 -0
  161. package/dist/utils/helpers.d.ts +137 -0
  162. package/dist/utils/helpers.d.ts.map +1 -0
  163. package/dist/utils/helpers.js +221 -0
  164. package/dist/utils/helpers.js.map +1 -0
  165. package/package.json +68 -3
  166. package/src/async/async-queue.ts +417 -0
  167. package/src/async/channels.ts +374 -0
  168. package/src/behaviors/bounded.ts +477 -0
  169. package/src/behaviors/composite.ts +99 -0
  170. package/src/behaviors/evented.ts +464 -0
  171. package/src/behaviors/lru.ts +447 -0
  172. package/src/behaviors/readonly.ts +224 -0
  173. package/src/behaviors/safe.ts +372 -0
  174. package/src/behaviors/synchronized.ts +316 -0
  175. package/src/behaviors/ttl.ts +462 -0
  176. package/src/contracts/model.ts +44 -0
  177. package/src/core/effects.ts +323 -0
  178. package/src/core/events.ts +356 -0
  179. package/src/core/middleware.ts +415 -0
  180. package/src/core/policies.ts +316 -0
  181. package/src/core/traits.ts +164 -0
  182. package/src/fx/collectors.ts +500 -0
  183. package/src/fx/iter.ts +536 -0
  184. package/src/impl/array-deque.ts +463 -0
  185. package/src/impl/array-list.ts +490 -0
  186. package/src/impl/hash-map.ts +623 -0
  187. package/src/impl/hash-set.ts +342 -0
  188. package/src/impl/linked-hash-map.ts +572 -0
  189. package/src/impl/linked-list.ts +606 -0
  190. package/src/impl/priority-queue.ts +448 -0
  191. package/src/impl/tree-map.ts +912 -0
  192. package/src/impl/tree-set.ts +469 -0
  193. package/src/index.ts +277 -0
  194. package/src/interfaces/collection.ts +141 -0
  195. package/src/interfaces/list.ts +281 -0
  196. package/src/interfaces/map.ts +405 -0
  197. package/src/interfaces/queue.ts +353 -0
  198. package/src/interfaces/set.ts +158 -0
  199. package/src/register.ts +8 -0
  200. package/src/storage/index.ts +16 -0
  201. package/src/storage/interface.ts +179 -0
  202. package/src/storage/memory.ts +180 -0
  203. package/src/utils/defaults.ts +345 -0
  204. package/src/utils/factories.ts +357 -0
  205. package/src/utils/helpers.ts +248 -0
@@ -0,0 +1,477 @@
1
+ /**
2
+ * Bounded behavior - Enforces capacity limits on collections.
3
+ *
4
+ * Wraps collections to prevent them from exceeding a maximum size.
5
+ * Provides various overflow policies to handle full collections.
6
+ *
7
+ * @example
8
+ * const list = compose(
9
+ * boundedList({ capacity: 100, policy: 'drop-oldest' })
10
+ * )(arrayList<number>())
11
+ *
12
+ * // Add 101 elements - first element will be dropped
13
+ * for (let i = 0; i < 101; i++) {
14
+ * list.add(i)
15
+ * }
16
+ */
17
+
18
+ import type { Middleware } from "../core/middleware.js";
19
+ import type { List } from "../interfaces/list.js";
20
+ import type { Queue, Deque } from "../interfaces/queue.js";
21
+ import type { MapLike } from "../interfaces/map.js";
22
+ import type { Collection } from "../interfaces/collection.js";
23
+ import type {
24
+ OverflowPolicy,
25
+ OverflowHandler,
26
+ OverflowContext,
27
+ } from "../core/policies.js";
28
+
29
+ /**
30
+ * Options for bounded behavior.
31
+ */
32
+ export interface BoundedOptions<T> {
33
+ /**
34
+ * Maximum number of elements the collection can hold.
35
+ */
36
+ capacity: number;
37
+
38
+ /**
39
+ * Policy for handling overflow when collection is full.
40
+ * @default 'throw'
41
+ */
42
+ policy?: OverflowPolicy;
43
+
44
+ /**
45
+ * Optional callback invoked when overflow occurs.
46
+ * Called before the policy is applied.
47
+ */
48
+ onOverflow?: OverflowHandler<T>;
49
+ }
50
+
51
+ /**
52
+ * Marker interface for bounded collections.
53
+ */
54
+ export interface BoundedCollection {
55
+ readonly capacity: number;
56
+ readonly isFull: boolean;
57
+ readonly remainingCapacity: number;
58
+ }
59
+
60
+ /**
61
+ * Creates a bounded List middleware.
62
+ *
63
+ * Intercepts add operations to enforce capacity limits.
64
+ * Supports multiple overflow policies.
65
+ */
66
+ export function boundedList<T>(
67
+ options: BoundedOptions<T>
68
+ ): Middleware<List<T>, List<T> & BoundedCollection> {
69
+ const { capacity, policy = "throw", onOverflow } = options;
70
+
71
+ return (next: List<T>): List<T> & BoundedCollection => {
72
+ const handleOverflow = (element: T): void => {
73
+ // Call overflow handler if provided
74
+ if (onOverflow) {
75
+ const context: OverflowContext<T> = {
76
+ newElement: element,
77
+ currentSize: next.size,
78
+ capacity,
79
+ timestamp: Date.now(),
80
+ };
81
+ onOverflow(context);
82
+ }
83
+
84
+ // Apply policy
85
+ switch (policy) {
86
+ case "throw":
87
+ throw new Error(`List is full (capacity: ${capacity})`);
88
+
89
+ case "drop-oldest":
90
+ // Remove first element
91
+ if (!next.isEmpty) {
92
+ next.shift();
93
+ }
94
+ break;
95
+
96
+ case "drop-newest":
97
+ // Don't add the new element (do nothing)
98
+ return;
99
+
100
+ case "reject":
101
+ // Silently reject (return false for add operations)
102
+ return;
103
+
104
+ case "grow":
105
+ // Allow growth (do nothing, element will be added)
106
+ return;
107
+
108
+ case "block":
109
+ throw new Error("Block policy not supported for sync collections");
110
+
111
+ default:
112
+ throw new Error(`Unknown overflow policy: ${policy}`);
113
+ }
114
+ };
115
+
116
+ return new Proxy(next, {
117
+ get(target, prop, receiver) {
118
+ // Capacity property
119
+ if (prop === "capacity") {
120
+ return capacity;
121
+ }
122
+
123
+ // isFull property
124
+ if (prop === "isFull") {
125
+ return target.size >= capacity;
126
+ }
127
+
128
+ // remainingCapacity property
129
+ if (prop === "remainingCapacity") {
130
+ return Math.max(0, capacity - target.size);
131
+ }
132
+
133
+ const value = Reflect.get(target, prop, receiver);
134
+
135
+ // Intercept methods that add elements
136
+ if (typeof value === "function") {
137
+ switch (prop) {
138
+ case "add":
139
+ return function (element: T): boolean {
140
+ if (target.size >= capacity && policy !== "grow") {
141
+ handleOverflow(element);
142
+ if (policy === "reject" || policy === "drop-newest") {
143
+ return false;
144
+ }
145
+ }
146
+ return (value as Function).call(target, element);
147
+ };
148
+
149
+ case "push":
150
+ return function (element: T): void {
151
+ if (target.size >= capacity && policy !== "grow") {
152
+ handleOverflow(element);
153
+ if (policy === "reject" || policy === "drop-newest") {
154
+ return;
155
+ }
156
+ }
157
+ (value as Function).call(target, element);
158
+ };
159
+
160
+ case "unshift":
161
+ return function (element: T): void {
162
+ if (target.size >= capacity && policy !== "grow") {
163
+ handleOverflow(element);
164
+ if (policy === "reject" || policy === "drop-newest") {
165
+ return;
166
+ }
167
+ // For unshift with drop-oldest, we should drop from the end
168
+ if (policy === "drop-oldest" && !target.isEmpty) {
169
+ target.pop();
170
+ }
171
+ }
172
+ (value as Function).call(target, element);
173
+ };
174
+
175
+ case "insert":
176
+ return function (index: number, element: T): void {
177
+ if (target.size >= capacity && policy !== "grow") {
178
+ handleOverflow(element);
179
+ if (policy === "reject" || policy === "drop-newest") {
180
+ return;
181
+ }
182
+ }
183
+ (value as Function).call(target, index, element);
184
+ };
185
+
186
+ case "addAll":
187
+ case "insertAll":
188
+ return function (...args: any[]): boolean {
189
+ const elements = args[args.length - 1] as Iterable<T>;
190
+ const elementsArray = Array.from(elements);
191
+
192
+ // Check if adding all elements would exceed capacity
193
+ if (target.size + elementsArray.length > capacity && policy !== "grow") {
194
+ // Add elements one by one with overflow handling
195
+ let added = false;
196
+ for (const element of elementsArray) {
197
+ if (target.size >= capacity) {
198
+ handleOverflow(element);
199
+ if (policy === "reject" || policy === "drop-newest") {
200
+ break;
201
+ }
202
+ }
203
+ if (prop === "addAll") {
204
+ target.add(element);
205
+ } else {
206
+ target.insert(args[0] as number, element);
207
+ }
208
+ added = true;
209
+ }
210
+ return added;
211
+ }
212
+
213
+ return (value as Function).apply(target, args);
214
+ };
215
+ }
216
+ }
217
+
218
+ return value;
219
+ },
220
+ }) as List<T> & BoundedCollection;
221
+ };
222
+ }
223
+
224
+ /**
225
+ * Creates a bounded Queue middleware.
226
+ */
227
+ export function boundedQueue<T>(
228
+ options: BoundedOptions<T>
229
+ ): Middleware<Queue<T>, Queue<T> & BoundedCollection> {
230
+ const { capacity, policy = "throw", onOverflow } = options;
231
+
232
+ return (next: Queue<T>): Queue<T> & BoundedCollection => {
233
+ const handleOverflow = (element: T): void => {
234
+ if (onOverflow) {
235
+ const context: OverflowContext<T> = {
236
+ newElement: element,
237
+ currentSize: next.size,
238
+ capacity,
239
+ timestamp: Date.now(),
240
+ };
241
+ onOverflow(context);
242
+ }
243
+
244
+ switch (policy) {
245
+ case "throw":
246
+ throw new Error(`Queue is full (capacity: ${capacity})`);
247
+ case "drop-oldest":
248
+ if (!next.isEmpty) {
249
+ next.poll();
250
+ }
251
+ break;
252
+ case "drop-newest":
253
+ case "reject":
254
+ return;
255
+ case "grow":
256
+ return;
257
+ case "block":
258
+ throw new Error("Block policy not supported for sync collections");
259
+ default:
260
+ throw new Error(`Unknown overflow policy: ${policy}`);
261
+ }
262
+ };
263
+
264
+ return new Proxy(next, {
265
+ get(target, prop, receiver) {
266
+ if (prop === "capacity") return capacity;
267
+ if (prop === "isFull") return target.size >= capacity;
268
+ if (prop === "remainingCapacity") return Math.max(0, capacity - target.size);
269
+
270
+ const value = Reflect.get(target, prop, receiver);
271
+
272
+ if (typeof value === "function") {
273
+ switch (prop) {
274
+ case "offer":
275
+ case "enqueue":
276
+ case "add":
277
+ return function (element: T): boolean {
278
+ if (target.size >= capacity && policy !== "grow") {
279
+ handleOverflow(element);
280
+ if (policy === "reject" || policy === "drop-newest") {
281
+ return false;
282
+ }
283
+ }
284
+ return (value as Function).call(target, element);
285
+ };
286
+ }
287
+ }
288
+
289
+ return value;
290
+ },
291
+ }) as Queue<T> & BoundedCollection;
292
+ };
293
+ }
294
+
295
+ /**
296
+ * Creates a bounded Deque middleware.
297
+ */
298
+ export function boundedDeque<T>(
299
+ options: BoundedOptions<T>
300
+ ): Middleware<Deque<T>, Deque<T> & BoundedCollection> {
301
+ const { capacity, policy = "throw", onOverflow } = options;
302
+
303
+ return (next: Deque<T>): Deque<T> & BoundedCollection => {
304
+ const handleOverflow = (element: T): void => {
305
+ if (onOverflow) {
306
+ const context: OverflowContext<T> = {
307
+ newElement: element,
308
+ currentSize: next.size,
309
+ capacity,
310
+ timestamp: Date.now(),
311
+ };
312
+ onOverflow(context);
313
+ }
314
+
315
+ switch (policy) {
316
+ case "throw":
317
+ throw new Error(`Deque is full (capacity: ${capacity})`);
318
+ case "drop-oldest":
319
+ if (!next.isEmpty) {
320
+ next.removeFirst();
321
+ }
322
+ break;
323
+ case "drop-newest":
324
+ case "reject":
325
+ return;
326
+ case "grow":
327
+ return;
328
+ case "block":
329
+ throw new Error("Block policy not supported for sync collections");
330
+ default:
331
+ throw new Error(`Unknown overflow policy: ${policy}`);
332
+ }
333
+ };
334
+
335
+ return new Proxy(next, {
336
+ get(target, prop, receiver) {
337
+ if (prop === "capacity") return capacity;
338
+ if (prop === "isFull") return target.size >= capacity;
339
+ if (prop === "remainingCapacity") return Math.max(0, capacity - target.size);
340
+
341
+ const value = Reflect.get(target, prop, receiver);
342
+
343
+ if (typeof value === "function") {
344
+ switch (prop) {
345
+ case "addFirst":
346
+ case "addLast":
347
+ case "offerFirst":
348
+ case "offerLast":
349
+ case "push":
350
+ case "offer":
351
+ case "add":
352
+ return function (element: T): any {
353
+ if (target.size >= capacity && policy !== "grow") {
354
+ handleOverflow(element);
355
+ if (policy === "reject" || policy === "drop-newest") {
356
+ return prop.startsWith("offer") || prop === "add" ? false : undefined;
357
+ }
358
+ }
359
+ return (value as Function).call(target, element);
360
+ };
361
+ }
362
+ }
363
+
364
+ return value;
365
+ },
366
+ }) as Deque<T> & BoundedCollection;
367
+ };
368
+ }
369
+
370
+ /**
371
+ * Creates a bounded Map middleware.
372
+ */
373
+ export function boundedMap<K, V>(
374
+ options: Omit<BoundedOptions<V>, "onOverflow"> & {
375
+ onOverflow?: (context: Omit<OverflowContext<V>, "newElement"> & { key: K; value: V }) => void;
376
+ }
377
+ ): Middleware<MapLike<K, V>, MapLike<K, V> & BoundedCollection> {
378
+ const { capacity, policy = "throw", onOverflow } = options;
379
+
380
+ return (next: MapLike<K, V>): MapLike<K, V> & BoundedCollection => {
381
+ let insertionOrder: K[] = []; // Track insertion order for drop-oldest
382
+
383
+ const handleOverflow = (key: K, value: V): void => {
384
+ if (onOverflow) {
385
+ onOverflow({
386
+ key,
387
+ value,
388
+ currentSize: next.size,
389
+ capacity,
390
+ timestamp: Date.now(),
391
+ });
392
+ }
393
+
394
+ switch (policy) {
395
+ case "throw":
396
+ throw new Error(`Map is full (capacity: ${capacity})`);
397
+ case "drop-oldest":
398
+ // Remove oldest key
399
+ if (insertionOrder.length > 0) {
400
+ const oldestKey = insertionOrder.shift()!;
401
+ next.delete(oldestKey);
402
+ }
403
+ break;
404
+ case "drop-newest":
405
+ case "reject":
406
+ return;
407
+ case "grow":
408
+ return;
409
+ case "block":
410
+ throw new Error("Block policy not supported for sync collections");
411
+ default:
412
+ throw new Error(`Unknown overflow policy: ${policy}`);
413
+ }
414
+ };
415
+
416
+ return new Proxy(next, {
417
+ get(target, prop, receiver) {
418
+ if (prop === "capacity") return capacity;
419
+ if (prop === "isFull") return target.size >= capacity;
420
+ if (prop === "remainingCapacity") return Math.max(0, capacity - target.size);
421
+
422
+ const value = Reflect.get(target, prop, receiver);
423
+
424
+ if (typeof value === "function") {
425
+ switch (prop) {
426
+ case "set":
427
+ return function (key: K, val: V): V | undefined {
428
+ const hadKey = target.has(key);
429
+
430
+ if (!hadKey && target.size >= capacity && policy !== "grow") {
431
+ handleOverflow(key, val);
432
+ if (policy === "reject" || policy === "drop-newest") {
433
+ return undefined;
434
+ }
435
+ }
436
+
437
+ const result = (value as Function).call(target, key, val);
438
+
439
+ // Track insertion order
440
+ if (!hadKey) {
441
+ insertionOrder.push(key);
442
+ }
443
+
444
+ return result;
445
+ };
446
+
447
+ case "delete":
448
+ return function (key: K): V | undefined {
449
+ const result = (value as Function).call(target, key);
450
+ // Remove from insertion order tracking
451
+ insertionOrder = insertionOrder.filter((k) => !target.keyEq(k, key));
452
+ return result;
453
+ };
454
+
455
+ case "clear":
456
+ return function (): void {
457
+ (value as Function).call(target);
458
+ insertionOrder = [];
459
+ };
460
+ }
461
+ }
462
+
463
+ return value;
464
+ },
465
+ }) as MapLike<K, V> & BoundedCollection;
466
+ };
467
+ }
468
+
469
+ /**
470
+ * Generic bounded collection middleware.
471
+ * Works with any collection type.
472
+ */
473
+ export function bounded<C extends Collection<any>>(
474
+ options: BoundedOptions<any>
475
+ ): Middleware<C, C & BoundedCollection> {
476
+ return boundedList(options) as any;
477
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Composite map methods built on `has`, `get`, `set` and `delete`.
3
+ *
4
+ * A map behavior (lruMap, ttlMap) intercepts the primitive methods. It returns these
5
+ * implementations for the composite methods, bound to the behavior's own proxy, so a call such
6
+ * as `setIfAbsent` goes through the behavior's `set` and keeps its rules: capacity and recency
7
+ * for LRU, expiry for TTL. (BUGS-2026-07 L13: the composite methods went straight to the map
8
+ * below, so they bypassed the behavior.)
9
+ *
10
+ * The semantics follow HashMap. One difference: `replaceEntry` and `deleteEntry` compare
11
+ * values with Object.is, not with a map's custom value equality.
12
+ */
13
+
14
+ import type { Entry, MapLike } from "../interfaces/map.js";
15
+
16
+ type AnyFn = (...args: never[]) => unknown;
17
+
18
+ export function compositeMapMethod<K, V>(map: MapLike<K, V>, name: PropertyKey): AnyFn | undefined {
19
+ switch (name) {
20
+ case "setIfAbsent":
21
+ return ((key: K, value: V): V => {
22
+ if (map.has(key)) return map.get(key);
23
+ map.set(key, value);
24
+ return value;
25
+ }) as AnyFn;
26
+
27
+ case "replace":
28
+ return ((key: K, value: V): V | undefined => (map.has(key) ? map.set(key, value) : undefined)) as AnyFn;
29
+
30
+ case "replaceEntry":
31
+ return ((key: K, oldValue: V, newValue: V): boolean => {
32
+ if (!map.has(key) || !Object.is(map.get(key), oldValue)) return false;
33
+ map.set(key, newValue);
34
+ return true;
35
+ }) as AnyFn;
36
+
37
+ case "deleteEntry":
38
+ return ((key: K, value: V): boolean => {
39
+ if (!map.has(key) || !Object.is(map.get(key), value)) return false;
40
+ map.delete(key);
41
+ return true;
42
+ }) as AnyFn;
43
+
44
+ case "computeIfAbsent":
45
+ return ((key: K, mappingFunction: (key: K) => V): V => {
46
+ if (map.has(key)) return map.get(key);
47
+ const value = mappingFunction(key);
48
+ map.set(key, value);
49
+ return value;
50
+ }) as AnyFn;
51
+
52
+ case "computeIfPresent":
53
+ return ((key: K, remapping: (key: K, value: V) => V | undefined): V | undefined => {
54
+ if (!map.has(key)) return undefined;
55
+ const value = remapping(key, map.get(key));
56
+ if (value === undefined) {
57
+ map.delete(key);
58
+ return undefined;
59
+ }
60
+ map.set(key, value);
61
+ return value;
62
+ }) as AnyFn;
63
+
64
+ case "compute":
65
+ return ((key: K, remapping: (key: K, value: V | undefined) => V | undefined): V | undefined => {
66
+ const present = map.has(key);
67
+ const value = remapping(key, present ? map.get(key) : undefined);
68
+ if (value === undefined) {
69
+ if (present) map.delete(key);
70
+ return undefined;
71
+ }
72
+ map.set(key, value);
73
+ return value;
74
+ }) as AnyFn;
75
+
76
+ case "merge":
77
+ return ((key: K, value: V, remapping: (oldValue: V, newValue: V) => V | undefined): V | undefined => {
78
+ if (!map.has(key)) {
79
+ map.set(key, value);
80
+ return value;
81
+ }
82
+ const merged = remapping(map.get(key), value);
83
+ if (merged === undefined) {
84
+ map.delete(key);
85
+ return undefined;
86
+ }
87
+ map.set(key, merged);
88
+ return merged;
89
+ }) as AnyFn;
90
+
91
+ case "putAll":
92
+ return ((other: Iterable<Entry<K, V>>): void => {
93
+ for (const entry of other) {
94
+ map.set(entry.key, entry.value);
95
+ }
96
+ }) as AnyFn;
97
+ }
98
+ return undefined;
99
+ }