Skip to content

unjs/undio

Repository files navigation

⇔ undio

npm version npm downloads bundle size codecov

⇔ Conventionally and safely convert between various JavaScript data types.

✅ Features

  • Type-safe usage
  • Runtime-type safety assertion
  • Auto type detection and conversion
  • Tree-shakable and compact build
  • Leverage runtime native performance (+Bun stream utils)

👍 Supported Types

Usage

Install package:

# ✨ Auto-detect
npx nypm install undio

# npm
npm install undio

# yarn
yarn add undio

# pnpm
pnpm install undio

# bun
bun install undio

Import:

ESM (Node.js, Bun)

import {} from "undio";

CommonJS (Legacy Node.js)

const {} = require("undio");

CDN (Deno, Bun and Browsers)

import {} from "https://esm.sh/undio";

Auto Convert

Undio automatically detects the input type and uses the proper method to convert it to the expected type.

Example:

import { detectType, toString, toReadableStream } from "undio";

// Convert any supported type (auto-detected)
const string = await toString(value);
const stream = await toReadableStream(value);

// "ArrayBuffer" | "Blob"| "DataView" | "NumberArray" | "ReadableStream" | "String" | "Uint8Array";
const type = detectType(value);

Note

Because of stream support, the return type can be a promise. Always make sure to use an await before them.

Note

Alternatively you can use low-level *To*(value) utils to explicitly convert from one type to another. See all utils section.

Runtime type checking

You can use is*(input) ans assert*(input) utils to validate input type.

Note

All conversion utilities use assertions for runtime type safety by default, so you don't need to manually do this.

Example:

import { isReadableStream, assertArrayBuffer } from "undio";

if (isReadableStream(value)) {
  /* do something */
}

assertArrayBuffer(value); // Throws an error if value is not ArrayBuffer
// do something

All utils

See all utils

Array Buffer

arrayBufferToBlob(arrayBuffer, options?)

Convert from ArrayBuffer to Blob

arrayBufferToDataView(arrayBuffer)

Convert from ArrayBuffer to DataView

arrayBufferToNumberArray(arrayBuffer)

Convert from ArrayBuffer to Number Array

arrayBufferToReadableStream(arrayBuffer)

Convert from ArrayBuffer to ReadableStream

arrayBufferToResponse(arrayBuffer, init?)

Convert from ArrayBuffer to Response

arrayBufferToString(arrayBuffer)

Convert from ArrayBuffer to String

arrayBufferToUint8Array(arrayBuffer)

Convert from ArrayBuffer to Uint8Array

assertArrayBuffer(input)

Assert that input is an instance of ArrayBuffer or throw a TypeError.

isArrayBuffer(input)

Test if input is an instance of ArrayBuffer and return true or false.

toArrayBuffer(input)

Convert from any value to ArrayBuffer

Blob

assertBlob(input)

Assert that input is an instance of Blob or throw a TypeError.

blobToArrayBuffer(blob)

Convert from Blob to ArrayBuffer

blobToDataView(blob)

Convert from Blob to DataView

blobToNumberArray(blob)

Convert from Blob to Number Array

blobToReadableStream(blob)

Convert from Blob to ReadableStream

blobToResponse(blob, init?)

Convert from Blob to Response

blobToString(blob)

Convert from Blob to String

blobToUint8Array(blob)

Convert from Blob to Uint8Array

isBlob(input)

Test if input is an instance of Blob and return true or false.

toBlob(input)

Convert from any value to Blob

Data View

assertDataView(input)

Assert that input is an instance of DataView or throw a TypeError.

dataViewToArrayBuffer(dataView)

Convert from DataView to ArrayBuffer

dataViewToBlob(dataView, options?)

Convert from DataView to Blob

dataViewToNumberArray(dataView)

Convert from DataView to Number Array

dataViewToReadableStream(dataView)

Convert from DataView to ReadableStream

dataViewToResponse(dataView, init?)

Convert from DataView to Response

dataViewToString(dataView)

Convert from DataView to String

dataViewToUint8Array(dataView)

Convert from DataView to Uint8Array

isDataView(input)

Test if input is an instance of DataView and return true or false.

toDataView(input)

Convert from any value to DataView

Number Array

assertNumberArray(input)

Assert that input is an instance of Number Array or throw a TypeError.

isNumberArray(input)

Test if input is an instance of Number Array and return true or false.

numberArrayToArrayBuffer(numberArray)

Convert from Number Array to ArrayBuffer

numberArrayToBlob(numberArray, options?)

Convert from Number Array to Blob

numberArrayToDataView(numberArray)

Convert from Number Array to DataView

numberArrayToReadableStream(numberArray)

Convert from Number Array to ReadableStream

numberArrayToString(numberArray)

Convert from Number Array to String

numberArrayToUint8Array(numberArray)

Convert from Number Array to Uint8Array

toNumberArray(input)

Convert from any value to Number Array

Readable Stream

assertReadableStream(input)

Assert that input is an instance of ReadableStream or throw a TypeError.

isReadableStream(input)

Test if input is an instance of ReadableStream and return true or false.

readableStreamToArrayBuffer(readableStream)

Convert from ReadableStream to ArrayBuffer

readableStreamToBlob(readableStream, options?)

Convert from ReadableStream to Blob

readableStreamToDataView(readableStream)

Convert from ReadableStream to DataView

readableStreamToNumberArray(readableStream)

Convert from ReadableStream to Number Array

readableStreamToString(readableStream)

Convert from ReadableStream to String

readableStreamToUint8Array(readableStream)

Convert from ReadableStream to Uint8Array

toReadableStream(input)

Convert from any value to ReadableStream

toResponse(input)

Convert from any value to Response

Response

assertResponse(input)

Assert that input is an instance of Response or throw a TypeError.

isResponse(input)

Test if input is an instance of Response and return true or false.

responseToArrayBuffer(response)

Convert from Response to ArrayBuffer

responseToBlob(response)

Convert from Response to Blob

responseToDataView(response)

Convert from Response to DataView

responseToNumberArray(response)

Convert from Response to Number Array

responseToReadableStream(response)

Convert from Response to ReadableStreamReadableStream]

responseToString(response)

Convert from Response to String

responseToUint8Array(response)

Convert from Response to Uint8Array

String

assertString(input)

Assert that input is an instance of String or throw a TypeError.

isString(input)

Test if input is an instance of String and return true or false.

stringToArrayBuffer(string)

Convert from string to ArrayBuffer

stringToBlob(string, options?)

Convert from string to Blob

stringToDataView(string)

Convert from string to DataView

stringToNumberArray(string)

Convert from string to Number Array

stringToReadableStream(string)

Convert from string to ReadableStream

stringToUint8Array(string)

Convert from string to Uint8Array

Uinit8 Array

toUnit8Array(input)

Convert from any value to [Uinit8Array][Uinit8Array]

Uint8 Array

assertUint8Array(input)

Assert that input is an instance of Uint8Array or throw a TypeError.

isUint8Array(input)

Test if input is an instance of Uint8Array and return true or false.

uint8ArrayToArrayBuffer(uint8Array)

Convert from Uint8Array to ArrayBuffer

uint8ArrayToBlob(uint8Array, options?)

Convert from Uint8Array to Blob

uint8ArrayToDataView(uint8Array)

Convert from Uint8Array to DataView

uint8ArrayToNumberArray(uint8Array)

Convert from Uint8Array to Number Array

uint8ArrayToReadableStream(uint8Array)

Convert from Uint8Array to ReadableStream

uint8ArrayToResponse(uint8Array, init?)

Convert from Uint8Array to Response

uint8ArrayToString(uint8Array)

Convert from Uint8Array to String

convertTo(toType, input)

Convert from any value to any supported data type

detectType(input)

numberArrayToResponse(numberArray, init?)

Convert from Number Array to Response

readableStreamToResponse(readableStream, init?)

Convert from ReadableStream to Response

stringToResponse(string, init?)

Development

local development
  • Clone this repository
  • Install the latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Published under the MIT license. Made by community 💛


🤖 auto updated with automd

About

⇔ Conventionally and Safely convert between various JavaScript data types

Resources

License

Stars

Watchers

Forks

Packages

No packages published