based-blob-js
v1.0.3
Published
Convert blobs from and to base64 data url strings.
Downloads
327
Maintainers
Readme
Convert blobs to and from base64 data url strings. Alternate of https://github.com/Xyfir/based-blob (DELETED somehow from both npm and github)
Usage
(async function() {
const b = require('based-blob-js');
const base64String = 'some base64 data...';
const blob = b.toBlob(base64String);
const b64s = await b.toBase64(blob);
console.log(b64s == base64String); // true
})();API
toBlob(base64[, contentType])
Converts a base64 data url string to a blob.
Parameters
base64: string- The string to convert.contentType: string- Optional - Used for the Blob constructor.
Returns
Blob - The blob built from the base64 string.
based-blob-js
Convert blobs to and from base64 data URL strings.
This project is written in TypeScript. The published package includes compiled JavaScript in dist/ and TypeScript declaration files (.d.ts) so TypeScript-aware editors and consumers get correct typings.
Quick Start
- Install:
npm install based-blob-js - Build (from source):
npm run build(compilessrc/→dist/)
Usage (CommonJS)
const { toBlob, toBase64 } = require('based-blob-js');
// toBlob expects raw base64 (no data: prefix)
const blob = toBlob('aGVsbG8=', 'text/plain');
(async () => {
const dataUrl = await toBase64(blob);
console.log(dataUrl); // data:text/plain;base64,aGVsbG8=
})();Usage (ES Module / TypeScript)
import { toBlob, toBase64 } from 'based-blob-js';
const blob = toBlob('aGVsbG8=', 'text/plain');
const dataUrl = await toBase64(blob);API
toBlob(base64: string, contentType?: string): Blob— Create aBlobfrom a base64 string. Pass an optionalcontentType(MIME type).toBase64(blob: Blob): Promise<string>— Convert aBlobto a base64 data URL string (resolves to a string likedata:<type>;base64,<data>).
Notes
- If you have a full data URL (
data:<type>;base64,<data>), strip the prefix before callingtoBlob(e.g.base64 = dataUrl.split(',')[1]). - Type definitions are emitted into
dist/when building so TypeScript consumers get typings via thetypesfield inpackage.json.
Testing
- Run tests:
npm test(Jest configured with ajsdomenvironment).
Contributing
- Make changes in
src/, runnpm run build, and open a PR.
License
MIT
