Giter Club home page Giter Club logo

binarybuff's Introduction

🔢BinaryBuff🐃

Copyright© MIT ---- Battledash-2 (& BinaryBuff)

What is it?

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.

Usage:

Encoding an value:

Encoding an integer is simple.
Use the int.encode. To encode a string, we use string.encode
const bbuf = require('binarybuff');

const encodedInt = bbuf.int.encode(532);
const encodedStr = bbuf.string.encode("hello");

Decoding a value:

Decoding a value is the exact same as encoding, except we use decode instead of encode
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));

Encoding multiple values

When we use the encode function directly, we have to specify whether we would like to encode it in safe mode or not.

To encode multiple values, we can use BinaryBuff.encode

const bbuf = require('binarybuff');

console.log(bbuf.encode(true, 42, "hello"));

Creating a custom operator

If you're looking for custom functionality in BinaryBuff, you can add your own operator for the decoder

BinaryBuff.internal.handleOperator creates a new operator and returns the opcode of your operator

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')));

Encoding objects/arrays (❗ USAGE IS NOT RECOMMENDED ❗)

Encoding an object is only possible via BinaryBuff.encode

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!");

binarybuff's People

Contributors

battledash-2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.