queue-typescript
Simple Typescript Queue with generics type templating and support for iterator and iterable protocols.
This queue uses the linked-list-typescript as the underlying datastructure.
See Also:
Installation
npm:
npm install --save queue-typescript
yarn:
yarn add queue-typescript
Building from source
install dev dependencies. There are no production dependencies.
yarnnpm install
build using the options in tsconfig.json
yarn|npm run build
run all package tests
yarn|npm run test
see the test coverage report
yarn|npm run coverageyarn|npm run coverage:report
Usage
Importing:
;
API
Queue(...values: T[])
Queue()
Create an empty queue by omitting any arguments during instantiation.
Queue(...values: T[])
Create a new queue and initialize it with values. Values will be added from front to back. i.e. the first argument will be at the front of the queue and the last argument will be at the back of the queue.
Specify the type using the typescript templating to enable type-checking of all values going into and out of the queue.
;;
;;
Typescript will check if the values match the type given to the template when initializing the new queue.
;; // arguments are not all strings
Queue(...values: Foo[])
Create a new queue using custom types or classes. All values are retained as references and not copies so removed values can be compared using strict comparison.
;;; fooQueue.front.bar // => 1val // => foo1
Queue(...values: any[])
Specify any
to allow the queue to take values of any type.
queue.length // => 3queue.front // => 4
Queue#[Symbol.iterator]
The queue supports both iterator and iterable protocols allowing it to be used
with the for...of
and ...spread
operators and with deconstruction.
for...of
:
;; for of queue //4//5//6
...spread
:
;; manyArgs...queue;//4//5//6
deconstruction
:
;; ;//a => 4//b => 5//c => 6
Queue#front :T
Peek at the front of the queue. This will not remove the value from the queue.
;;queue.front // => 4
Queue#length :number
Query the length of the queue. An empty queue will return 0.
;;queue.length // => 4
Queue#enqueue(val: T): boolean
Enqueue an item at the back of the queue. The new item will replace the previous last item.
;;queue.length // => 4queue.enqueue8queue.length // => 5
Queue#dequeue(): T
Removes the item from the front of the queue and returns the item.
;;queue.length // => 4queue.length // => 3queue.front // => 5val // => 4
Queue#toArray(): T[]
This method simply returns [...this]
.
Converts the queue into an array and returns the array representation. This method does not mutate the queue in any way.
Objects are not copied, so all non-primitive items in the array are still referencing the queue items.
;;result // => [4, 5, 6, 7]