Skip to content

Commit

Permalink
added support for readUIntLE / readUIntBE / readIntLE / readIntBE
Browse files Browse the repository at this point in the history
  • Loading branch information
ctusch committed Apr 26, 2018
1 parent 27f7df8 commit 7473799
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
15 changes: 13 additions & 2 deletions bl.js
Expand Up @@ -266,12 +266,23 @@ BufferList.prototype.destroy = function destroy () {
, 'readUInt16LE' : 2
, 'readInt8' : 1
, 'readUInt8' : 1
, 'readIntBE' : null
, 'readIntLE' : null
, 'readUIntBE' : null
, 'readUIntLE' : null
}

for (var m in methods) {
(function (m) {
BufferList.prototype[m] = function (offset) {
return this.slice(offset, offset + methods[m])[m](0)
if (methods[m] === null) {
BufferList.prototype[m] = function (offset, byteLength) {
return this.slice(offset, offset + byteLength)[m](0, byteLength)
}
}
else {
BufferList.prototype[m] = function (offset) {
return this.slice(offset, offset + methods[m])[m](0)
}
}
}(m))
}
Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Expand Up @@ -305,6 +305,50 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
t.end()
})

tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) {
var buf1 = Buffer.alloc(1)
, buf2 = Buffer.alloc(3)
, buf3 = Buffer.alloc(3)
, bl = new BufferList()

buf2[0] = 0x2
buf2[1] = 0x3
buf2[2] = 0x4
buf3[0] = 0x23
buf3[1] = 0x42
buf3[2] = 0x61

bl.append(buf1)
bl.append(buf2)
bl.append(buf3)

t.equal(bl.readUIntBE(1, 1), 0x02)
t.equal(bl.readUIntBE(1, 2), 0x0203)
t.equal(bl.readUIntBE(1, 3), 0x020304)
t.equal(bl.readUIntBE(1, 4), 0x02030423)
t.equal(bl.readUIntBE(1, 5), 0x0203042342)
t.equal(bl.readUIntBE(1, 6), 0x020304234261)
t.equal(bl.readUIntLE(1, 1), 0x02)
t.equal(bl.readUIntLE(1, 2), 0x0302)
t.equal(bl.readUIntLE(1, 3), 0x040302)
t.equal(bl.readUIntLE(1, 4), 0x23040302)
t.equal(bl.readUIntLE(1, 5), 0x4223040302)
t.equal(bl.readUIntLE(1, 6), 0x614223040302)
t.equal(bl.readIntBE(1, 1), 0x02)
t.equal(bl.readIntBE(1, 2), 0x0203)
t.equal(bl.readIntBE(1, 3), 0x020304)
t.equal(bl.readIntBE(1, 4), 0x02030423)
t.equal(bl.readIntBE(1, 5), 0x0203042342)
t.equal(bl.readIntBE(1, 6), 0x020304234261)
t.equal(bl.readIntLE(1, 1), 0x02)
t.equal(bl.readIntLE(1, 2), 0x0302)
t.equal(bl.readIntLE(1, 3), 0x040302)
t.equal(bl.readIntLE(1, 4), 0x23040302)
t.equal(bl.readIntLE(1, 5), 0x4223040302)
t.equal(bl.readIntLE(1, 6), 0x614223040302)
t.end()
})

tape('test readFloatLE / readFloatBE', function (t) {
var buf1 = Buffer.alloc(1)
, buf2 = Buffer.alloc(3)
Expand Down

0 comments on commit 7473799

Please sign in to comment.