@llblab/uniqueue 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/index.d.ts +89 -0
  2. package/package.json +13 -3
package/index.d.ts ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @template T
3
+ * @typedef {Object} UniQueueOptions
4
+ * @property {T[]} [data] - Initial data array
5
+ * @property {number} [maxSize] - Maximum queue size (default: Infinity)
6
+ * @property {(a: T, b: T) => number} [compare] - Comparison function for heap ordering
7
+ * @property {(item: T) => string} [extractKey] - Function to extract unique key from item
8
+ */
9
+ /**
10
+ * Priority queue with unique key constraint.
11
+ * Combines a min-heap with a key-to-index map for O(log n) push/pop with deduplication.
12
+ *
13
+ * @template T
14
+ */
15
+ export class UniQueue<T> {
16
+ [x: number]: () => IterableIterator<T>;
17
+ /**
18
+ * @param {UniQueueOptions<T>} [options]
19
+ */
20
+ constructor({ data, maxSize, compare, extractKey, }?: UniQueueOptions<T>);
21
+ /** @type {T[]} */
22
+ data: T[];
23
+ /** @type {Map<string, number>} */
24
+ indexes: Map<string, number>;
25
+ /**
26
+ * Add or update an item in the queue.
27
+ * - If key exists: Updates item and rebalances (unconditional update).
28
+ * - If key new: Adds item. If size > maxSize, evicts and returns min item.
29
+ * @param {T} item
30
+ * @returns {T | undefined} Evicted item if queue was full
31
+ */
32
+ push(item: T): T | undefined;
33
+ /**
34
+ * Remove and return the top (minimum) item.
35
+ * @returns {T | undefined}
36
+ */
37
+ pop(): T | undefined;
38
+ /**
39
+ * Return the top (minimum) item without removing it.
40
+ * @returns {T | undefined}
41
+ */
42
+ peek(): T | undefined;
43
+ /**
44
+ * Remove an item by key.
45
+ * @param {string} key
46
+ * @returns {boolean} true if item was removed
47
+ */
48
+ remove(key: string): boolean;
49
+ /**
50
+ * Check if an item exists.
51
+ * @param {string} key
52
+ * @returns {boolean}
53
+ */
54
+ has(key: string): boolean;
55
+ /**
56
+ * Get an item by key.
57
+ * @param {string} key
58
+ * @returns {T | undefined}
59
+ */
60
+ get(key: string): T | undefined;
61
+ /**
62
+ * Remove all items.
63
+ */
64
+ clear(): void;
65
+ /**
66
+ * Get item count.
67
+ * @returns {number}
68
+ */
69
+ get size(): number;
70
+ #private;
71
+ }
72
+ export type UniQueueOptions<T> = {
73
+ /**
74
+ * - Initial data array
75
+ */
76
+ data?: T[];
77
+ /**
78
+ * - Maximum queue size (default: Infinity)
79
+ */
80
+ maxSize?: number;
81
+ /**
82
+ * - Comparison function for heap ordering
83
+ */
84
+ compare?: (a: T, b: T) => number;
85
+ /**
86
+ * - Function to extract unique key from item
87
+ */
88
+ extractKey?: (item: T) => string;
89
+ };
package/package.json CHANGED
@@ -1,18 +1,25 @@
1
1
  {
2
2
  "name": "@llblab/uniqueue",
3
- "version": "1.0.0",
3
+ "version": "1.0.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": "index.js",
7
+ "types": "index.d.ts",
7
8
  "exports": {
8
- ".": "./index.js"
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ }
9
13
  },
10
14
  "files": [
11
15
  "index.js",
16
+ "index.d.ts",
12
17
  "README.md"
13
18
  ],
14
19
  "repository": "github:llblab/uniqueue",
15
20
  "scripts": {
21
+ "build:types": "tsc index.js --declaration --emitDeclarationOnly --allowJs --outDir .",
22
+ "prepack": "npm run build:types",
16
23
  "test": "node --test test.js"
17
24
  },
18
25
  "keywords": [
@@ -27,5 +34,8 @@
27
34
  ],
28
35
  "author": "LLB <shlavik@gmail.com>",
29
36
  "license": "MIT",
30
- "sideEffects": false
37
+ "sideEffects": false,
38
+ "devDependencies": {
39
+ "typescript": "^5.9.3"
40
+ }
31
41
  }