Buffer
This is buffer based on a circular singly linked list implementation that follows the itterator interface, being a drop in replacement for iterable structure when you need a list. When used unbound, it acts like a queue, when used bound, will start evicting once it hits the maxSize.
Usage Deno
import Buffer from "https://deno.land/x/buffer/mod.ts";
Usage Node
npm install --save @denox/buffer
import Buffer from "@denox/Buffer";
API
Initialization
The only argument is entries
and it is optional, allowing prepopulating the buffer.
const queue = new Buffer();
const queueWithData = new Buffer(Infinity, ["value1", "value2"]);
const buffer = new Buffer(100);
const bufferWithData = new Buffer(100, ["value1", "value2"]);
Push
Add a value to end of the buffer.
buffer.push("value");
Pop
Retrieve a value from begining of the buffer.
buffer.pop();
Peek
Retrieve a value from begining of the buffer, similar with pop
but without changing the buffer.
buffer.peek("key");
Clear
Clear everything from the buffer, leaving the queue empty.
buffer.clear();
Size
Get the current size of the buffer.
buffer.size;
MaxSize
Get the capacity of the buffer.
buffer.maxSize;
Keys, Values, Entries
Get the iterators for keys
, values
or entries
ordered based on the insetion.
Array.from(buffer.keys());
Array.from(buffer.values());
Array.from(buffer.entries());
ForEach
Iterate over the values in the insersion order.
buffer.forEach((value, key, buffer) => {
});
License
MIT