Skip to content

Commit

Permalink
add positional get method
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrychris committed Jul 18, 2024
1 parent 1fe1f88 commit 77ce836
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 330 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

![CI Tests](https://github.com/sentrychris/circle-buffer/actions/workflows/tests.yml/badge.svg)

A simple circular array buffer for efficient management of string data in JavaScript/TypeScript.
A simple circular array buffer (also known as a ring buffer) for efficient management of string data in JavaScript/TypeScript.

This simple module is designed to manage a fixed-size buffer efficiently, with the ability to
overwrite old data with new data as it arrives. This is useful in scenarios where memory usage
needs to be constant and predictable, such as in real-time data processing, streaming applications,
or when handling continuous data streams like logs, sensor data, or network packets.

## Installation

Expand All @@ -24,19 +29,19 @@ buffer.forward('B');
buffer.forward('C');

// Get the current state of the buffer
console.log(buffer.get()); // Output: 'ABC'
console.log(buffer.current()); // Output: 'ABC'

// Rewind the buffer
buffer.rewind();

// Get the updated buffer state
console.log(buffer.get()); // Output: 'AB'
console.log(buffer.current()); // Output: 'AB'

// Reset the buffer
buffer.reset();

// Get the buffer state after reset
console.log(buffer.get()); // Output: ''
console.log(buffer.current()); // Output: ''
```

## API
Expand All @@ -53,9 +58,9 @@ console.log(buffer.get()); // Output: ''

#### Methods

- `get(): string`
- `current(): string`

Returns a string representation of the current state of the circular buffer.
Returns the current state of the circular buffer.

- `reset(): void`

Expand Down
52 changes: 37 additions & 15 deletions dist/CircularBuffer.d.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,64 @@
/** @public */
export declare class CircularBuffer {
/**
* The buffer
* The buffer.
*/
private buffer;

/**
* The buffer limit
* The buffer size limit.
*/
private limit;

/**
* Create a new circular buffer
* Create a new circular buffer.
*
* @param limit - the buffer size limit
* @throws if the provided limit is not a positive integer
* @param limit - The buffer size limit.
* @throws if the provided limit is not a positive integer.
*/
constructor({ limit }: {
limit: number;
});

/**
* Get the current state of the buffer
* Get the current state of the buffer.
*
* @returns the contents of the buffer.
*/
current(): string;

/**
* Get the content at the specific position in the buffer.
*
* @param index - the position in the buffer.
* @returns the content at the specified position.
* @throws if the index is out of bounds.
*/
get(): string;
get(index: number): string;

/**
* Reset the buffer, fill it with empty strings
* Reset the buffer, fill it with empty strings.
*
* @returns no return value.
*/
reset(): void;

/**
* Move the buffer forward by one position and add
* the provided value at the end of the buffer, if
* the buffer is full, the oldest value is removed
* Move the buffer forward by one position and add the provided
* value at the end of the buffer, if the buffer is full, the
* oldest value is removed.
*
* @param value - the value to add to the buffer
* @param value - the value to add to the buffer.
* @returns no return value.
*/
forward(value: string): void;

/**
* Move the buffer backward by one position, removing
* the last value, if the buffer becomes empty, an empty
* string is added at the beginning
* Move the buffer backward by one position, removing the last
* value, if the buffer becomes empty, an empty string is added
* at the beginning.
*
* @returns no return value.
*/
rewind(): void;
}
Loading

0 comments on commit 77ce836

Please sign in to comment.