@juancwu/q-arr v0.0.5
Array Based Queue
Cross-platform array based queue implementation in javascript.
Get Started
npm install @juancwu/q-arr
const Queue = require("@juancwu/q-arr");
// Create a new Queue instance
const queue = new Queue();
Table of Content
Queue API
queue = new Queue([size,[opts]]);
Creates new Queue instance. Arguments size
and opts
are both optional.
Default size
is 50
.
Default opts
:
// opts
{
/*
* Setting this to true, will allow the Queue to dynamically
* increase slots in Queue.
*/
allowExtend: undefined, // treated as falsy
}
queue.push(item);
Push an arbitary value into a Queue
instance asynchronously. This method is O(1) when Queue
is not full. If allowExtend
is true, this method is O(n) when Queue
is full.
queue.pushSync(item);
The same as push()
but synchronous.
queue.pop();
Pop the first value from Queue
asynchronously. Resolves null if Queue
is empty. This method is O(1).
queue.popSync();
The same as pop()
but synchronous.
queue.length();
Returns the length of the Queue
. This is different than size()
because this method returns the number of items/values in the Queue
.
queue.size();
Returns the size of the Queue
. This method returns the available slots in the Queue
, but not the actual number of items/values.
queue.toString();
Returns a string representation of the Queue
in the following format:
`Queue ${length}/${size}`;
queue.forEach(cb);
Works the same way as Array.prototype.forEach()
.
queue.iter();
Generates an iterator that allow the usage of for...of
.
for (let item of queue.iter()) {
// do something with item...
}
for (let i of queue) { ... }
Iterate through the items in Queue
from first to last.
for await (let i of queue) { ... }
Iterate through the items in Queue
from first to last asynchronously.
Issues
Contribute
Help me improve the quality of code and algorithm for this package by openning an issue or making a PR.