javascript-binary-converter
Version:
A utility package to quickly handle and convert various Javascript binary objects
41 lines • 1.49 kB
JavaScript
import { arrayBufferToDecimalBytes, typedArrayToBytes } from "../utils/binary";
import { blobToBase64, blobToCanvas } from "../utils/blob";
import * as blobUtils from "../utils/image";
export default class BlobConverter {
constructor(original) {
this.original = original;
}
async toUint8Array() {
const arrayBuffer = await this.toArrayBuffer();
return new Uint8Array(arrayBuffer);
}
async toInt8Array() {
const arrayBuffer = await this.toArrayBuffer();
return new Int8Array(arrayBuffer);
}
async toArrayBuffer() {
return await this.original.arrayBuffer();
}
async toImage(config) {
return blobUtils.binaryToImage(this.original, config ? config : undefined); //
}
async toCanvas() {
return blobToCanvas(this.original);
}
async toBytes() {
const uint8 = await this.toUint8Array();
return typedArrayToBytes(uint8);
}
async toDecimalBytes({ isSigned = false } = {}) {
const arrayBuffer = await this.toArrayBuffer();
return arrayBufferToDecimalBytes(arrayBuffer, { isSigned });
}
/**
* Returns a base64 string. If you want a dataUrl appended to it, pass {appendDataUrl:true}
* In Node will always return plain base64
*/
toBase64(config = { appendDataUrl: false }) {
return blobToBase64(this.original, config);
}
}
//# sourceMappingURL=BlobConverter.js.map