Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrychris committed Jul 18, 2024
1 parent 77ce836 commit 887dd98
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`

Expand Down
12 changes: 11 additions & 1 deletion dist/CircularBuffer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }: {
Expand All @@ -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.
*
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.1.5",
"version": "0.2.1",
"author": "Chris Rowles <christopher.rowles@outlook.com>",
"license": "MIT",
"description": "A simple circular array buffer",
Expand Down
24 changes: 23 additions & 1 deletion src/CircularBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
Expand All @@ -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.',
);
});
});
26 changes: 25 additions & 1 deletion src/CircularBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 887dd98

Please sign in to comment.