From 1de660f6eb9a3a0a37ee10fc8a0f9ee7c235c8d6 Mon Sep 17 00:00:00 2001 From: Chris Rowles Date: Fri, 19 Jul 2024 01:50:50 +0100 Subject: [PATCH] v0.2.2 --- dist/CircularBuffer.d.ts | 8 ----- dist/CircularBuffer.js | 64 ++++++++++++++++++++++++++++++++-------- package.json | 2 +- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/dist/CircularBuffer.d.ts b/dist/CircularBuffer.d.ts index db106f6..2eff214 100644 --- a/dist/CircularBuffer.d.ts +++ b/dist/CircularBuffer.d.ts @@ -4,12 +4,10 @@ export declare class CircularBuffer { * The buffer. */ private buffer; - /** * The buffer size limit. */ private limit; - /** * Create a new circular buffer. * @@ -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. * @@ -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. * @@ -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 @@ -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 diff --git a/dist/CircularBuffer.js b/dist/CircularBuffer.js index c6580a8..be74e86 100644 --- a/dist/CircularBuffer.js +++ b/dist/CircularBuffer.js @@ -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) { @@ -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) { diff --git a/package.json b/package.json index f97bb13..dd07cdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "circle-buffer", - "version": "0.2.1", + "version": "0.2.2", "author": "Chris Rowles ", "license": "MIT", "description": "A simple circular array buffer",