@zag-js/file-utils
Advanced tools
Comparing version 0.0.0-dev-20230930104320 to 0.0.0-dev-20230930235819
@@ -1,3 +0,10 @@ | ||
declare const formatBytes: (bytes: number, decimals?: number) => string; | ||
declare function downloadFile(fileOrUrl: File | string, win: typeof window): void; | ||
interface FormatByteOptions { | ||
locale?: string; | ||
} | ||
declare const formatFileSize: (bytes: number, options?: FormatByteOptions) => string; | ||
declare function getAcceptAttrString(accept: Record<string, string[]> | string | undefined): string | undefined; | ||
declare const getFileDataUrl: (file: File | Blob) => Promise<string | undefined>; | ||
@@ -9,2 +16,6 @@ | ||
export { formatBytes, getFileDataUrl, getTotalFileSize, isFileEqual }; | ||
declare function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, string | null]; | ||
declare function isValidFileType(file: File, accept: string | undefined): [boolean, string | null]; | ||
export { downloadFile, formatFileSize, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isValidFileSize, isValidFileType }; |
@@ -23,21 +23,59 @@ "use strict"; | ||
__export(src_exports, { | ||
formatBytes: () => formatBytes, | ||
downloadFile: () => downloadFile, | ||
formatFileSize: () => formatFileSize, | ||
getAcceptAttrString: () => getAcceptAttrString, | ||
getFileDataUrl: () => getFileDataUrl, | ||
getTotalFileSize: () => getTotalFileSize, | ||
isFileEqual: () => isFileEqual | ||
isFileEqual: () => isFileEqual, | ||
isValidFileSize: () => isValidFileSize, | ||
isValidFileType: () => isValidFileType | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/format-bytes.ts | ||
var SIZES = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | ||
// src/download-file.ts | ||
function downloadFile(fileOrUrl, win) { | ||
const doc = win.document; | ||
const objectUrl = typeof fileOrUrl === "string" ? fileOrUrl : win.URL.createObjectURL(fileOrUrl); | ||
const anchor = doc.createElement("a"); | ||
anchor.style.display = "none"; | ||
anchor.href = objectUrl; | ||
anchor.download = typeof fileOrUrl === "string" ? objectUrl : fileOrUrl.name; | ||
doc.body.appendChild(anchor); | ||
anchor.click(); | ||
setTimeout(() => { | ||
if (typeof fileOrUrl !== "string") { | ||
win.URL.revokeObjectURL(objectUrl); | ||
} | ||
anchor?.parentNode?.removeChild(anchor); | ||
}, 0); | ||
} | ||
// src/format-file-size.ts | ||
var SIZES = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | ||
var KILO = 1024; | ||
var formatBytes = (bytes, decimals = 2) => { | ||
if (bytes === 0) { | ||
return "0 Bytes"; | ||
} | ||
const dm = decimals <= 0 ? 0 : decimals || 2; | ||
var formatFileSize = (bytes, options = {}) => { | ||
const { locale = "en-US" } = options; | ||
if (bytes === 0) | ||
return "0 B"; | ||
const i = Math.floor(Math.log(bytes) / Math.log(KILO)); | ||
return parseFloat((bytes / Math.pow(KILO, i)).toFixed(dm)) + " " + SIZES[i]; | ||
const fileSize = bytes / Math.pow(KILO, i); | ||
const formattedSize = new Intl.NumberFormat(locale).format(fileSize); | ||
return `${formattedSize} ${SIZES[i]}`; | ||
}; | ||
// src/get-accept-attr.ts | ||
function isMIMEType(v) { | ||
return v === "audio/*" || v === "video/*" || v === "image/*" || v === "text/*" || /\w+\/[-+.\w]+/g.test(v); | ||
} | ||
function isExt(v) { | ||
return /^.*\.[\w]+$/.test(v); | ||
} | ||
function getAcceptAttrString(accept) { | ||
if (!accept) | ||
return; | ||
if (typeof accept === "string") | ||
return accept; | ||
return Object.entries(accept).reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], []).filter((v) => isMIMEType(v) || isExt(v)).join(","); | ||
} | ||
// src/get-file-data-url.ts | ||
@@ -72,9 +110,56 @@ var getFileDataUrl = async (file) => { | ||
}; | ||
// src/is-valid-file-size.ts | ||
var isDefined = (v) => v !== void 0 && v !== null; | ||
function isValidFileSize(file, minSize, maxSize) { | ||
if (isDefined(file.size)) { | ||
if (isDefined(minSize) && isDefined(maxSize)) { | ||
if (file.size > maxSize) | ||
return [false, "TOO_LARGE"]; | ||
if (file.size < minSize) | ||
return [false, "TOO_SMALL"]; | ||
} else if (isDefined(minSize) && file.size < minSize) { | ||
return [false, "TOO_SMALL"]; | ||
} else if (isDefined(maxSize) && file.size > maxSize) { | ||
return [false, "TOO_LARGE"]; | ||
} | ||
} | ||
return [true, null]; | ||
} | ||
// src/is-valid-file-type.ts | ||
function isFileAccepted(file, accept) { | ||
if (file && accept) { | ||
const types = Array.isArray(accept) ? accept : accept.split(","); | ||
const fileName = file.name || ""; | ||
const mimeType = (file.type || "").toLowerCase(); | ||
const baseMimeType = mimeType.replace(/\/.*$/, ""); | ||
return types.some((type) => { | ||
const validType = type.trim().toLowerCase(); | ||
if (validType.charAt(0) === ".") { | ||
return fileName.toLowerCase().endsWith(validType); | ||
} | ||
if (validType.endsWith("/*")) { | ||
return baseMimeType === validType.replace(/\/.*$/, ""); | ||
} | ||
return mimeType === validType; | ||
}); | ||
} | ||
return true; | ||
} | ||
function isValidFileType(file, accept) { | ||
const isAcceptable = file.type === "application/x-moz-file" || isFileAccepted(file, accept); | ||
return [isAcceptable, isAcceptable ? null : "FILE_INVALID_TYPE"]; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
formatBytes, | ||
downloadFile, | ||
formatFileSize, | ||
getAcceptAttrString, | ||
getFileDataUrl, | ||
getTotalFileSize, | ||
isFileEqual | ||
isFileEqual, | ||
isValidFileSize, | ||
isValidFileType | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@zag-js/file-utils", | ||
"version": "0.0.0-dev-20230930104320", | ||
"version": "0.0.0-dev-20230930235819", | ||
"description": "JS File API utilities", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28924
286