@hardlydifficult/queue 1.1.0 → 1.1.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 (2) hide show
  1. package/README.md +209 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,209 @@
1
+ # @hardlydifficult/queue
2
+
3
+ A high-performance priority queue implementation with O(1) enqueue and dequeue operations, supporting FIFO ordering within priority levels.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @hardlydifficult/queue
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { createPriorityQueue } from "@hardlydifficult/queue";
15
+
16
+ const queue = createPriorityQueue<string>();
17
+
18
+ queue.enqueue("low-priority", "low");
19
+ queue.enqueue("urgent", "high");
20
+ queue.enqueue("standard"); // defaults to 'medium'
21
+
22
+ // Dequeues in priority order, then by insertion time
23
+ console.log(queue.dequeue()?.data); // "urgent"
24
+ console.log(queue.dequeue()?.data); // "standard"
25
+ console.log(queue.dequeue()?.data); // "low-priority"
26
+ ```
27
+
28
+ ## Core API
29
+
30
+ ### createPriorityQueue
31
+
32
+ Creates a new priority queue instance.
33
+
34
+ ```typescript
35
+ import { createPriorityQueue } from "@hardlydifficult/queue";
36
+
37
+ const queue = createPriorityQueue<number>();
38
+ ```
39
+
40
+ ### PriorityQueue Interface
41
+
42
+ All queue operations are provided via the `PriorityQueue<T>` interface returned by `createPriorityQueue()`.
43
+
44
+ #### enqueue(data, priority?)
45
+
46
+ Adds an item to the queue.
47
+
48
+ | Parameter | Type | Default | Description |
49
+ |-----------|-----------|-----------|------------------------------|
50
+ | data | `T` | — | The item data to enqueue |
51
+ | priority | `Priority`| `"medium"`| Priority level: `"high"`, `"medium"`, or `"low"` |
52
+
53
+ Returns a `QueueItem<T>` with metadata including unique ID and enqueue timestamp.
54
+
55
+ ```typescript
56
+ const item = queue.enqueue("task", "high");
57
+ console.log(item.id); // "q_1"
58
+ console.log(item.priority); // "high"
59
+ ```
60
+
61
+ #### dequeue()
62
+
63
+ Removes and returns the highest-priority item (FIFO within same priority).
64
+
65
+ ```typescript
66
+ const item = queue.dequeue();
67
+ if (item) {
68
+ console.log(item.data); // "urgent"
69
+ }
70
+ ```
71
+
72
+ Returns `undefined` if the queue is empty.
73
+
74
+ #### peek()
75
+
76
+ Returns the next item to be dequeued without removing it.
77
+
78
+ ```typescript
79
+ const next = queue.peek();
80
+ if (next) {
81
+ console.log(next.data); // First item to be dequeued
82
+ }
83
+ ```
84
+
85
+ #### remove(id)
86
+
87
+ Removes an item by its unique ID.
88
+
89
+ ```typescript
90
+ const item = queue.enqueue("remove-me");
91
+ const removed = queue.remove(item.id); // true
92
+ const absent = queue.remove("unknown"); // false
93
+ ```
94
+
95
+ #### size
96
+
97
+ Readonly property returning the total number of items in the queue.
98
+
99
+ ```typescript
100
+ console.log(queue.size); // 5
101
+ ```
102
+
103
+ #### isEmpty
104
+
105
+ Readonly property indicating whether the queue is empty.
106
+
107
+ ```typescript
108
+ console.log(queue.isEmpty); // false
109
+ ```
110
+
111
+ #### onEnqueue(callback)
112
+
113
+ Registers a callback invoked whenever an item is enqueued.
114
+
115
+ Returns an unsubscribe function.
116
+
117
+ ```typescript
118
+ const unsubscribe = queue.onEnqueue((item) => {
119
+ console.log("Enqueued:", item.data);
120
+ });
121
+
122
+ queue.enqueue("test");
123
+ unsubscribe();
124
+ queue.enqueue("ignored"); // callback no longer invoked
125
+ ```
126
+
127
+ #### toArray()
128
+
129
+ Returns a snapshot of all items in dequeue order (by priority, then FIFO). Does not modify the queue.
130
+
131
+ ```typescript
132
+ const items = queue.toArray();
133
+ // Items sorted by priority: high → medium → low, then by insertion order
134
+ ```
135
+
136
+ #### clear()
137
+
138
+ Removes all items from the queue.
139
+
140
+ ```typescript
141
+ queue.enqueue("a");
142
+ queue.enqueue("b");
143
+ queue.clear();
144
+ console.log(queue.size); // 0
145
+ ```
146
+
147
+ ### Priority Updates
148
+
149
+ #### updatePriority(id, newPriority)
150
+
151
+ Changes the priority of an existing item.
152
+
153
+ ```typescript
154
+ const item = queue.enqueue("task", "low");
155
+ queue.updatePriority(item.id, "high"); // true
156
+ ```
157
+
158
+ Returns `true` if the item was found and updated, `false` otherwise.
159
+
160
+ #### moveBefore(itemId, beforeItemId)
161
+
162
+ Moves an item before another item within the same priority bucket.
163
+
164
+ ```typescript
165
+ const a = queue.enqueue("a", "medium");
166
+ const b = queue.enqueue("b", "medium");
167
+ const c = queue.enqueue("c", "medium");
168
+
169
+ queue.moveBefore(c.id, a.id); // true
170
+ console.log(queue.toArray().map(i => i.data)); // ["c", "a", "b"]
171
+ ```
172
+
173
+ Both items must exist and share the same priority. Returns `true` on success.
174
+
175
+ #### moveToEnd(itemId)
176
+
177
+ Moves an item to the end of its priority bucket.
178
+
179
+ ```typescript
180
+ const a = queue.enqueue("a", "medium");
181
+ const b = queue.enqueue("b", "medium");
182
+ queue.moveToEnd(a.id); // true
183
+ console.log(queue.toArray().map(i => i.data)); // ["b", "a"]
184
+ ```
185
+
186
+ Returns `true` if the item was found (even if already at end), `false` otherwise.
187
+
188
+ ## QueueItem Type
189
+
190
+ Represents an item in the queue with metadata:
191
+
192
+ ```typescript
193
+ interface QueueItem<T> {
194
+ data: T;
195
+ priority: Priority;
196
+ enqueuedAt: number; // epoch milliseconds
197
+ id: string; // unique identifier
198
+ }
199
+ ```
200
+
201
+ ## Priority Order
202
+
203
+ Priority levels are ordered from highest to lowest:
204
+
205
+ 1. `"high"`
206
+ 2. `"medium"` (default)
207
+ 3. `"low"`
208
+
209
+ Items with higher priority are dequeued before items with lower priority. Within the same priority, items are dequeued in FIFO order (first-in, first-out).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/queue",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [