Registry / database / ts-priority-queue

ts-priority-queue

JSON →
library0.1.1jsnpmunverified

A priority queue data structure implemented in TypeScript, providing O(log n) insertion and removal. Version 0.1.1 is the current stable release. The library uses a binary heap strategy and is a port of the CoffeeScript js-priority-queue, with array and BHeap strategies removed. It offers a simple API: queue, dequeue, peek, clear, and length. Supports custom comparators and initial values. Ideal for TypeScript projects needing a minimal, dependency-free priority queue. However, it is a small, possibly unmaintained package with limited testing and documentation.

npm install ts-priority-queue
INSTALL
IMPORT
SIG · TS-PRIORITY-QUEUE
T
ts-priority-queue
databasejavascriptv0.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PriorityQueue
import { PriorityQueue } from 'ts-priority-queue';
const { PriorityQueue } = require('ts-priority-queue');
ESM import is preferred; package ships TypeScript types.
PriorityQueue (default)
import PriorityQueue from 'ts-priority-queue';
import { PriorityQueue } from 'ts-priority-queue';
Default export is the same as named export; both work. Named export is more explicit.
PriorityQueueOptions
import { PriorityQueueOptions } from 'ts-priority-queue';
Type import for options interface, available in TypeScript.

Demonstrates creating a min-heap and max-heap priority queue, adding elements, peeking, dequeuing, and clearing.

import { PriorityQueue } from 'ts-priority-queue'; // Create a min-heap priority queue (default comparator is a - b) const queue = new PriorityQueue<number>(); queue.queue(5); queue.queue(3); queue.queue(8); console.log(queue.length); // 3 console.log(queue.peek()); // 3 console.log(queue.dequeue()); // 3 console.log(queue.peek()); // 5 queue.clear(); console.log(queue.length); // 0 // Create a max-heap priority queue using custom comparator const maxQueue = new PriorityQueue<number>({ comparator: (a, b) => b - a }); maxQueue.queue(5); maxQueue.queue(3); maxQueue.queue(8); console.log(maxQueue.dequeue()); // 8
Debug
Known issues
gotchaIf no comparator is provided, the default is (a, b) => a - b (min-heap). This may be unexpected if you want a max-heap.
fix
Explicitly provide comparator: new PriorityQueue({ comparator: (a, b) => b - a }) for max-heap.
affects: >=0.0.0
gotchaThe package expects valid comparator function. Passing something like (a, b) => { return a < b } (boolean instead of number) will produce incorrect ordering.
fix
Ensure comparator returns a number (negative, zero, or positive), not a boolean.
affects: >=0.0.0
deprecatedThe library does not accept a default value for generic type; you must specify <number> or similar when using TypeScript.
fix
Use explicit type parameter: new PriorityQueue<number>()
affects: >=0.1.0
gotchaThere is no type export for the constructor options; you must inline the object literal.
fix
Use inline object: { comparator?: (a: T, b: T) => number; initialValues?: T[] }
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: queue.dequeue is not a function
Using default import incorrectly with require() in CommonJS environment.
fix
Use const { PriorityQueue } = require('ts-priority-queue');
TS2345: Type 'number' is not assignable to type 'string'
Using generic type without specifying it, or using wrong type.
fix
new PriorityQueue<number>() instead of using default any.
TypeError: Cannot read property 'length' of undefined
Accessing queue length before initialization. The queue must be instantiated with 'new'.
fix
const queue = new PriorityQueue(); queue.length works.
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
Meta
1
OpenAI (training)
1
Resources
ts-priority-queue — npm install ts-priority-queue · libregistry