Blob-Fns
Blob-Fns is a lightweight npm package for generating and handling small Blob objects in various formats, making it easier to work with binary data in web applications.
🚀 Features
The Blob Utilities package provides a variety of functions to work with Blob objects in JavaScript and TypeScript. These functions cover creating Blobs from different data types, converting Blobs to other formats, downloading Blobs as files, and compressing Blob data. The package is lightweight and easy to use.
Functions Offered
- createAndDownloadBlob: Create a Blob from data and download it as a file.
- fetchAndDownloadBlob: Fetch a Blob from url and download it as a file.
- blobToBase64: Convert a Blob to a Base64 string.
- base64ToBlob: Convert a Base64 string to a Blob.
- getBlobMetadata: Retrieve metadata (type and size) from a Blob.
- compressBlob: Compress an image Blob to a specified quality.
- createTextBlob: Create a Blob from a text string.
- createJSONBlob: Create a Blob from a JSON object.
- createImageBlob: Create a Blob from a Base64 image string.
- createBinaryBlob: Create a Blob from binary data.
- blobToJSON: Convert a Blob back to a JSON object.
- blobToImage: Convert a Blob back to an image in Base64 format.
- blobToText: Convert a Blob back to a text string.
💡 Use Cases
File Downloads
Allow users to download generated or fetched data as files, such as exporting data to CSV, JSON, or generating PDFs.
Image Manipulation
Handle image data for resizing, cropping, compressing, and converting between different image formats before displaying or uploading.
Data Storage
Save and retrieve binary data in IndexedDB or localStorage for offline access or caching purposes.
Media Handling
Manage audio and video blobs for media playback, recording, and manipulation.
Clipboard Operations
Copy blobs (e.g., images, files) to the clipboard for pasting into other applications.
Conversion Between Formats
Convert between various data formats such as ArrayBuffer, Base64, and Blob to support diverse application needs.
📦 Installation
Install the package using npm:
npm install blob-fns
or
yarn add blob-fns
📚 Usage
import { createAndDownloadBlob } from "blob-fns";
const textBlob = new Blob(["Hello, world!"]);
createAndDownloadBlob(textBlob, "hello.txt", "text/plain");
import { fetchAndDownloadBlob } from "blob-fns";
fetchAndDownloadBlob('URL', "FILENAME", "MIMETYPE");
import { blobToBase64 } from "blob-fns";
const blob = new Blob(["Hello, world!"], { type: "text/plain" });
blobToBase64(blob).then((base64) => {
console.log(base64);
});
import { base64ToBlob } from "blob-fns";
const blob = base64ToBlob("BASE64","MIMETYPE");
console.log(blob);
import { getBlobMetadata } from "blob-fns";
const blob = new Blob(["Hello, world!"], { type: "text/plain" });
const metadata = getBlobMetadata(blob);
console.log(metadata);
import { compressBlob } from "blob-fns";
const base64Image = "data:image/png;base64,...";
const imageBlob = base64ToBlob(base64Image, "image/png");
compressBlob(imageBlob, 0.7).then((compressedBlob) => {
console.log(compressedBlob);
});
import { createTextBlob } from "blob-fns";
const textBlob = createTextBlob("Hello, world!");
console.log(textBlob);
import { createJSONBlob } from "blob-fns";
const jsonObject = { message: "Hello, world!" };
const jsonBlob = createJSONBlob(jsonObject);
console.log(jsonBlob);
import { createImageBlob } from "blob-fns";
const base64Image = "data:image/png;base64,...";
const imageBlob = createImageBlob(base64Image);
console.log(imageBlob);
import { createBinaryBlob } from "blob-fns";
const binaryData = new Uint8Array([72, 101, 108, 108, 111]);
const binaryBlob = createBinaryBlob(binaryData);
console.log(binaryBlob);
import { blobToText } from "blob-fns";
const textBlob = createTextBlob("Hello, world!");
blobToText(textBlob).then((text) => {
console.log(text);
});
import { blobToJSON } from "blob-fns";
const jsonBlob = createJSONBlob({ message: "Hello, world!" });
blobToJSON(jsonBlob).then((jsonObject) => {
console.log(jsonObject);
});
import { blobToImage } from "blob-fns";
const base64Image = "data:image/png;base64,...";
const imageBlob = createImageBlob(base64Image);
blobToImage(imageBlob).then((base64) => {
console.log(base64);
});
Check Out My Other Packages
Explore more useful packages by visiting my npm profile. Made with ❤️ by Ashish