Skip to content

Commit

Permalink
v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrychris committed Jul 19, 2024
1 parent 7a37a63 commit 1de660f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
8 changes: 0 additions & 8 deletions dist/CircularBuffer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ export declare class CircularBuffer {
* The buffer.
*/
private buffer;

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

/**
* Create a new circular buffer.
*
Expand All @@ -19,14 +17,12 @@ export declare class CircularBuffer {
constructor({ limit }: {
limit: number;
});

/**
* 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.
*
Expand All @@ -35,7 +31,6 @@ export declare class CircularBuffer {
* @throws if the index is out of bounds.
*/
get(index: number): string;

/**
* Get the content within a specific range in the buffer.
*
Expand All @@ -45,14 +40,12 @@ export declare class CircularBuffer {
* @throws if the start or len indices are out of bounds.
*/
range(start: number, len: number): string;

/**
* 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
Expand All @@ -62,7 +55,6 @@ export declare class CircularBuffer {
* @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
Expand Down
64 changes: 51 additions & 13 deletions dist/CircularBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ exports.CircularBuffer = void 0;
/** @public */
class CircularBuffer {
/**
* 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 }) {
if (!Number.isInteger(limit) || limit <= 0) {
Expand All @@ -17,32 +17,70 @@ class CircularBuffer {
this.limit = limit;
}
/**
* Get the current state of the buffer
* Get the current state of the buffer.
*
* @returns the contents of the buffer.
*/
get() {
current() {
return this.buffer.join('');
}
/**
* Reset the buffer, fill it with empty strings
* 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(index) {
if (!Number.isInteger(index) || index < 0 || index >= this.limit) {
throw new Error('Index must be a non-negative integer within buffer limits.');
}
return this.buffer[index];
}
/**
* Get the content within a specific range in the buffer.
*
* @param start - the starting position of the range.
* @param len - the length of the range (exclusive).
* @returns the content within the specified range.
* @throws if the start or len indices are out of bounds.
*/
range(start, len) {
if (!Number.isInteger(start) ||
!Number.isInteger(len) ||
start < 0 ||
len > this.limit ||
start >= len) {
throw new Error('Start and len must be non-negative integers within buffer limits, and start must be less than len.');
}
return this.buffer.slice(start, len).join('').toString();
}
/**
* Reset the buffer, fill it with empty strings.
*
* @returns no return value.
*/
reset() {
this.buffer = new Array(this.limit).fill('');
}
/**
* 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) {
this.buffer.shift();
this.buffer.push(value);
}
/**
* 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() {
if (this.buffer.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "circle-buffer",
"version": "0.2.1",
"version": "0.2.2",
"author": "Chris Rowles <christopher.rowles@outlook.com>",
"license": "MIT",
"description": "A simple circular array buffer",
Expand Down

0 comments on commit 1de660f

Please sign in to comment.