binkv
v1.0.2
Published
> Typescript library for encoding & decoding binary data
Readme
binkv
Typescript library for encoding & decoding binary data
Field
Fields store basic data, example:
import { Field } from 'binkv';
const firstname = Field.string("john");
const age = Field.int32(42);
const colors = Field.auto(['red', 'green', 'blue']); // .auto() automatically infers
Structure
Structures stores multiple fields, example:
import { Structure, Field } from 'binkv';
const struct = new Structure();
struct.writeField(Field.int32(5013));
struct.writeField(Field.string("hello"));
console.log(struct.data) // holds the binary dataTo decode a structure:
import { Structure, Field } from 'binkv';
// we're using the `struct` from the previous example.
const number = struct.readField().toInt32(); // 5013
const stringValue = struct.readField().toString(); // "hello"Dictionary
Dictionaries are basically key-value stores.
const dict = Dictionary.create({
firstname: 'john',
lastname: 'doe',
age: 42,
friend: {
firstname: 'david',
lastname: 'sharp',
age: 41
}
});
dict.getField('firstname')?.toString(); // "john"
dict.getField('lastname')?.toString(); // "doe"
dict.getField('age')?.toInt32(); // 42
const innerDict = dict.getDict('friend');
innerDict?.getField('firstname')?.toString(); // "david"
innerDict?.getField('lastname')?.toString(); // "sharp"
innerDict?.getField('age')?.toInt32(); // 41
const encoded = dict.getStructure(); // Convert dict to a structure
const decoded = Dictionary.fromStructure(encoded); // Decode structure back into a dict
decoded.getField('firstname')?.toString(); // "john"
decoded.getField('lastname')?.toString(); // "doe"
decoded.getField('age')?.toInt32(); // 42
const decodedInnerDict = decoded.getDict('friend');
decodedInnerDict?.getField('firstname')?.toString(); // "david"
decodedInnerDict?.getField('lastname')?.toString(); // "sharp"
decodedInnerDict?.getField('age')?.toInt32(); // 41