BinaryBuff is a module to convert strings and floats or integers to buffers
then later on save it and read it from a binary file.
const bbuf = require('binarybuff');
const encodedInt = bbuf.int.encode(532);
const encodedStr = bbuf.string.encode("hello");
const bbuf = require('binarybuff');
const hello = bbuf.string.encode(true, "Hello, ");
const world = bbuf.string.encode(true, "world!");
console.log(bbuf.string.decode(hello) + bbuf.string.decode(world));
When we use the encode
function directly, we have to specify whether we would like to encode it in safe mode or not.
const bbuf = require('binarybuff');
console.log(bbuf.encode(true, 42, "hello"));
If you're looking for custom functionality in BinaryBuff
, you can add your own operator for the decoder
const bbuf = require('binarybuff');
const helloOperator = bbuf.internal.handleOperator((value) => {
console.log('My custom operator!');
return "Hello, " + bbuf.string.decode(value);
});
const encodeHelloOperator = (helloTo='me') => {
return bbuf.internal.encode(helloOperator, helloTo);
};
console.log(bbuf.decode(encodeHelloOperator('BinaryBuff')));
If you'd like to store any sort of Javascript functions,
you have to encode and decode it as an unsafe object.
const bbuf = require('binarybuff');
const encoded = bbuf.encode(false, [ 53, 89 ], { hello: "Hello, ", sayHelloTo: (usr)=>{console.log("Hello,", usr);} });
bbuf.decode(encoded, false)[1].sayHelloTo("me!");