@stassi/binary-transcoder
TypeScript icon, indicating that this package has built-in type declarations

0.11.2 • Public • Published

@stassi/binary-transcoder

version license types engines code size minified size minzipped size package health Continuous integration Security

Convert binary data between any two formats and encodings listed here.

ArrayLike

Further information: Uint8Array (MDN)

  • number[]
  • Uint8Array

Buffer

Further information: node:buffer (Node.js)

  • Buffer (browser-compatible, Node.js not required)

number

Further information: Number (MDN)

  • number

string

Further information: Binary number (Wikipedia) | JSON (MDN) | node:buffer character encodings (Node.js) | Unicode (Wikipedia)

  • 'base64'
  • 'base64url'
  • 'binary' (binary number string, not the legacy Node.js alias of the same name for 'latin1' encoding)
  • 'hex'
  • 'json'
  • 'latin1'
  • 'utf8' (encode output only)
  • 'utf16le' (encode output only)

Demo

Instant demonstration: @stassi/binary-transcoder (RunKit + npm)

Installation

Node.js

npm i @stassi/binary-transcoder

Usage

Node.js

ES module

import {
  fromBase64,
  fromBase64URL,
  fromBinary,
  fromHex,
  fromJSON,
  fromLatin1,
  transcode,
} from '@stassi/binary-transcoder'

CommonJS

const {
  fromBase64,
  fromBase64URL,
  fromBinary,
  fromHex,
  fromJSON,
  fromLatin1,
  transcode,
} = require('@stassi/binary-transcoder')

Web

import {
  fromBase64,
  fromBase64URL,
  fromBinary,
  fromHex,
  fromJSON,
  fromLatin1,
  transcode,
} from 'https://cdn.skypack.dev/@stassi/binary-transcoder'

Examples

Base64 encoding

transcode([0x3e, 0x3f, 0xfe, 0xff]).toBase64()
// 'Pj/+/w=='

Base64 decoding

fromBase64('Pj/+/w==').toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode({
  encoding: 'base64',
  text: 'Pj/+/w==',
}).toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>

Base64URL encoding

transcode([0x3e, 0x3f, 0xfe, 0xff]).toBase64URL()
// 'Pj_-_w'

Base64URL decoding

fromBase64URL('Pj_-_w').toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode({
  encoding: 'base64url',
  text: 'Pj_-_w',
}).toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>

Binary encoding

transcode([0b1001011, 0b1100101, 0b1111001]).toBinary()
// '010010110110010101111001'

Binary decoding

fromBinary('010010110110010101111001').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
  encoding: 'binary',
  text: '010010110110010101111001',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>

Buffer encoding

fromHex('4b6579').toBuffer()
// Buffer <4B, 65, 79>
transcode({
  encoding: 'hex',
  text: '4b6579',
}).toBuffer()
// Buffer <4B, 65, 79>

Buffer decoding

transcode(Buffer.from([0x4b, 0x65, 0x79])).toHex()
// '4b6579'

Hexadecimal encoding

fromLatin1('Key').toHex()
// '4b6579'
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toHex()
// '4b6579'

Hexadecimal decoding

fromHex('4b6579').toLatin1()
// 'Key'
transcode({
  encoding: 'hex',
  text: '4b6579',
}).toLatin1()
// 'Key'

JSON encoding

fromLatin1('Key').toJSON()
// '{"type":"Buffer","data":[75,101,121]}'
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toJSON()
// '{"type":"Buffer","data":[75,101,121]}'

JSON decoding

fromJSON('{"type":"Buffer","data":[75,101,121]}').toLatin1()
// 'Key'
transcode({
  encoding: 'json',
  text: '{"type":"Buffer","data":[75,101,121]}',
}).toLatin1()
// 'Key'

Latin-1 encoding

transcode([0x4b, 0x65, 0x79]).toLatin1()
// 'Key'

Latin-1 decoding

fromLatin1('Key').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>

number encoding

transcode([0x4b, 0x65, 0x79]).toNumber()
// 4941177

number decoding

transcode(4941177).toUInt8Array()
// Uint8Array <4B, 65, 79>

number[] encoding

fromLatin1('Key').toArray()
// [75, 101, 121]
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toArray()
// [75, 101, 121]

number[] decoding

Byte

transcode([0b1001011, 0b1100101, 0b1111001]).toLatin1()
// 'Key'

Decimal

transcode([75, 101, 121]).toLatin1()
// 'Key'

Hexadecimal

transcode([0x4b, 0x65, 0x79]).toLatin1()
// 'Key'

Nibble

transcode([0b100_1011, 0b110_0101, 0b111_1001]).toLatin1()
// 'Key'

Octal

transcode([0o113, 0o145, 0o171]).toLatin1()
// 'Key'

Uint8Array encoding

fromLatin1('Key').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>

Uint8Array decoding

transcode(Uint8Array.from([75, 101, 121])).toLatin1()
// 'Key'

UTF-8 encoding

transcode([0x4b, 0x65, 0x79]).toUTF8()
// 'Key'

UTF-16 LE encoding

transcode([0x4b, 0x65, 0x79]).toUTF16LE()
// '敋'

Interface & types

Function signatures provided here for reference. Built-in types are automatically usable in JavaScript. TypeScript is optional and not required.

transcode

type Transcode = (
  param:
    | Buffer
    | number
    | number[]
    | Uint8Array
    | {
        encoding: 'base64' | 'base64url' | 'binary' | 'hex' | 'json' | 'latin1'
        text: string
      }
) => {
  toArray(): number[]
  toBase64(): string
  toBase64URL(): string
  toBinary(): string
  toBuffer(): Buffer
  toHex(): string
  toJSON(): string
  toLatin1(): string
  toNumber(): number
  toUInt8Array(): Uint8Array
  toUTF8(): string
  toUTF16LE(): string
}

fromBase64, fromBase64URL, fromBinary, fromHex, fromJSON, fromLatin1

type FromString = (text: string) => {
  toArray(): number[]
  toBase64(): string
  toBase64URL(): string
  toBinary(): string
  toBuffer(): Buffer
  toHex(): string
  toJSON(): string
  toLatin1(): string
  toNumber(): number
  toUInt8Array(): Uint8Array
  toUTF8(): string
  toUTF16LE(): string
}

Package Sidebar

Install

npm i @stassi/binary-transcoder

Weekly Downloads

1

Version

0.11.2

License

MIT

Unpacked Size

60.7 kB

Total Files

20

Last publish

Collaborators

  • stassi