Skip to content

Commit

Permalink
Merge pull request #65 from reconbot/reconbot/empty-slice
Browse files Browse the repository at this point in the history
fix: empty shallowSlice return
  • Loading branch information
mcollina committed Feb 3, 2019
2 parents 055a3ff + 9b80b00 commit 635b6ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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

0 comments on commit 635b6ce

Please sign in to comment.