⇔ Conventionally and safely convert between various JavaScript data types.
- Type-safe usage
- Runtime-type safety assertion
- Auto type detection and conversion
- Tree-shakable and compact build
- Leverage runtime native performance (+Bun stream utils)
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";
Undio automatically detects the input type and uses the proper method to convert it to the expected type.
Example:
import { detectType, toText, toReadableStream } from "undio";
// Convert any supported type (auto-detected)
const string = await toText(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.
You can use is*(input)
and 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
See all utils
Convert from ArrayBuffer to Base64
Convert from ArrayBuffer to Blob
Convert from ArrayBuffer to DataView
Convert from ArrayBuffer to Number Array
Convert from ArrayBuffer to ReadableStream
Convert from ArrayBuffer to Response
Convert from ArrayBuffer to Text
Convert from ArrayBuffer to Uint8Array
Assert that input is an instance of ArrayBuffer or throw a TypeError
.
Test if input is an instance of ArrayBuffer and return true
or false
.
Convert from any value to ArrayBuffer
Assert if input matches the Base64 data URL (data:[][;base64],) or throw a TypeError
.
Convert from Base64 to ArrayBuffer
Convert from Base64 to DataView
Convert from Base64 to Number Array
Convert from Base64 to ReadableStream
Convert from Base64 to Response
Convert from Base64 to Uint8Array
Test if input matches the Base64 data URL (data:[][;base64],) and return true
or false
.
Convert from any value to Base64
Assert that input is an instance of Blob or throw a TypeError
.
Convert from Blob to ArrayBuffer
Convert from Blob to Number Array
Convert from Blob to ReadableStream
Convert from Blob to Uint8Array
Test if input is an instance of Blob and return true
or false
.
Convert from any value to Blob
Assert that input is an instance of DataView or throw a TypeError
.
Convert from DataView to ArrayBuffer
Convert from DataView to Base64
Convert from DataView to Number Array
Convert from DataView to ReadableStream
Convert from DataView to Response
Convert from DataView to Uint8Array
Test if input is an instance of DataView and return true
or false
.
Convert from any value to DataView
Assert that input is an instance of NodeStream or throw a TypeError
.
Test if input is an instance of NodeStream and return true
or false
.
Convert from NodeStream to ArrayBuffer
Convert from NodeStream to Base64
Convert from NodeStream to Blob
Convert from NodeStream to DataView
Convert from NodeStream to Number Array
Convert from NodeStream to ReadableStream
Convert from NodeStream to Response
Convert from NodeStream to Text
Convert from NodeStream to Uint8Array
Assert that input is an instance of Number Array or throw a TypeError
.
Test if input is an instance of Number Array and return true
or false
.
Convert from Number Array to ArrayBuffer
Convert from Number Array to Base64
Convert from Number Array to Blob
Convert from Number Array to DataView
Convert from Number Array to ReadableStream
Convert from Number Array to Text
Convert from Number Array to Uint8Array
Convert from any value to Number Array
Assert that input is an instance of ReadableStream or throw a TypeError
.
Test if input is an instance of ReadableStream and return true
or false
.
Convert from ReadableStream to ArrayBuffer
Convert from ReadableStream to Base64
Convert from ReadableStream to Blob
Convert from ReadableStream to DataView
Convert from ReadableStream to Number Array
Convert from ReadableStream to Text
Convert from ReadableStream to Uint8Array
Convert from any value to ReadableStream
Convert from any value to Response
Assert that input is an instance of Response or throw a TypeError
.
Test if input is an instance of Response and return true
or false
.
Convert from Response to ArrayBuffer
Convert from Response to Base64
Convert from Response to DataView
Convert from Response to Number Array
Convert from Response to ReadableStreamReadableStream]
Convert from Response to Uint8Array
Convert from any value to Text
Assert that input is an instance of Text or throw a TypeError
.
Test if input is an instance of Text and return true
or false
.
Convert from Text to ArrayBuffer
Convert from Text to Number Array
Convert from Text to ReadableStream
Convert from Text to Uint8Array
Assert that input is an instance of Uint8Array or throw a TypeError
.
Test if input is an instance of Uint8Array and return true
or false
.
Convert from any value to Uint8Array
Convert from Uint8Array to ArrayBuffer
Convert from Uint8Array to Base64
Convert from Uint8Array to Blob
Convert from Uint8Array to DataView
Convert from Uint8Array to Number Array
Convert from Uint8Array to ReadableStream
Convert from Uint8Array to Response
Convert from Uint8Array to Text
Convert from any value to any supported data type
Convert from Number Array to Response
Convert from ReadableStream to Response
local development
Published under the MIT license.
Made by community 💛
🤖 auto updated with automd