Skip to content

Converters for Blob, Uint8Array, ReadableStream, ArrayBuffer, string in JavaScript/TypeScript

License

Notifications You must be signed in to change notification settings

nwtgck/binconv-npm

Repository files navigation

binconv

npm CircleCI

Binary converters for Blob, Uint8Array, ReadableStream, ArrayBuffer, string in JavaScript/TypeScript

Installation

npm i -S binconv

Converters

Here are avaiable converters.
Naming rule: AB should be aToB().

conversion function
Base64Uint8Array base64ToUint8Array()
BlobArrayBuffer blobToArrayBuffer()
BlobReadableStream blobToReadableStream()
BlobUint8Array blobToUint8Array()
ReadableStreamBlob readableStreamToBlob()
ReadableStreamUint8Array readableStreamToUint8Array()
stringUint8Array stringToUint8Array()
stringArrayBuffer stringArrayBuffer()
Uint8ArrayArrayBuffer uint8ArrayToArrayBuffer()
Uint8Array → Base64 uint8ArrayToBase64()
Uint8ArrayBlob uint8ArrayToBlob()
Uint8Array → hex string uint8ArrayToHexString()
Uint8ArrayReadableStream uint8ArrayToReadableStream()
Uint8Arraystring uint8ArrayToString()

Other binary utilities

function mergeUint8Array(a: Uint8Array, b: Uint8Array): Uint8Array;
function mergeAllUint8Arrays(arrays: ReadonlyArray<Uint8Array>): Uint8Array;

Usage

import * as binconv from 'binconv';

const blob = new Blob(["this is a blob"]);
const readableStream = binconv.blobToReadableStream(blob);

Usage (on-demand import)

You can import only specific conversion to reduce file size.

import {blobToReadableStream} from 'binconv/dist/src/blobToReadableStream';

const blob = new Blob(["this is a blob"]);
const readableStream = blobToReadableStream(blob);