@llblab/uniqueue 1.2.0 → 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 +11 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,14 +21,19 @@ npm install @llblab/uniqueue
|
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
23
23
|
|
|
24
|
-
### Basic Example (
|
|
24
|
+
### Basic Example (Top-N Leaderboard)
|
|
25
25
|
|
|
26
26
|
```javascript
|
|
27
27
|
import { Uniqueue } from "@llblab/uniqueue";
|
|
28
28
|
|
|
29
|
-
//
|
|
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.
|
|
30
35
|
const leaderboard = new Uniqueue({
|
|
31
|
-
compare: (a, b) =>
|
|
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: "
|
|
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
|
-
//
|
|
54
|
+
// Bob (lowest score at root) is evicted
|
|
51
55
|
|
|
52
56
|
console.log(leaderboard.data);
|
|
53
|
-
// Contains
|
|
57
|
+
// Contains charlie (120), alice (150), dave (200)
|
|
54
58
|
|
|
55
59
|
// Iterate over all items
|
|
56
60
|
for (const player of leaderboard) {
|