Various data structures for JavaScript - implemented in TypeScript.
$ npm install containers.js
or
$ yarn add containers.js
-
SortedArray
- An array that automatically sorts items pushed into the container.
- Provides optional compare function to provide custom sorting.
-
UniqueQueue
- Ensures that items in queue are truley unique.
- Optional argument to keep the queue at a fixed length.
Basic usage:
const c = new SortedArray<number>();
c.push(3);
c.push(2);
c.push(5);
c.values(); // [2, 3, 5]
With custom compare function:
const c = new SortedArray<number>((a, b) => {
if (a === b) {
return 0;
}
return a < b ? 1 : -1;
});
c.insertValues([1, 2, 3, 4]);
c.values(); // [4, 3, 2, 1];
Basic usage:
const queue = new UniqueQueue<number>();
queue.push(1);
queue.push(2);
queue.push(2);
queue.push(3);
queue.values(); // [3, 2, 1]
With fixed queue-length:
const queue = new UniqueQueue<number>(5);
for (let i = 0; i < 10; i++) {
queue.push(i);
}
expect(queue.values()).toEqual([9, 8, 7, 6, 5]);
Setup:
yarn
Running tests:
$ yarn test