buffer-array
Advanced tools
Comparing version 1.0.1 to 1.1.0
34
index.js
@@ -31,9 +31,8 @@ 'use strict' | ||
* Write raw buffer to the end | ||
* @param buf {Buffer} | ||
* @param buf {Buffer|string} | ||
* @param encoding {string} | ||
* @returns {Boolean} | ||
*/ | ||
push(buf) { | ||
if (!Buffer.isBuffer(buf)) { | ||
throw new TypeError('Expected buffer') | ||
} | ||
push(buf, encoding) { | ||
buf = string2buffer(buf, encoding) | ||
@@ -71,9 +70,8 @@ if (out_of_bounds_in(this._buf, this._pos, buf.length)) { | ||
* Write raw buffer to the beginning | ||
* @param {Buffer} buf | ||
* @param {Buffer|string} buf | ||
* @param encoding {string} | ||
* @returns {Boolean} | ||
*/ | ||
unshift(buf) { | ||
if (!Buffer.isBuffer(buf)) { | ||
throw new TypeError('Expected buffer') | ||
} | ||
unshift(buf, encoding) { | ||
buf = string2buffer(buf, encoding) | ||
@@ -140,2 +138,5 @@ if (out_of_bounds_in(this._buf, this._pos, buf.length)) { | ||
// alias to shift() method | ||
BufferArray.prototype.read = BufferArray.prototype.shift | ||
/** | ||
@@ -282,2 +283,14 @@ * return true if out of bounds | ||
function string2buffer(str, enc) { | ||
if (!Buffer.isBuffer(str)) { | ||
if (typeof str === 'string') { | ||
return new Buffer(str, enc) | ||
} else { | ||
throw new TypeError('Expected buffer or string') | ||
} | ||
} | ||
return str | ||
} | ||
for(let m of Object.keys(methods)) { | ||
@@ -287,2 +300,3 @@ BufferArray.prototype['push'+m] = _push('write' + m, methods[m]) | ||
BufferArray.prototype['shift' + m] = _shift('read' + m, methods[m]) | ||
BufferArray.prototype['read' + m] = _shift('read' + m, methods[m]) | ||
BufferArray.prototype['unshift'+m] = _unshift('write' + m, methods[m]) | ||
@@ -289,0 +303,0 @@ } |
{ | ||
"name": "buffer-array", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "The Buffer with Array API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,19 +89,21 @@ # buffer-array | ||
#### `shift(size: Number): Buffer` | ||
* **`shift(size: Number): Buffer`** | ||
* **`read(size: Number): Buffer`** | ||
Read `size` bytes from the beginning and return buffer. Return `undefined` if out of bounds. | ||
* **`shiftDoubleBE(): Number`** | ||
* **`shiftDoubleLE(): Number`** | ||
* **`shiftFloatBE(): Number`** | ||
* **`shiftFloatLE(): Number`** | ||
* **`shiftInt32BE(): Number`** | ||
* **`shiftInt32LE(): Number`** | ||
* **`shiftInt16BE(): Number`** | ||
* **`shiftInt16LE(): Number`** | ||
* **`shiftUInt32BE(): Number`** | ||
* **`shiftUInt32LE(): Number`** | ||
* **`shiftUInt16BE(): Number`** | ||
* **`shiftUInt16LE(): Number`** | ||
* **`shiftInt8(): Number`** | ||
* **`shiftUInt8(): Number`** | ||
* **`shiftDoubleBE(): Number`**, **`readDoubleBE(): Number`** | ||
* **`shiftDoubleLE(): Number`**, **`readDoubleLE(): Number`** | ||
* **`shiftFloatBE(): Number`**, **`readFloatBE(): Number`** | ||
* **`shiftFloatLE(): Number`**, **`readFloatLE(): Number`** | ||
* **`shiftInt32BE(): Number`**, **`readInt32BE(): Number`** | ||
* **`shiftInt32LE(): Number`**, **`readInt32LE(): Number`** | ||
* **`shiftInt16BE(): Number`**, **`readInt16BE(): Number`** | ||
* **`shiftInt16LE(): Number`**, **`readInt16LE(): Number`** | ||
* **`shiftUInt32BE(): Number`**, **`readUInt32BE(): Number`** | ||
* **`shiftUInt32LE(): Number`**, **`readUInt32LE(): Number`** | ||
* **`shiftUInt16BE(): Number`**, **`readUInt16BE(): Number`** | ||
* **`shiftUInt16LE(): Number`**, **`readUInt16LE(): Number`** | ||
* **`shiftInt8(): Number`**, **`readInt8(): Number`** | ||
* **`shiftUInt8(): Number`**, **`readUInt8(): Number`** | ||
@@ -108,0 +110,0 @@ Read fixed-size number from the beginning. Return `undefined` if out of bounds. |
41
test.js
@@ -70,2 +70,6 @@ 'use strict' | ||
}, 'Expected buffer') | ||
ba.seek(2) | ||
t.ok(ba.push('09F3', 'hex')) | ||
t.ok(ba.toBuffer().equals(new Buffer([13, 17, 0x09, 0xF3]))) | ||
@@ -162,2 +166,32 @@ t.end() | ||
test('method `read`', function(t) { | ||
var ba = new BufferArray(new Buffer([13, 17, 23, 12])) | ||
var out = ba.read(3) | ||
t.equal(ba.seek(), 1) | ||
t.ok(out.equals(new Buffer([13, 17, 23])), 'buffers should be equals') | ||
t.ok(ba.toBuffer().equals(new Buffer([12, 0, 0, 0])), 'buffers should be equals') | ||
t.notOk(ba.read(2)) | ||
t.end() | ||
}) | ||
test('method `read*`', function (t) { | ||
var ba = new BufferArray(5) | ||
ba.pushInt16BE(10) | ||
ba.pushInt16BE(114) | ||
var out = ba.readInt16BE() | ||
t.equal(out, 10) | ||
t.equal(ba.seek(), 2) | ||
var out2 = ba.readInt32BE() | ||
t.equal(out2, void 0) | ||
t.equal(ba.seek(), 2) | ||
t.end() | ||
}) | ||
test('method `unshift`', function(t) { | ||
@@ -180,3 +214,10 @@ var ba = new BufferArray(5) | ||
ba.clear() | ||
t.ok(ba.unshift('A1B2C3', 'hex')) | ||
t.ok(ba.unshift(new Buffer([0x03, 0x12]))) | ||
t.equal(ba.seek(), 5) | ||
t.ok(ba.toBuffer().equals(new Buffer([0x03, 0x12, 0xA1, 0xB2, 0xC3]))) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
17758
415
133