Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
() => {
Expand Down Expand Up @@ -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(
() => {
Expand Down