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

Add default read offsets for Node Buffer compatibility #99

Merged
merged 3 commits into from Feb 9, 2021
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
2 changes: 1 addition & 1 deletion BufferList.js
Expand Up @@ -373,7 +373,7 @@ BufferList.prototype._match = function (offset, search) {
return this.slice(offset, offset + byteLength)[m](0, byteLength)
}
} else {
BufferList.prototype[m] = function (offset) {
BufferList.prototype[m] = function (offset = 0) {
return this.slice(offset, offset + methods[m])[m](0)
}
}
Expand Down
22 changes: 20 additions & 2 deletions test/test.js
Expand Up @@ -265,6 +265,7 @@ tape('test readUInt8 / readInt8', function (t) {
const buf3 = Buffer.alloc(3)
const bl = new BufferList()

buf1[0] = 0x1
buf2[1] = 0x3
buf2[2] = 0x4
buf3[0] = 0x23
Expand All @@ -274,6 +275,7 @@ tape('test readUInt8 / readInt8', function (t) {
bl.append(buf2)
bl.append(buf3)

t.equal(bl.readUInt8(), 0x1)
t.equal(bl.readUInt8(2), 0x3)
t.equal(bl.readInt8(2), 0x3)
t.equal(bl.readUInt8(3), 0x4)
Expand All @@ -292,6 +294,7 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
const buf3 = Buffer.alloc(3)
const bl = new BufferList()

buf1[0] = 0x1
buf2[1] = 0x3
buf2[2] = 0x4
buf3[0] = 0x23
Expand All @@ -301,6 +304,8 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
bl.append(buf2)
bl.append(buf3)

t.equal(bl.readUInt16BE(), 0x0100)
t.equal(bl.readUInt16LE(), 0x0001)
t.equal(bl.readUInt16BE(2), 0x0304)
t.equal(bl.readUInt16LE(2), 0x0403)
t.equal(bl.readInt16BE(2), 0x0304)
Expand All @@ -323,6 +328,7 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
const buf3 = Buffer.alloc(3)
const bl = new BufferList()

buf1[0] = 0x1
buf2[1] = 0x3
buf2[2] = 0x4
buf3[0] = 0x23
Expand All @@ -332,6 +338,8 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
bl.append(buf2)
bl.append(buf3)

t.equal(bl.readUInt32BE(), 0x01000304)
t.equal(bl.readUInt32LE(), 0x04030001)
t.equal(bl.readUInt32BE(2), 0x03042342)
t.equal(bl.readUInt32LE(2), 0x42230403)
t.equal(bl.readInt32BE(2), 0x03042342)
Expand Down Expand Up @@ -391,6 +399,7 @@ tape('test readFloatLE / readFloatBE', function (t) {
const buf3 = Buffer.alloc(3)
const bl = new BufferList()

buf1[0] = 0x01
buf2[1] = 0x00
buf2[2] = 0x00
buf3[0] = 0x80
Expand All @@ -400,7 +409,11 @@ tape('test readFloatLE / readFloatBE', function (t) {
bl.append(buf2)
bl.append(buf3)

t.equal(bl.readFloatLE(2), 0x01)
const canonical = Buffer.concat([buf1, buf2, buf3])
t.equal(bl.readFloatLE(), canonical.readFloatLE())
t.equal(bl.readFloatBE(), canonical.readFloatBE())
t.equal(bl.readFloatLE(2), canonical.readFloatLE(2))
t.equal(bl.readFloatBE(2), canonical.readFloatBE(2))

t.end()
})
Expand All @@ -411,6 +424,7 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
const buf3 = Buffer.alloc(10)
const bl = new BufferList()

buf1[0] = 0x01
buf2[1] = 0x55
buf2[2] = 0x55
buf3[0] = 0x55
Expand All @@ -424,7 +438,11 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
bl.append(buf2)
bl.append(buf3)

t.equal(bl.readDoubleLE(2), 0.3333333333333333)
const canonical = Buffer.concat([buf1, buf2, buf3])
t.equal(bl.readDoubleBE(), canonical.readDoubleBE())
t.equal(bl.readDoubleLE(), canonical.readDoubleLE())
t.equal(bl.readDoubleBE(2), canonical.readDoubleBE(2))
t.equal(bl.readDoubleLE(2), canonical.readDoubleLE(2))

t.end()
})
Expand Down