'use strict'; /** * @description Check if value is classified as a string. * @category Type * @param value - The value to check. * @returns Returns true if value is a string, else false. */ function isString(value) { return typeof value === "string"; } /** * @description Check if value is undefined. * @category Type * @param value - The value to check. * @returns Returns true if value is undefined, else false. */ function isUndefined(value) { return value === void 0; } /** * @description Check if value is classified as a blob. * @category Type * @param value - The value to check * @returns Returns true if value is a blob, else false. */ function isBlob(value) { return Object.prototype.toString.call(value) === "[object Blob]"; } async function transformWithFileReader(file, type, encoding) { return await new Promise((resolve, reject) => { const fileReader = new FileReader(); fileReader.onloadend = () => { resolve(fileReader.result); }; fileReader.onerror = reject; fileReader.onabort = reject; switch (type) { case "arrayBuffer": fileReader.readAsArrayBuffer(file); break; case "dataUrl": fileReader.readAsDataURL(file); break; case "text": fileReader.readAsText(file, encoding); break; case "binaryString": fileReader.readAsBinaryString(file); break; default: reject(new Error("Type is invalid")); } }); } async function toArrayBuffer(file) { if (file.arrayBuffer) { return await file.arrayBuffer(); } return (await transformWithFileReader(file, "arrayBuffer")); } async function toBinaryString(file) { return (await transformWithFileReader(file, "binaryString")); } async function toDataUrl(file) { return (await transformWithFileReader(file, "dataUrl")); } async function toText(file, encoding) { return (await transformWithFileReader(file, "text", encoding)); } async function toAny(file, options, encoding) { const type = isUndefined(options) ? "arrayBuffer" : isString(options) ? options : options.type; const textEncoding = isUndefined(options) ? undefined : isString(options) ? encoding : options.encoding; if (type === "blob") { return await toBlob(file); } if (type === "objectUrl") { return await toObjectUrl(file); } return await transformWithFileReader(file, type, textEncoding); } async function toBlob(file) { if (isBlob(file)) { return file; } return file.slice(); } async function toObjectUrl(params) { return URL.createObjectURL(params); } exports.toAny = toAny; exports.toArrayBuffer = toArrayBuffer; exports.toBinaryString = toBinaryString; exports.toBlob = toBlob; exports.toDataUrl = toDataUrl; exports.toObjectUrl = toObjectUrl; exports.toText = toText;