buffer-extra
provide extra methods for buildin Buffer class like equals(), sliceNew()
npm install --save buffer-extra
npm test
To require this module:
var bfx = require('buffer-extra');
equals(bufferA, bufferB, posA, posB, count)
Compares two buffers if their bytes are equals to each other, same as buildin node.js Buffer.equals
with one usefull feature to specify positions (of both buffers) to start comparison at and the count of bytes to be compared.
Returns true
when buffers at specified positions and count equals, otherwise returns false
.
Params:
- bufferA and bufferB should be
Buffer
,string
or array of octets (as used when creatingnew Buffer(...)
instance), forstring
encodingutf8
is used, for other encoding explicitly create newBuffer
instance with required string encoding - posA and posB is number - position on which to start comparing bytes, negative value is not allowed and will act as value 0 (zero), when position exceed buffer(s) length method retuns
false
- count is number - count of bytes to be compared within buffers, negative value will be converted to absolute value
Example:
var bfx = require('buffer-extra');
var bufferA = new Buffer([1,2,3,4]);
var bufferB = new Buffer([3,4]);
// compare two buffers where bufferA position starts at 2, bufferB position at 0 (zero) with count 2
// method internally compares bytes [3,4] from bufferA and [3,4] from bufferB which is equal
var equals = bfx.equals(bufferA, bufferB, 2, 0, 2);
sliceNew(source, start, count)
Returns a new buffer which does not reference same memory as source buffer, it allways create new Buffer and fill data from source buffer copying count bytes starting on start position.
Licensed under ISC
Copyright (c) 2015 Peter Šulek