@llblab/uniqueue 1.1.1 → 1.2.1

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 CHANGED
@@ -21,14 +21,19 @@ npm install @llblab/uniqueue
21
21
 
22
22
  ## Usage
23
23
 
24
- ### Basic Example (Max Heap)
24
+ ### Basic Example (Top-N Leaderboard)
25
25
 
26
26
  ```javascript
27
- import { UniQueue } from "@llblab/uniqueue";
28
-
29
- // Create a max-heap for leaderboard scores
30
- const leaderboard = new UniQueue({
31
- compare: (a, b) => b.score - a.score, // Sort by score descending
27
+ import { Uniqueue } from "@llblab/uniqueue";
28
+
29
+ // The `compare` function determines which item sits at the root:
30
+ // - (a, b) => a - b → Min-heap (smallest at root, evicted first)
31
+ // - (a, b) => b - a Max-heap (largest at root, evicted first)
32
+ //
33
+ // For "Top-N" scenario, use min-heap: when maxSize is exceeded,
34
+ // the root (smallest/worst) is evicted, keeping the best scores.
35
+ const leaderboard = new Uniqueue({
36
+ compare: (a, b) => a.score - b.score,
32
37
  extractKey: (item) => item.playerId, // Unique by playerId
33
38
  maxSize: 3, // Keep only top 3 scores
34
39
  });
@@ -39,18 +44,17 @@ leaderboard.push({ playerId: "bob", score: 80 });
39
44
  leaderboard.push({ playerId: "charlie", score: 120 });
40
45
 
41
46
  console.log(leaderboard.peek());
42
- // Output: { playerId: "charlie", score: 120 }
47
+ // Output: { playerId: "bob", score: 80 } (min-heap root)
43
48
 
44
49
  // Update existing item (alice improves score)
45
50
  leaderboard.push({ playerId: "alice", score: 150 });
46
- // Now alice is top, bob is pushed down
47
51
 
48
52
  // Add item that exceeds maxSize
49
53
  leaderboard.push({ playerId: "dave", score: 200 });
50
- // Dave enters, Bob (lowest score) is evicted
54
+ // Bob (lowest score at root) is evicted
51
55
 
52
56
  console.log(leaderboard.data);
53
- // Contains dave (200), alice (150), charlie (120)
57
+ // Contains charlie (120), alice (150), dave (200)
54
58
 
55
59
  // Iterate over all items
56
60
  for (const player of leaderboard) {
@@ -60,7 +64,7 @@ for (const player of leaderboard) {
60
64
 
61
65
  ## API
62
66
 
63
- ### `new UniQueue(options)`
67
+ ### `new Uniqueue(options)`
64
68
 
65
69
  Creates a new priority queue instance.
66
70
 
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- export interface UniQueueOptions<T> {
1
+ export interface UniqueueOptions<T> {
2
2
  data?: T[];
3
3
  maxSize?: number;
4
4
  compare?: (a: T, b: T) => number;
5
5
  extractKey?: (item: T) => string;
6
6
  }
7
- export declare class UniQueue<T> {
7
+ export declare class Uniqueue<T> {
8
8
  #private;
9
9
  data: T[];
10
10
  indexes: Map<string, number>;
11
- constructor({ data, maxSize, compare, extractKey, }?: UniQueueOptions<T>);
11
+ constructor({ data, maxSize, compare, extractKey, }?: UniqueueOptions<T>);
12
12
  push(item: T): T | undefined;
13
13
  pop(): T | undefined;
14
14
  peek(): T | undefined;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export class UniQueue {
1
+ export class Uniqueue {
2
2
  data;
3
3
  indexes;
4
4
  #maxSize;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/uniqueue",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "High-performance priority queue with unique key constraint (O(1) lookup, O(log n) update)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",