diff --git a/README.md b/README.md index c9c2c6b..4aff4e8 100644 --- a/README.md +++ b/README.md @@ -27,21 +27,29 @@ const buffer = new CircularBuffer({ limit: 5 }); buffer.forward('A'); buffer.forward('B'); buffer.forward('C'); +buffer.forward('D'); +buffer.forward('E'); // Get the current state of the buffer -console.log(buffer.current()); // Output: 'ABC' +buffer.current(); // Output: 'ABCDE' // Rewind the buffer buffer.rewind(); // Get the updated buffer state -console.log(buffer.current()); // Output: 'AB' +buffer.current(); // Output: 'ABCD' + +// Get the buffer value at the specified position using index +buffer.get(1); // Output: 'B' + +// Get the buffer value at the specified range using index and length +buffer.range(0,2); // Output: 'AB' // Reset the buffer buffer.reset(); // Get the buffer state after reset -console.log(buffer.current()); // Output: '' +buffer.current(); // Output: '' ``` ## API @@ -60,11 +68,22 @@ console.log(buffer.current()); // Output: '' - `current(): string` - Returns the current state of the circular buffer. + Returns the current state of the buffer. + +- `get(index: number): string` + + Returns the content at the specific position in the buffer. + - `index`: The index position in the buffer. + +- `range(start: number, length: number): string` + + Returns the content at the specific range in the buffer. + - `start`: The starting position in the buffer. + - `length`: The length of the range to fetch. - `reset(): void` - Resets the circular buffer by filling it with empty strings. + Resets the buffer by filling it with empty strings. - `forward(value: string): void` diff --git a/dist/CircularBuffer.d.ts b/dist/CircularBuffer.d.ts index 419db88..db106f6 100644 --- a/dist/CircularBuffer.d.ts +++ b/dist/CircularBuffer.d.ts @@ -13,7 +13,7 @@ export declare class CircularBuffer { /** * Create a new circular buffer. * - * @param limit - The buffer size limit. + * @param limit - the buffer size limit. * @throws if the provided limit is not a positive integer. */ constructor({ limit }: { @@ -36,6 +36,16 @@ export declare class CircularBuffer { */ get(index: number): string; + /** + * 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: number, len: number): string; + /** * Reset the buffer, fill it with empty strings. * diff --git a/package.json b/package.json index 223a790..f97bb13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "circle-buffer", - "version": "0.1.5", + "version": "0.2.1", "author": "Chris Rowles ", "license": "MIT", "description": "A simple circular array buffer", diff --git a/src/CircularBuffer.test.ts b/src/CircularBuffer.test.ts index 0db4d44..a78e35c 100644 --- a/src/CircularBuffer.test.ts +++ b/src/CircularBuffer.test.ts @@ -66,7 +66,15 @@ describe('buffer', () => { expect(buffer.get(2)).toBe('C'); }); - it('should throw an exception when creating a new buffer', () => { + it('should get the buffer at the specified range', () => { + buffer.forward('A'); + buffer.forward('B'); + buffer.forward('C'); + + expect(buffer.range(0, 2)).toBe('AB'); + }); + + it('should throw exception when creating a new buffer', () => { expect(() => new CircularBuffer({ limit: -1 })).toThrow( 'Limit must be a positive integer.', ); @@ -79,4 +87,18 @@ describe('buffer', () => { 'Limit must be a positive integer.', ); }); + + it('should throw when attempting to get the buffer at an invalid position', () => { + expect(() => { + buffer.get(99); + }).toThrow('Index must be a non-negative integer within buffer limits.'); + }); + + it('should throw when attempting to get the buffer at an invalid range.', () => { + expect(() => { + buffer.range(0, 99); + }).toThrow( + 'Start and len must be non-negative integers within buffer limits, and start must be less than len.', + ); + }); }); diff --git a/src/CircularBuffer.ts b/src/CircularBuffer.ts index 7d95e33..e37c8bb 100644 --- a/src/CircularBuffer.ts +++ b/src/CircularBuffer.ts @@ -13,7 +13,7 @@ export class CircularBuffer { /** * Create a new circular buffer. * - * @param limit - The buffer size limit. + * @param limit - the buffer size limit. * @throws if the provided limit is not a positive integer. */ constructor({ limit }: { limit: number }) { @@ -51,6 +51,30 @@ export class CircularBuffer { 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: number, len: number): string { + 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. *