@mark1russell7/collections 0.9.3 → 0.9.5
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.
- package/README.md +4 -0
- package/build/dag/index.d.ts +10 -10
- package/build/dag/index.js +12 -12
- package/build/dag/index.js.map +1 -1
- package/build/index.d.ts +109 -77
- package/build/index.d.ts.map +1 -1
- package/build/index.js +104 -42
- package/build/index.js.map +1 -1
- package/package.json +4 -1
- package/src/async/async-queue.ts +24 -10
- package/src/async/channels.ts +3 -3
- package/src/async/semaphore.ts +88 -0
- package/src/behaviors/bounded.ts +1 -1
- package/src/behaviors/evented.ts +1 -1
- package/src/behaviors/readonly.ts +1 -1
- package/src/behaviors/safe.ts +16 -16
- package/src/behaviors/synchronized.ts +1 -1
- package/src/behaviors/ttl.ts +6 -6
- package/src/core/events.ts +14 -14
- package/src/core/middleware.ts +6 -6
- package/src/core/policies.ts +3 -3
- package/src/dag/builder.ts +5 -5
- package/src/dag/executor.ts +5 -5
- package/src/dag/traversal.ts +2 -2
- package/src/fx/collectors.ts +1 -1
- package/src/impl/hash-map.ts +2 -2
- package/src/impl/hash-set.ts +1 -1
- package/src/impl/linked-hash-map.ts +1 -1
- package/src/index.ts +2 -0
- package/src/interfaces/collection.ts +2 -2
- package/src/interfaces/list.ts +4 -4
- package/src/interfaces/map.ts +3 -3
- package/src/interfaces/queue.ts +10 -10
- package/src/interfaces/set.ts +3 -3
- package/src/utils/defaults.ts +2 -2
- package/src/utils/helpers.ts +4 -4
package/src/dag/executor.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type {
|
|
|
11
11
|
} from "./types.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* This function starts a batch of items with a limit of concurrency.
|
|
15
15
|
*/
|
|
16
16
|
async function executeWithConcurrency<T, R>(
|
|
17
17
|
items: T[],
|
|
@@ -45,7 +45,7 @@ async function executeWithConcurrency<T, R>(
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
48
|
+
* This function calls a listener. A listener that throws does not stop the run: the function gives a log
|
|
49
49
|
* line for its error (deep dive DATA-21).
|
|
50
50
|
*/
|
|
51
51
|
function notify<T>(name: string, listener: ((arg: T) => void) | undefined, arg: T): string | undefined {
|
|
@@ -59,7 +59,7 @@ function notify<T>(name: string, listener: ((arg: T) => void) | undefined, arg:
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* This function starts a DAG level by level, with the nodes of each level in parallel.
|
|
63
63
|
*
|
|
64
64
|
* - Levels are processed sequentially (level 0 first, then level 1, and so on)
|
|
65
65
|
* - Within each level, nodes are processed in parallel up to concurrency limit
|
|
@@ -167,7 +167,7 @@ export async function executeDAG<TNode extends DAGNode>(
|
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
/**
|
|
170
|
-
*
|
|
170
|
+
* This function starts a DAG one node at a time, with no nodes in parallel.
|
|
171
171
|
*/
|
|
172
172
|
export async function executeDAGSequential<TNode extends DAGNode>(
|
|
173
173
|
dag: DependencyDAG<TNode>,
|
|
@@ -178,7 +178,7 @@ export async function executeDAGSequential<TNode extends DAGNode>(
|
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
/**
|
|
181
|
-
*
|
|
181
|
+
* This function makes a simple processor that wraps an async function.
|
|
182
182
|
*/
|
|
183
183
|
export function createProcessor<TNode extends DAGNode>(
|
|
184
184
|
fn: (node: TNode) => Promise<void>
|
package/src/dag/traversal.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import type { DAGNode, DependencyDAG } from "./types.js";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* This function builds a leveled DAG from nodes, with Kahn's algorithm.
|
|
12
12
|
*
|
|
13
13
|
* This assigns levels to each node where:
|
|
14
14
|
* - Level 0: Nodes with no dependencies (leaves)
|
|
@@ -97,7 +97,7 @@ export function buildLeveledDAG<TNode extends DAGNode>(
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
100
|
+
* This function gives the topological order: a flat list, leaves first.
|
|
101
101
|
*/
|
|
102
102
|
export function getTopologicalOrder<TNode extends DAGNode>(
|
|
103
103
|
dag: DependencyDAG<TNode>
|
package/src/fx/collectors.ts
CHANGED
|
@@ -149,7 +149,7 @@ export function groupingBy<T, K>(
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
|
-
*
|
|
152
|
+
* This function groups the elements and transforms each group with a downstream collector.
|
|
153
153
|
*
|
|
154
154
|
* @example
|
|
155
155
|
* const countByAge = collect(
|
package/src/impl/hash-map.ts
CHANGED
|
@@ -44,7 +44,7 @@ export interface HashMapOptions<K, V> {
|
|
|
44
44
|
initialCapacity?: number;
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
|
-
*
|
|
47
|
+
* This option is the load factor threshold for a resize, from 0 to 1.
|
|
48
48
|
* When size > capacity * loadFactor, the map resizes.
|
|
49
49
|
* @default 0.75
|
|
50
50
|
*/
|
|
@@ -73,7 +73,7 @@ export interface HashMapOptions<K, V> {
|
|
|
73
73
|
* HashMap<K, V> - Hash table implementation with custom equality/hashing.
|
|
74
74
|
*
|
|
75
75
|
* This class uses separate chaining for collision resolution.
|
|
76
|
-
*
|
|
76
|
+
* The map resizes when its load factor is more than the threshold.
|
|
77
77
|
*
|
|
78
78
|
* Not thread-safe. Use synchronized() behavior for concurrent access.
|
|
79
79
|
*/
|
package/src/impl/hash-set.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -93,6 +93,7 @@ export * from "./behaviors/ttl.js";
|
|
|
93
93
|
export * from "./async/async-queue.js";
|
|
94
94
|
export * from "./async/channels.js";
|
|
95
95
|
export * from "./async/settled.js";
|
|
96
|
+
export * from "./async/semaphore.js";
|
|
96
97
|
|
|
97
98
|
// ============================================================================
|
|
98
99
|
// Functional Operations
|
|
@@ -135,6 +136,7 @@ export { ttlMap, ttlCache, TTLCache, ttlCollection } from "./behaviors/ttl.js";
|
|
|
135
136
|
// Async/Concurrent
|
|
136
137
|
export { asyncQueue, AsyncQueue } from "./async/async-queue.js";
|
|
137
138
|
export { asSettled, type Settled } from "./async/settled.js";
|
|
139
|
+
export { semaphore, type Semaphore } from "./async/semaphore.js";
|
|
138
140
|
export {
|
|
139
141
|
channel,
|
|
140
142
|
Channel,
|
|
@@ -129,12 +129,12 @@ export type IsReadonly<C> = C extends ReadonlyCollection<any>
|
|
|
129
129
|
: false;
|
|
130
130
|
|
|
131
131
|
/**
|
|
132
|
-
*
|
|
132
|
+
* This type gives the element type of a collection.
|
|
133
133
|
*/
|
|
134
134
|
export type ElementOf<C> = C extends ReadonlyCollection<infer T> ? T : never;
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
|
-
*
|
|
137
|
+
* This type makes a collection readonly: it removes the methods that change the collection.
|
|
138
138
|
*/
|
|
139
139
|
export type Readonly<C extends Collection<any>> = C extends Collection<infer T>
|
|
140
140
|
? ReadonlyCollection<T>
|
package/src/interfaces/list.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* This module has the list interfaces, which mirror the List interface of Java.
|
|
3
3
|
*
|
|
4
4
|
* Lists are ordered collections that can hold duplicate elements
|
|
5
5
|
* and give positional (indexed) access to elements.
|
|
@@ -153,7 +153,7 @@ export interface List<T> extends ReadonlyList<T>, Collection<T> {
|
|
|
153
153
|
push(element: T): void;
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
|
-
*
|
|
156
|
+
* This method removes the last element of this list and gives it.
|
|
157
157
|
*
|
|
158
158
|
* @returns The last element
|
|
159
159
|
* @throws Error if list is empty
|
|
@@ -161,7 +161,7 @@ export interface List<T> extends ReadonlyList<T>, Collection<T> {
|
|
|
161
161
|
pop(): T;
|
|
162
162
|
|
|
163
163
|
/**
|
|
164
|
-
*
|
|
164
|
+
* This method removes the first element of this list and gives it.
|
|
165
165
|
*
|
|
166
166
|
* @returns The first element
|
|
167
167
|
* @throws Error if list is empty
|
|
@@ -209,7 +209,7 @@ export interface Stack<T> extends Iterable<T> {
|
|
|
209
209
|
push(element: T): void;
|
|
210
210
|
|
|
211
211
|
/**
|
|
212
|
-
*
|
|
212
|
+
* This method removes the top element and gives it.
|
|
213
213
|
*
|
|
214
214
|
* @throws Error if stack is empty
|
|
215
215
|
*/
|
package/src/interfaces/map.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* This module has the map interfaces, which mirror the Map interface hierarchy of Java.
|
|
3
3
|
*
|
|
4
4
|
* A map holds key-value pairs and gives efficient lookup by key.
|
|
5
5
|
* Keys are unique within a map.
|
|
@@ -288,12 +288,12 @@ export interface NavigableMap<K, V> extends SortedMap<K, V> {
|
|
|
288
288
|
lastEntry(): Entry<K, V> | undefined;
|
|
289
289
|
|
|
290
290
|
/**
|
|
291
|
-
*
|
|
291
|
+
* This method removes the first (lowest) entry and gives it, or gives undefined when the map is empty.
|
|
292
292
|
*/
|
|
293
293
|
pollFirstEntry(): Entry<K, V> | undefined;
|
|
294
294
|
|
|
295
295
|
/**
|
|
296
|
-
*
|
|
296
|
+
* This method removes the last (highest) entry and gives it, or gives undefined when the map is empty.
|
|
297
297
|
*/
|
|
298
298
|
pollLastEntry(): Entry<K, V> | undefined;
|
|
299
299
|
|
package/src/interfaces/queue.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type { Compare } from "../core/traits.js";
|
|
|
15
15
|
*/
|
|
16
16
|
export interface ReadonlyQueue<T> extends ReadonlyCollection<T> {
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* This method gives the head of this queue, and does not remove it.
|
|
19
19
|
*
|
|
20
20
|
* @returns The head element
|
|
21
21
|
* @throws Error if queue is empty
|
|
@@ -47,7 +47,7 @@ export interface Queue<T> extends ReadonlyQueue<T>, Collection<T> {
|
|
|
47
47
|
offer(element: T): boolean;
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* This method removes the head of this queue and gives it.
|
|
51
51
|
*
|
|
52
52
|
* @returns The head element
|
|
53
53
|
* @throws Error if queue is empty
|
|
@@ -55,7 +55,7 @@ export interface Queue<T> extends ReadonlyQueue<T>, Collection<T> {
|
|
|
55
55
|
poll(): T;
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* This method removes the head of this queue and gives it, or gives undefined when the queue is empty.
|
|
59
59
|
*/
|
|
60
60
|
pollOrUndefined(): T | undefined;
|
|
61
61
|
|
|
@@ -66,7 +66,7 @@ export interface Queue<T> extends ReadonlyQueue<T>, Collection<T> {
|
|
|
66
66
|
enqueue(element: T): boolean;
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
|
-
*
|
|
69
|
+
* This method is an alias for poll(). It removes the element at the front of the queue and gives it.
|
|
70
70
|
* This method is here for consistency with standard queue terminology.
|
|
71
71
|
*/
|
|
72
72
|
dequeue(): T;
|
|
@@ -96,14 +96,14 @@ export interface Deque<T> extends Queue<T> {
|
|
|
96
96
|
offerFirst(element: T): boolean;
|
|
97
97
|
|
|
98
98
|
/**
|
|
99
|
-
*
|
|
99
|
+
* This method removes the first element and gives it.
|
|
100
100
|
*
|
|
101
101
|
* @throws Error if deque is empty
|
|
102
102
|
*/
|
|
103
103
|
removeFirst(): T;
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
|
-
*
|
|
106
|
+
* This method removes the first element and gives it, or gives undefined when the deque is empty.
|
|
107
107
|
*/
|
|
108
108
|
pollFirst(): T | undefined;
|
|
109
109
|
|
|
@@ -135,14 +135,14 @@ export interface Deque<T> extends Queue<T> {
|
|
|
135
135
|
offerLast(element: T): boolean;
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
|
-
*
|
|
138
|
+
* This method removes the last element and gives it.
|
|
139
139
|
*
|
|
140
140
|
* @throws Error if deque is empty
|
|
141
141
|
*/
|
|
142
142
|
removeLast(): T;
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
145
|
+
* This method removes the last element and gives it, or gives undefined when the deque is empty.
|
|
146
146
|
*/
|
|
147
147
|
pollLast(): T | undefined;
|
|
148
148
|
|
|
@@ -197,7 +197,7 @@ export interface BlockingQueue<T> extends Queue<T> {
|
|
|
197
197
|
put(element: T, timeoutMs?: number): Promise<void>;
|
|
198
198
|
|
|
199
199
|
/**
|
|
200
|
-
*
|
|
200
|
+
* This method removes the head and gives it. It waits until an element is available.
|
|
201
201
|
*
|
|
202
202
|
* @param timeoutMs Optional timeout in milliseconds
|
|
203
203
|
* @returns Promise that resolves to the head element
|
|
@@ -258,7 +258,7 @@ export interface AsyncQueue<T> extends AsyncIterable<T> {
|
|
|
258
258
|
put(element: T): Promise<void>;
|
|
259
259
|
|
|
260
260
|
/**
|
|
261
|
-
*
|
|
261
|
+
* This method removes the next element and gives it.
|
|
262
262
|
* Waits for an element if queue is empty.
|
|
263
263
|
*/
|
|
264
264
|
take(): Promise<T>;
|
package/src/interfaces/set.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* This module has the set interfaces, which mirror the Set interface hierarchy of Java.
|
|
3
3
|
*
|
|
4
4
|
* Sets are collections that contain no duplicate elements.
|
|
5
5
|
* They give efficient membership testing.
|
|
@@ -137,12 +137,12 @@ export interface NavigableSet<T> extends SortedSet<T> {
|
|
|
137
137
|
higher(element: T): T | undefined;
|
|
138
138
|
|
|
139
139
|
/**
|
|
140
|
-
*
|
|
140
|
+
* This method removes the first (lowest) element and gives it, or gives undefined when the set is empty.
|
|
141
141
|
*/
|
|
142
142
|
pollFirst(): T | undefined;
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
145
|
+
* This method removes the last (highest) element and gives it, or gives undefined when the set is empty.
|
|
146
146
|
*/
|
|
147
147
|
pollLast(): T | undefined;
|
|
148
148
|
|
package/src/utils/defaults.ts
CHANGED
|
@@ -338,7 +338,7 @@ export const hashNumber = (num: number): number => {
|
|
|
338
338
|
};
|
|
339
339
|
|
|
340
340
|
/**
|
|
341
|
-
*
|
|
341
|
+
* This function combines hash values into one hash.
|
|
342
342
|
* Useful for hashing objects with multiple fields.
|
|
343
343
|
*
|
|
344
344
|
* @example
|
|
@@ -355,7 +355,7 @@ export const combineHashes = (...hashes: number[]): number => {
|
|
|
355
355
|
};
|
|
356
356
|
|
|
357
357
|
/**
|
|
358
|
-
*
|
|
358
|
+
* This function makes a hash function that hashes by a property.
|
|
359
359
|
*
|
|
360
360
|
* @example
|
|
361
361
|
* const userHash = hashBy<User>((u) => u.id)
|
package/src/utils/helpers.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type { Eq } from "../core/traits.js";
|
|
|
11
11
|
// ============================================================================
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* This function gives true when an index is in the bounds [0, size).
|
|
15
15
|
* This function throws RangeError if out of bounds.
|
|
16
16
|
*
|
|
17
17
|
* @param index The index to check
|
|
@@ -26,7 +26,7 @@ export function checkBounds(index: number, size: number, collectionName: string
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* This function gives true when an index is valid for an insertion, in [0, size].
|
|
30
30
|
* In this function, index === size is correct for append operations.
|
|
31
31
|
*
|
|
32
32
|
* @param index The index to check
|
|
@@ -41,7 +41,7 @@ export function checkInsertBounds(index: number, size: number, collectionName: s
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
44
|
+
* This function gives true when a range [start, end) is valid.
|
|
45
45
|
*
|
|
46
46
|
* @param start Start index (inclusive)
|
|
47
47
|
* @param end End index (exclusive)
|
|
@@ -101,7 +101,7 @@ export function findLastIndex<T>(array: T[], element: T, eq: Eq<T>): number {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
/**
|
|
104
|
-
*
|
|
104
|
+
* This function gives true when an array has an element, with a custom equality.
|
|
105
105
|
*
|
|
106
106
|
* @param array The array to search
|
|
107
107
|
* @param element The element to find
|