Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "iobuffer", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Read and write binary data on ArrayBuffers", | ||
@@ -5,0 +5,0 @@ "main": "src/iobuffer.js", |
@@ -81,3 +81,3 @@ # iobuffer | ||
* readInt8 | ||
* readUint8 | ||
* readUint8 / readByte / readBytes(n) | ||
* readInt16 | ||
@@ -89,2 +89,3 @@ * readUint16 | ||
* readFloat64 | ||
* readChar / readChars(n) | ||
@@ -91,0 +92,0 @@ ## License |
@@ -24,2 +24,15 @@ 'use strict'; | ||
readByte() { | ||
return this._data.getUint8(this.offset++); | ||
} | ||
readBytes(n) { | ||
if (n === undefined) n = 1; | ||
var bytes = new Uint8Array(n); | ||
for (var i = 0; i < n; i++) { | ||
bytes[i] = this.readByte(); | ||
} | ||
return bytes; | ||
} | ||
readInt16() { | ||
@@ -60,4 +73,17 @@ var value = this._data.getInt16(this.offset, this.littleEndian); | ||
} | ||
readChar() { | ||
return String.fromCharCode(this.readInt8()); | ||
} | ||
readChars(n) { | ||
if (n === undefined) n = 1; | ||
var str = ''; | ||
for (var i = 0; i < n; i++) { | ||
str += this.readChar(); | ||
} | ||
return str; | ||
} | ||
} | ||
module.exports = InputBuffer; |
@@ -37,11 +37,9 @@ 'use strict'; | ||
it('readUint8', function () { | ||
it('readUint8 / readByte / readBytes', function () { | ||
buffer.readUint8().should.equal(0); | ||
buffer.readUint8().should.equal(255); | ||
buffer.readUint8().should.equal(0); | ||
buffer.readUint8().should.equal(255); | ||
buffer.readUint8().should.equal(255); | ||
buffer.readUint8().should.equal(0); | ||
buffer.readUint8().should.equal(255); | ||
buffer.readUint8().should.equal(0); | ||
buffer.readByte().should.equal(0); | ||
buffer.readByte().should.equal(255); | ||
Array.from(buffer.readBytes()).should.eql([255]); | ||
Array.from(buffer.readBytes(3)).should.eql([0, 255, 0]); | ||
}); | ||
@@ -81,2 +79,10 @@ | ||
}); | ||
it('readChar(s)', function () { | ||
var chars = 'hello'.split('').map(char => char.charCodeAt(0)); | ||
var buffer = new InputBuffer(new Uint8Array(chars)); | ||
buffer.readChar().should.equal('h'); | ||
buffer.readChars().should.equal('e'); | ||
buffer.readChars(3).should.equal('llo'); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12466
257
102
0