/byteify

An npm module that serializes or deserializes your native types to an array of bytes.

Primary LanguageTypeScriptMIT LicenseMIT

Lint Build Test Coverage Status codecov Known Vulnerabilities Commitizen friendly License GitHub issues GitHub stars npm code style: prettier Types Test Coverage

byteify

An npm module that serializes or deserializes your native types to an array of bytes.

Install

To install byteify, run:

$ npm install byteify

Usage

Serialize

const byteify = require('byteify');

// The value will be: [1]
const serializedBool = byteify.serializeBool(true);

// The value will be: [10]
const serializedUint8 = byteify.serializeUint8(10); 
// The value will be: [1, 0]
const serializedUint16 = byteify.serializeUint16(256, { endianess: ByteifyEndianess.BIG_ENDIAN }); 
// The value will be: [0, 15, 66, 64]
const serializedUint32 = byteify.serializeUint32(1000000, { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: [0, 0, 0, 0, 5, 245, 225, 0]
const serializedUint64 = byteify.serializeUint64(100000000n, { endianess: ByteifyEndianess.BIG_ENDIAN }); 

// The value will be: [255]
const serializedInt8 = byteify.serializeInt8(-1);
// The value will be: [252, 24]
const serializedInt16 = byteify.serializeInt16(-1000, { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: [255, 254, 121, 96]
const serializedInt32 = byteify.serializeInt32(-100000, { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: [255, 255, 255, 255, 255, 255, 255, 255]
const serializedInt64 = byteify.serializeInt64(-1n, { endianess: ByteifyEndianess.BIG_ENDIAN });

// The value will be: [10, 215, 185, 65]  
const serializedFloat32 = byteify.serializeFloat32(23.23, { endianess: ByteifyEndianess.LITTLE_ENDIAN });
// The value will be: [123, 20, 174, 71, 225, 58, 55, 64]
const serializedFloat64 = byteify.serializeFloat64(23.23, { endianess: ByteifyEndianess.LITTLE_ENDIAN });

Deserialize

const byteify = require('byteify');
const ByteifyEndianess = byteify.ByteifyEndianess;

// The value will be: true
const deserializedBool = byteify.deserializeBool([1]);

// The value will be: 10
const deserializedUint8 = byteify.deserializeUint8([10]);
// The value will be: 1
const deserializedUint16 = byteify.deserializeUint16([1, 0], { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: 0
const deserializedUint32 = byteify.deserializeUint32([0, 15, 66, 64], { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: 100000000nn
const deserializedUint64 = byteify.deserializeUint64([5, 245, 225, 0, 5, 245, 225, 0], { endianess: ByteifyEndianess.BIG_ENDIAN });

// The value will be: -1
const deserializedInt8 = byteify.deserializeInt8([255]);
// The value will be: -1000
const deserializedInt16 = byteify.deserializeInt16([252, 24], { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: -100000
const deserializedInt32 = byteify.deserializeInt32([255, 254, 121, 96], { endianess: ByteifyEndianess.BIG_ENDIAN });
// The value will be: -1n
const deserializedInt64 = byteify.deserializeInt64([255, 255, 255, 255, 255, 255, 255, 255], { endianess: ByteifyEndianess.BIG_ENDIAN });

// The value will be: 23.23
const deserializedFloat32 = byteify.deserializeFloat32([10, 215, 185, 65], { endianess: ByteifyEndianess.LITTLE_ENDIAN });
// The value will be: 23.23
const deserializedFloat64 = byteify.deserializeFloat64([123, 20, 174, 71, 225, 58,  55, 64], { endianess: ByteifyEndianess.LITTLE_ENDIAN });

Endianess

By default, Little Endian is used. If you want to use Big Endian, you can pass it as an option

const byteify = require('byteify');
const ByteifyEndianess = byteify.ByteifyEndianess;

// The value will be: 1000000
const deserializedUint64 = byteify.deserializeUint64([64, 66, 15, 0, 0, 0, 0, 0]);
const deserializedUint64 = byteify.deserializeUint64([64, 66, 15, 0, 0, 0, 0, 0], { endianess: ByteifyEndianess.LITTLE_ENDIAN });
const deserializedUint64 = byteify.deserializeUint64([0, 0, 0, 0, 0, 15, 66, 64], { endianess: ByteifyEndianess.BIG_ENDIAN });

// The value will be [64, 66, 15, 0, 0, 0, 0, 0]
const serializedUint64 = byteify.serializeUint64(1000000, { endianess: ByteifyEndianess.LITTLE_ENDIAN });
// The value will be [0, 0, 0, 0, 0, 15, 66, 64]
const serializedUint64 = byteify.serializeUint64(1000000, { endianess: ByteifyEndianess.BIG_ENDIAN });

Limits

const { limits } = require('byteify');

/*
{
    bool: 1,
    uint8: 255,
    uint16: 65_535,
    uint32: 4_294_967_295,
    uint64: 18_446_744_073_709_551_616n,
    int8: 127,
    int16: 32_767,
    int32: 2_147_483_647,
    int64: 9_223_372_036_854_775_807n,
    float32: Number.MAX_VALUE,
    float64: Number.MAX_VALUE
}
*/
console.log(limits.MAX);

/*
{
    bool: 0,
    uint8: 0,
    uint16: 0,
    uint32: 0,
    uint64: 0n,
    int8: -128,
    int16: -32_768,
    int32: -2_147_483_648,
    int64: -9_223_372_036_854_775_808n,
    float32: -Number.MAX_VALUE,
    float64: -Number.MAX_VALUE
}
*/
console.log(limits.MIN);

/*
{
    bool: 1,
    uint8: 1,
    uint16: 2,
    uint32: 4,
    uint64: 8,
    int8: 1,
    int16: 2,
    int32: 4,
    int64: 8,
    float32: 4,
    float64: 8
}
*/
console.log(limits.N_OF_BYTES);

API

The documentation site is: byteify documentation

The documentation for development site is: byteify dev documentation

Development

To build the module make sure you have the dev dependencies installed.

The project is written in Typescript, bundled with esbuild, linted with ESLint and tested with Jest.

Lint

In order to lint the code:

$ npm run lint

In order to lint and fix the code:

$ npm run lint:fix

There are also the :source and :test suffix after lint in order to lint only the source code or the test code.

Transpile

To transpile both the source and the test code:

$ npm run transpile

The source and the test folders will be transpiled in the dist folder. Also the type declarations will be generated.

To transpile only the source code:

$ npm run transpile:source

The source folder will be transpiled in the dist folder. Also the type declarations will be generated.

Test

After having transpiled the code, run:

$ npm test

in order to run the tests with jest.

If a coverage report is to be generated, run:

$ npm run cover:generate

Bundle

$ npm run bundle

The source folder will be compiled in the bundled folder. It will contain the bundled index.js and index.d.ts files.

Notes

  • For simplicity, the limits for the floating point numbers are always Number.MAX_VALUE and -Number.MAX_VALUE. For float32 this means that the result is not guaranteed for too precise inputs.
  • For uint64 and int64, the BigInt type is used.