diff --git a/src/index.ts b/src/index.ts index 6c017fe..aa9817b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,6 +115,10 @@ export class DynamicBuffer { throw new OutOfBoundsError('Out of bounds.') } + if (start === end) { + return new DynamicBuffer(EMPTY_BUFFER) + } + if (this.buffers.length === 0) { return new DynamicBuffer(EMPTY_BUFFER) } else if (this.buffers.length === 1) { @@ -152,6 +156,10 @@ export class DynamicBuffer { throw new OutOfBoundsError('Out of bounds.') } + if (start === end) { + return EMPTY_BUFFER + } + if (this.buffers.length === 0) { return EMPTY_BUFFER } else if (this.buffers.length === 1) { diff --git a/test/index.test.ts b/test/index.test.ts index ef2e239..aca6245 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -205,6 +205,16 @@ test('subarray', () => { deepStrictEqual(sub.buffer, Buffer.from([5])) } + { + const sub = multiBuffer.subarray(6) + deepStrictEqual(sub.buffer, EMPTY_BUFFER) + } + + { + const sub = multiBuffer.subarray(6, 6) + deepStrictEqual(sub.buffer, EMPTY_BUFFER) + } + // Test Out of bounds throws( () => { @@ -299,6 +309,16 @@ test('slice', () => { strictEqual(slice[0], 5) } + { + const slice = multiBuffer.slice(6) + deepStrictEqual(slice, EMPTY_BUFFER) + } + + { + const slice = multiBuffer.slice(6, 6) + deepStrictEqual(slice, EMPTY_BUFFER) + } + // Test Out of bounds throws( () => {