@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.
@@ -11,7 +11,7 @@ import type {
11
11
  } from "./types.js";
12
12
 
13
13
  /**
14
- * Start a batch of items with concurrency limit
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
- * Call a listener. A listener that throws does not stop the run: the function gives a log
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
- * Start DAG with level-based parallelization
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
- * Start DAG sequentially (no parallelization)
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
- * Make a simple processor that wraps an async function
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>
@@ -8,7 +8,7 @@
8
8
  import type { DAGNode, DependencyDAG } from "./types.js";
9
9
 
10
10
  /**
11
- * Build a leveled DAG from nodes using Kahn's algorithm
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
- * Get the topological order (flat list, leaves first)
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>
@@ -149,7 +149,7 @@ export function groupingBy<T, K>(
149
149
  }
150
150
 
151
151
  /**
152
- * Groups and transforms with a downstream collector.
152
+ * This function groups the elements and transforms each group with a downstream collector.
153
153
  *
154
154
  * @example
155
155
  * const countByAge = collect(
@@ -44,7 +44,7 @@ export interface HashMapOptions<K, V> {
44
44
  initialCapacity?: number;
45
45
 
46
46
  /**
47
- * Load factor threshold for resizing (0-1).
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
- * Automatically resizes when load factor is exceeded.
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
  */
@@ -34,7 +34,7 @@ export interface HashSetOptions<T> {
34
34
  initialCapacity?: number;
35
35
 
36
36
  /**
37
- * Load factor threshold for resizing.
37
+ * This option is the load factor threshold for a resize.
38
38
  * @default 0.75
39
39
  */
40
40
  loadFactor?: number;
@@ -57,7 +57,7 @@ export interface LinkedHashMapOptions<K, V> {
57
57
  initialCapacity?: number;
58
58
 
59
59
  /**
60
- * Load factor before resizing.
60
+ * This option is the load factor before a resize.
61
61
  * @default 0.75
62
62
  */
63
63
  loadFactor?: number;
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
- * Extract element type from a collection.
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
- * Make a collection readonly by removing mutation methods.
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>
@@ -1,5 +1,5 @@
1
1
  /**
2
- * List interfaces that mirror Java's List interface.
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
- * Removes and gives the last element in this list.
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
- * Removes and gives the first element in this list.
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
- * Removes and gives the top element.
212
+ * This method removes the top element and gives it.
213
213
  *
214
214
  * @throws Error if stack is empty
215
215
  */
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Map interfaces that mirror Java's Map interface hierarchy.
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
- * Removes and gives the first (lowest) entry, or undefined if empty.
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
- * Removes and gives the last (highest) entry, or undefined if empty.
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
 
@@ -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
- * Retrieves, but does not remove, the head of this queue.
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
- * Retrieves and removes the head of this queue.
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
- * Retrieves and removes the head of this queue, or gives undefined if empty.
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
- * Alias for poll(). Removes and gives element from front of queue.
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
- * Retrieves and removes the first element.
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
- * Retrieves and removes the first element, or undefined if empty.
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
- * Retrieves and removes the last element.
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
- * Retrieves and removes the last element, or undefined if empty.
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
- * Retrieves and removes the head, waiting if necessary until an element is available.
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
- * Removes and gives next element.
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>;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Set interfaces that mirror Java's Set interface hierarchy.
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
- * Retrieves and removes the first (lowest) element, or gives undefined if empty.
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
- * Retrieves and removes the last (highest) element, or gives undefined if empty.
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
 
@@ -338,7 +338,7 @@ export const hashNumber = (num: number): number => {
338
338
  };
339
339
 
340
340
  /**
341
- * Combine multiple hash values.
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
- * Make a hash function that hashes by a specific property.
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)
@@ -11,7 +11,7 @@ import type { Eq } from "../core/traits.js";
11
11
  // ============================================================================
12
12
 
13
13
  /**
14
- * Checks if an index is within valid bounds [0, size).
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
- * Checks if an index is valid for insertion [0, size].
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
- * Checks if a range [start, end) is valid.
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
- * Checks if an array contains an element using custom equality.
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