A "magical" ring buffer implemention with clever define properties allowing it to be used as a normal array.
A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.
There are numerous existing JS ringbuffer implementations but they require a) the user to interact with custom APIs, b) the re-implementation of desired array APIs, c) the user to frequently convert the buffer to interact with third party libraries.
I wrote this library because I find
RingBuffers a lot of fun to implement (and its usually the first thing I do when I learn a lanaguage).
The goal of this library is to make the buffer appear to any third party code as an array. In fact, this implementation simply inherits most of the array API directly (including
filter, etc.). The implementation has also been tested to work with 3rd party libraries which consume arrays (
The library implements the mutator functionality of the array API, stubs several properties and inherits the remaining functionality from the array prototype. The following methods are defined or can be inheritted:
buffer[<index>]: get a value in the buffer, where index corresponds to the index after rotation
buffer[<index>]=: set a value in the buffer at the provided (rotated) index
length: the number of items currently in the buffer
capacity: the maximum number of items the buffer can contain
head: the current starting index of the buffer
tail: the current ending index of the buffer
data: the managed array being proxied by the buffer
push(...items): append to items to the buffers end. It may overwrite values at the buffers head.
unshift(...items): place items at the buffer start. It may overwrite values on the buffers tail.
pop(): remove last item in the buffer, and return item
shift(): remove first item in the buffer, and return item
sort(fn): resort the buffer with a comparator.
empty(): clear items from buffer
reverse(): reverse order of buffer
reduce(fn, initial, [context]):
reduceRight(fn, initial, [context]):
[Symbol.iterator](): Allows a buffer to be used in an iterator (e.g.
var maxBufferSize = 5;var b = maxBufferSizebb2 // => 3b4 // => undefinedblength // => 3bbbb // => [4, 5, 6, 7, 8]b2 // => 6b // => 8blength // => 4