Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty shallowSlice return #65

Merged
merged 1 commit into from Feb 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion bl.js
Expand Up @@ -203,13 +203,16 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {

BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
start = start || 0
end = end || this.length
end = typeof end !== 'number' ? this.length : end

if (start < 0)
start += this.length
if (end < 0)
end += this.length

if (start === end) {
return new BufferList()
}
var startOffset = this._offset(start)
, endOffset = this._offset(end)
, buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Expand Up @@ -649,6 +649,20 @@ tape('shallow slice does not make a copy', function (t) {
t.equal(bl.toString(), 'hhhhhhhh')
})

tape('shallow slice with 0 length', function (t) {
t.plan(1)
var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
var bl = (new BufferList(buffers)).shallowSlice(0, 0)
t.equal(bl.length, 0)
})

tape('shallow slice with 0 length from middle', function (t) {
t.plan(1)
var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
var bl = (new BufferList(buffers)).shallowSlice(10, 10)
t.equal(bl.length, 0)
})

tape('duplicate', function (t) {
t.plan(2)

Expand Down