0.5.0 • Published 9 months ago

bincoder v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

A binary encoder / decoder implementation in Typescript.

The repo is heavily inspired by bincode.

Test

cd bincoder
yarn
yarn run test

Install

yarn add bincoder

Example

Basic Type

import { Bool } from 'bincoder';

it("Bool", () => {
  let a = new Bool(true);

  let encoded = a.pack();
  console.log(encoded);

  let [decoded, size] = new Bool().unpack(encoded);
  console.log(decoded, " ", size);

  expect(decoded).toEqual(a);
});

Class

import { Base, Uint16, Uint32, Vec } from 'bincoder';

class SimpleArrayTest extends Base {
  a: Uint16;
  b: Vec<Uint32>;

  constructor(a: Uint16 = new Uint16(), b: Vec<Uint32> = new Vec(Uint32)) {
    super();
    this.a = a;
    this.b = b;
  }
}

it("class with simple array", () => {
  let t = new SimpleArrayTest(
    new Uint16(1),
    new Vec(Uint32, [new Uint32(2), new Uint32(3)])
  );

  let encoded = t.pack();
  console.log("encoded: ", encoded);

  let [decoded, size] = new SimpleArrayTest().unpack(encoded);
  let decodedObj = decoded as SimpleArrayTest;
  console.log("decodedObj.b.v: ", decodedObj.b.v, "size: ", size);

  expect(decodedObj.a).toEqual(t.a);
  expect(decodedObj.b.v).toEqual(t.b.v);
});

More examples: ./__test__/*.test.ts

0.5.0

9 months ago

0.4.0

9 months ago

0.3.0

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago