Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/charmander/array-queue
A queue backed by an array
https://github.com/charmander/array-queue
javascript
Last synced: about 1 month ago
JSON representation
A queue backed by an array
- Host: GitHub
- URL: https://github.com/charmander/array-queue
- Owner: charmander
- License: isc
- Created: 2018-01-09T15:52:58.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2022-07-05T22:57:01.000Z (over 2 years ago)
- Last Synced: 2024-10-13T20:13:29.675Z (3 months ago)
- Topics: javascript
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/array-queue
- Size: 64.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A queue backed by an array. Like [double-ended-queue][], but single-ended.
## Install
```sh
npm install array-queue
```## Example
```javascript
import Queue from 'array-queue';const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
console.log(queue.dequeue()); // 1
console.log(queue.dequeue()); // 2
console.log(queue.dequeue()); // Error: queue empty
```## API
- `new Queue()`
Creates an empty queue.
- `queue.count`
The number of items in the queue.
- `queue.enqueue(value)`
Adds a value to the end of the queue. Constant amortized time.
- `queue.dequeue()`
Removes a value from the beginning of the queue and returns it. Throws an error if the queue is empty. Constant time.
- `queue.tryDequeue()`
Like `dequeue()`, but returns `null` instead of throwing an error if the queue is empty.
[double-ended-queue]: https://www.npmjs.com/package/double-ended-queue