@technobuddha/library
Version:
A large library of useful functions
17 lines (16 loc) • 579 B
JavaScript
import map from 'lodash/map';
import build from '../build';
/**
* Convert any binary object into a data URL
*
* @param input The binary object
* @param mimeType The MIME type for the URL
* @returns The data URL
*/
export function dataURL(input, mimeType) {
const buffer = input instanceof ArrayBuffer ? input : input.buffer;
const bytes = new Uint8Array(buffer);
return `data:${mimeType};base64,${btoa(build(map(bytes, c => String.fromCharCode(c))))}`;
//return `data:${mimeType};base64,${Buffer.from(buffer).toString('base64')}`;
}
export default dataURL;