Releases: sentrychris/circle-buffer
v0.2.2
What's Changed
- Renamed
get()
tocurrent()
for fetching the current state of the buffer - Introduced new
get(index)
method for fetching at position - Introduced new
range(start, len)
method for fetching at range
- v0.2.1 by @sentrychris in #1
Full Changelog: v0.1.5...v0.2.2
v0.1.5
What's changed?
- Removed tests from distibution build by @sentrychris
- Changed visibility on
buffer
property by @sentrychris
Full Changelog: v0.1.4...v0.1.5
v0.1.4
Full Changelog: v0.1.3...v0.1.4
v0.1.3
Full Changelog: v0.1.2...v0.1.3
v0.1.2
Circle Buffer
A simple circular array buffer for efficient management of circular buffers in JavaScript/TypeScript.
Installation
npm install circle-buffer
Usage
import { CircularBuffer } from 'circle-buffer';
// Create a circular buffer with a limit of 5
const buffer = new CircularBuffer({ limit: 5 });
// Add values to the buffer
buffer.forward('A');
buffer.forward('B');
buffer.forward('C');
// Get the current state of the buffer
console.log(buffer.get()); // Output: 'ABC'
// Rewind the buffer
buffer.rewind();
// Get the updated buffer state
console.log(buffer.get()); // Output: 'AB'
// Reset the buffer
buffer.reset();
// Get the buffer state after reset
console.log(buffer.get()); // Output: ''
API
CircularBuffer
Constructor
-
new CircularBuffer(options: { limit: number }): CircularBuffer
Creates a new instance of the
CircularBuffer
class with the specified limit.options.limit
: The maximum number of elements the circular buffer can hold.
Methods
-
get(): string
Returns a string representation of the current state of the circular buffer.
-
reset(): void
Resets the circular buffer by filling it with empty strings.
-
forward(value: string): void
Moves the buffer forward by one position, adding the provided value at the end of the buffer. If the buffer is full, the oldest value is removed.
value
: The string value to be added to the buffer.
-
rewind(): void
Moves the buffer backward by one position, removing the last value. If the buffer becomes empty, an empty string is added at the beginning.
License
This project is licensed under the MIT License - see the LICENSE file for details.