UNPKG

@itslanguage/api

Version:
74 lines (65 loc) 1.91 kB
import "core-js/modules/es.array.iterator"; import "core-js/modules/es.array-buffer.slice"; import "core-js/modules/es.promise"; import "core-js/modules/es.typed-array.uint8-array"; import "core-js/modules/es.typed-array.to-locale-string"; /** * Some all-round re-usable utilities. * * @module api/utils */ /** * Convert the given ArrayBuffer to a base64 encoded string. * * @param {ArrayBuffer} data - The data to transform to base64. * * @returns {string} - The base64 encoded data. */ export function dataToBase64(data) { // eslint-disable-line import/prefer-default-export var binary = ''; var bytes = new Uint8Array(data); var len = bytes.byteLength; for (var i = 0; i < len; i += 1) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); } /** * Async function to convert a Blob to an ArrayBuffer. * @param {Blob} blob - The blob to read as ArrayBuffer * @returns {Promise} */ export function asyncBlobToArrayBuffer(blob) { return new Promise((resolve, reject) => { var fileReader = new FileReader(); fileReader.addEventListener('loadend', () => { resolve(fileReader.result); }); fileReader.addEventListener('error', () => { fileReader.abort(); reject(fileReader.error); }); fileReader.readAsArrayBuffer(blob); }); } /** * A Cheap test to check if the provided audio file has just the header, or that * it can be assumed to be "audio". The mimeType is used to do make a best * guess. Currently only WAV is supported. * * @param {number} filesize - Size of the file to check. * @param {string} mimeType - Type of the provided blob. * @returns {boolean} - Has audio. */ export function checkAudioIsNotEmpty(filesize, mimeType) { var result; switch (mimeType) { case 'audio/wav': result = filesize > 44; break; default: result = filesize > 0; } return result; }