An npm module that serializes or deserializes your native types to an array of bytes.
To install byteify, run:
$ npm install byteifyconstbyteify=require('byteify');// The value will be: [1]constserializedBool=byteify.serializeBool(true);// The value will be: [10]constserializedUint8=byteify.serializeUint8(10);// The value will be: [1, 0]constserializedUint16=byteify.serializeUint16(256,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [0, 15, 66, 64]constserializedUint32=byteify.serializeUint32(1000000,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [0, 0, 0, 0, 5, 245, 225, 0]constserializedUint64=byteify.serializeUint64(100000000n,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [255]constserializedInt8=byteify.serializeInt8(-1);// The value will be: [252, 24]constserializedInt16=byteify.serializeInt16(-1000,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [255, 254, 121, 96]constserializedInt32=byteify.serializeInt32(-100000,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [255, 255, 255, 255, 255, 255, 255, 255]constserializedInt64=byteify.serializeInt64(-1n,{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: [10, 215, 185, 65] constserializedFloat32=byteify.serializeFloat32(23.23,{endianess: ByteifyEndianess.LITTLE_ENDIAN});// The value will be: [123, 20, 174, 71, 225, 58, 55, 64]constserializedFloat64=byteify.serializeFloat64(23.23,{endianess: ByteifyEndianess.LITTLE_ENDIAN});constbyteify=require('byteify');constByteifyEndianess=byteify.ByteifyEndianess;// The value will be: trueconstdeserializedBool=byteify.deserializeBool([1]);// The value will be: 10constdeserializedUint8=byteify.deserializeUint8([10]);// The value will be: 1constdeserializedUint16=byteify.deserializeUint16([1,0],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: 0constdeserializedUint32=byteify.deserializeUint32([0,15,66,64],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: 100000000nnconstdeserializedUint64=byteify.deserializeUint64([5,245,225,0,5,245,225,0],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: -1constdeserializedInt8=byteify.deserializeInt8([255]);// The value will be: -1000constdeserializedInt16=byteify.deserializeInt16([252,24],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: -100000constdeserializedInt32=byteify.deserializeInt32([255,254,121,96],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: -1nconstdeserializedInt64=byteify.deserializeInt64([255,255,255,255,255,255,255,255],{endianess: ByteifyEndianess.BIG_ENDIAN});// The value will be: 23.23constdeserializedFloat32=byteify.deserializeFloat32([10,215,185,65],{endianess: ByteifyEndianess.LITTLE_ENDIAN});// The value will be: 23.23constdeserializedFloat64=byteify.deserializeFloat64([123,20,174,71,225,58,55,64],{endianess: ByteifyEndianess.LITTLE_ENDIAN});By default, Little Endian is used. If you want to use Big Endian, you can pass it as an option
constbyteify=require('byteify');constByteifyEndianess=byteify.ByteifyEndianess;// The value will be: 1000000constdeserializedUint64=byteify.deserializeUint64([64,66,15,0,0,0,0,0]);constdeserializedUint64=byteify.deserializeUint64([64,66,15,0,0,0,0,0],{endianess: ByteifyEndianess.LITTLE_ENDIAN});constdeserializedUint64=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]constserializedUint64=byteify.serializeUint64(1000000,{endianess: ByteifyEndianess.LITTLE_ENDIAN});// The value will be [0, 0, 0, 0, 0, 15, 66, 64]constserializedUint64=byteify.serializeUint64(1000000,{endianess: ByteifyEndianess.BIG_ENDIAN});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);The documentation site is: byteify documentation
The documentation for development site is: byteify dev documentation
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.
In order to lint the code:
$ npm run lintIn order to lint and fix the code:
$ npm run lint:fixThere are also the :source and :test suffix after lint in order to lint only the source code or the test code.
To transpile both the source and the test code:
$ npm run transpileThe 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:sourceThe source folder will be transpiled in the dist folder. Also the type declarations will be generated.
After having transpiled the code, run:
$ npm testin order to run the tests with jest.
If a coverage report is to be generated, run:
$ npm run cover:generate$ npm run bundleThe source folder will be compiled in the bundled folder. It will contain the bundled index.js and index.d.ts files.
- For simplicity, the limits for the floating point numbers are always
Number.MAX_VALUEand-Number.MAX_VALUE. Forfloat32this means that the result is not guaranteed for too precise inputs. - For
uint64andint64, theBigInttype is used.