Socket
Book a DemoInstallSign in
Socket

binar

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binar

Encode / decode binary data using Buffer written in TypeScript

0.0.2
latest
Source
npmnpm
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

binar

Encode / decode binary data using Buffer written in TypeScript

npm version npm npm type definitions Build Status Dependency Status GitHub

Get Started

npm install -D binar

or

yarn add -D binar
import { DataType, decodeBinar, encodeBinar } from 'binar';

test('binar', () => {
  const data = [
    [DataType.Null, null],
    [DataType.Boolean, false],
    [DataType.Boolean, true],
    [DataType.Number, 0],
    [DataType.Number, 1],
    [DataType.Number, -1],
    [DataType.Number, Math.random()],
    [DataType.String, 'this is string 하하'],
    [DataType.Buffer, Buffer.from('another 하하')],
    [DataType.BigInt64BE, BigInt(-Math.floor(Math.random() * 2 ** 32))],
    [DataType.BigInt64LE, BigInt(-Math.floor(Math.random() * 2 ** 32))],
    [DataType.BigUInt64BE, BigInt(Math.floor(Math.random() * 2 ** 32))],
    [DataType.BigUInt64LE, BigInt(Math.floor(Math.random() * 2 ** 32))],
    [DataType.DoubleBE, -Math.random()],
    [DataType.DoubleLE, -Math.random()],
    [DataType.FloatBE, -Math.random()],
    [DataType.FloatLE, -Math.random()],
    [DataType.Int8, -Math.floor(Math.random() * 2 ** 4)],
    [DataType.Int16BE, -Math.floor(Math.random() * 2 ** 8)],
    [DataType.Int16LE, -Math.floor(Math.random() * 2 ** 8)],
    [DataType.Int32BE, -Math.floor(Math.random() * 2 ** 16)],
    [DataType.Int32LE, -Math.floor(Math.random() * 2 ** 16)],
    [DataType.UInt8, Math.floor(Math.random() * 2 ** 8)],
    [DataType.UInt16BE, Math.floor(Math.random() * 2 ** 16)],
    [DataType.UInt16LE, Math.floor(Math.random() * 2 ** 16)],
    [DataType.UInt32BE, Math.floor(Math.random() * 2 ** 32)],
    [DataType.UInt32LE, Math.floor(Math.random() * 2 ** 32)],
  ];

  const buf = encodeBinar(data as any);
  console.log(Buffer.from(data.map((x) => x[1]).join(':')).toString('base64'));
  console.log(buf.toString('base64'));
  const values = decodeBinar(buf);

  for (let i = 0, len = values.length; i < len; i += 1) {
    const val = values[i];
    if (typeof val === 'number') {
      expect(values[i]).toBeCloseTo(data[i][1] as any);
    } else {
      expect(values[i]).toEqual(data[i][1] as any);
    }
  }
});

License

MIT License

Keywords

binary

FAQs

Package last updated on 26 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.