@llblab/uniqueue 1.2.0 → 1.3.0

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
27
  import { Uniqueue } from "@llblab/uniqueue";
28
28
 
29
- // Create a max-heap for leaderboard scores
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) => b.score - a.score, // Sort by score descending
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) {
@@ -71,7 +75,7 @@ Creates a new priority queue instance.
71
75
  | `data` | `T[]` | `[]` | Initial data array. |
72
76
  | `maxSize` | `number` | `Infinity` | Maximum number of items. If exceeded, lowest priority item is evicted. |
73
77
  | `compare` | `(a, b) => number` | `(a, b) => (a < b ? -1 : a > b ? 1 : 0)` | Comparison function for heap ordering. |
74
- | `extractKey` | `(item) => string` | `(item) => item` | Function to extract unique key string from item. |
78
+ | `extractKey` | `(item) => K` | `(item) => item` | Function to extract unique key any type from item. |
75
79
 
76
80
  ### Instance Methods
77
81
 
@@ -90,15 +94,15 @@ Removes and returns the highest priority item (the root of the heap).
90
94
 
91
95
  Returns the highest priority item without removing it.
92
96
 
93
- #### `remove(key: string): boolean`
97
+ #### `remove(key: K): boolean`
94
98
 
95
99
  Removes the item with the given key. Returns `true` if removed, `false` otherwise.
96
100
 
97
- #### `has(key: string): boolean`
101
+ #### `has(key: K): boolean`
98
102
 
99
103
  Checks if an item with the given key exists.
100
104
 
101
- #### `get(key: string): T | undefined`
105
+ #### `get(key: K): T | undefined`
102
106
 
103
107
  Returns the item with the given key without removing it.
104
108
 
package/dist/index.d.ts CHANGED
@@ -1,20 +1,20 @@
1
- export interface UniqueueOptions<T> {
1
+ export interface UniqueueOptions<T, K = string> {
2
2
  data?: T[];
3
3
  maxSize?: number;
4
4
  compare?: (a: T, b: T) => number;
5
- extractKey?: (item: T) => string;
5
+ extractKey?: (item: T) => K;
6
6
  }
7
- export declare class Uniqueue<T> {
7
+ export declare class Uniqueue<T, K = string> {
8
8
  #private;
9
9
  data: T[];
10
- indexes: Map<string, number>;
11
- constructor({ data, maxSize, compare, extractKey, }?: UniqueueOptions<T>);
10
+ indexes: Map<K, number>;
11
+ constructor({ data, maxSize, compare, extractKey, }?: UniqueueOptions<T, K>);
12
12
  push(item: T): T | undefined;
13
13
  pop(): T | undefined;
14
14
  peek(): T | undefined;
15
- remove(key: string): boolean;
16
- has(key: string): boolean;
17
- get(key: string): T | undefined;
15
+ remove(key: K): boolean;
16
+ has(key: K): boolean;
17
+ get(key: K): T | undefined;
18
18
  clear(): void;
19
19
  get size(): number;
20
20
  [Symbol.iterator](): IterableIterator<T>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC;CAClC;AAED,qBAAa,QAAQ,CAAC,CAAC;;IACrB,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBAMjB,EACV,IAAS,EACT,OAAkB,EAClB,OAAgD,EAChD,UAAgD,GACjD,GAAE,eAAe,CAAC,CAAC,CAAM;IAkE1B,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAsB5B,GAAG,IAAI,CAAC,GAAG,SAAS;IAgBpB,IAAI,IAAI,CAAC,GAAG,SAAS;IAIrB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IA0B5B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAK/B,KAAK,IAAI,IAAI;IAKb,IAAI,IAAI,IAAI,MAAM,CAEjB;IAEA,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC;CAG1C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM;IAC5C,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC;CAC7B;AAED,qBAAa,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM;;IACjC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,OAAO,EAAE,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;gBAMZ,EACV,IAAS,EACT,OAAkB,EAClB,OAAgD,EAChD,UAA2C,GAC5C,GAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAM;IAyE7B,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAsB5B,GAAG,IAAI,CAAC,GAAG,SAAS;IAgBpB,IAAI,IAAI,CAAC,GAAG,SAAS;IAIrB,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IA0BvB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAIpB,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAK1B,KAAK,IAAI,IAAI;IAKb,IAAI,IAAI,IAAI,MAAM,CAEjB;IAEA,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC;CAG1C"}
package/dist/index.js CHANGED
@@ -5,13 +5,20 @@ export class Uniqueue {
5
5
  #compare;
6
6
  #extractKey;
7
7
  constructor({ data = [], maxSize = Infinity, compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0), extractKey = (item) => item, } = {}) {
8
- this.data = data;
9
- this.indexes = new Map(data.map((item, index) => [extractKey(item), index]));
10
8
  this.#maxSize = maxSize;
11
9
  this.#compare = compare;
12
10
  this.#extractKey = extractKey;
13
- if (data.length > 0) {
14
- for (let i = (data.length >>> 1) - 1; i >= 0; i--) {
11
+ this.data = [];
12
+ this.indexes = new Map();
13
+ for (const item of data) {
14
+ const key = extractKey(item);
15
+ if (!this.indexes.has(key)) {
16
+ this.indexes.set(key, this.data.length);
17
+ this.data.push(item);
18
+ }
19
+ }
20
+ if (this.data.length > 0) {
21
+ for (let i = (this.data.length >>> 1) - 1; i >= 0; i--) {
15
22
  this.#siftDown(i);
16
23
  }
17
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@llblab/uniqueue",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
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",
@@ -28,7 +28,6 @@
28
28
  "deduplication",
29
29
  "leaderboard",
30
30
  "cache",
31
- "lru",
32
31
  "performance"
33
32
  ],
34
33
  "author": "LLB <shlavik@gmail.com>",