A simple circular buffer implementation in Typescript. More information about a circular buffer can be found here.
npm install simple-circular-buffer
import { CircularBuffer } from 'simple-circular-buffer';
const buffer = new CircularBuffer<number>(3);
buffer.push(1);
const item = buffer.get(); // 1
buffer.push(2);
Creates a new circular buffer with the given size. Can optionally allow overwriting the oldest item when the buffer is full.
Pushes an item into the buffer. Returns true
if the item was successfully pushed, false
if the buffer is full and allowOverwrite
is false
.
Returns the oldest item in the buffer, or null
if the buffer is empty.
Clears the buffer.
Returns true
if the buffer is empty, false
otherwise.
Returns true
if the buffer is full, false
otherwise.
Returns the maximum number of items the buffer can hold.
Returns the current number of items in the buffer.
MIT