⇔ 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, 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.
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
See all utils
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 String
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 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 Number Array
Convert from DataView to ReadableStream
Convert from DataView to Response
Convert from DataView to String
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 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 Blob
Convert from Number Array to DataView
Convert from Number Array to ReadableStream
Convert from Number Array to String
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 Blob
Convert from ReadableStream to DataView
Convert from ReadableStream to Number Array
Convert from ReadableStream to String
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 DataView
Convert from Response to Number Array
Convert from Response to ReadableStreamReadableStream]
Convert from Response to String
Convert from Response to Uint8Array
Assert that input is an instance of String or throw a TypeError
.
Test if input is an instance of String and return true
or false
.
Convert from string to ArrayBuffer
Convert from string to DataView
Convert from string to Number Array
Convert from string to ReadableStream
Convert from string to Uint8Array
Convert from any value to [Uinit8Array][Uinit8Array]
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 Uint8Array to ArrayBuffer
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 String
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