@zag-js/file-utils 0.56.1 → 0.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.js +8 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -25,7 +25,7 @@ interface DownloadFileOptions {
|
|
|
25
25
|
}
|
|
26
26
|
declare function downloadFile(options: DownloadFileOptions): void;
|
|
27
27
|
|
|
28
|
-
declare function getAcceptAttrString(accept: Record<string, string[]> | string | undefined): string | undefined;
|
|
28
|
+
declare function getAcceptAttrString(accept: Record<string, string[]> | string | string[] | undefined): string | undefined;
|
|
29
29
|
|
|
30
30
|
declare const getFileDataUrl: (file: File | Blob) => Promise<string | undefined>;
|
|
31
31
|
|
|
@@ -33,10 +33,19 @@ declare const getTotalFileSize: (files: File[]) => number;
|
|
|
33
33
|
|
|
34
34
|
declare const isFileEqual: (file1: File, file2: File) => boolean;
|
|
35
35
|
|
|
36
|
-
type
|
|
36
|
+
type AnyString = string & {};
|
|
37
|
+
type FileError = "TOO_MANY_FILES" | "FILE_INVALID_TYPE" | "FILE_TOO_LARGE" | "FILE_TOO_SMALL" | "FILE_INVALID" | AnyString;
|
|
38
|
+
type ImageFileMimeType = "image/png" | "image/gif" | "image/jpeg" | "image/svg+xml" | "image/webp" | "image/avif" | "image/heic" | "image/bmp";
|
|
39
|
+
type ApplicationFileMimeType = "application/pdf" | "application/zip" | "application/json" | "application/xml" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/vnd.ms-powerpoint" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" | "application/rtf" | "application/x-tar" | "application/vnd.microsoft.portable-executable";
|
|
40
|
+
type TextFileMimeType = "text/css" | "text/csv" | "text/html" | "text/markdown" | "text/plain";
|
|
41
|
+
type FontFileMimeType = "font/ttf" | "font/otf" | "font/woff" | "font/woff2" | "font/eot";
|
|
42
|
+
type VideoFileMimeType = "video/mp4" | "video/webm" | "video/ogg" | "video/quicktime" | "video/x-msvideo";
|
|
43
|
+
type AudioFileMimeType = "audio/mpeg" | "audio/ogg" | "audio/wav" | "audio/webm" | "audio/aac" | "audio/flac" | "audio/x-m4a";
|
|
44
|
+
type FileMimeTypeGroup = "image/*" | "audio/*" | "video/*" | "text/*";
|
|
45
|
+
type FileMimeType = ImageFileMimeType | ApplicationFileMimeType | TextFileMimeType | FontFileMimeType | VideoFileMimeType | AudioFileMimeType | FileMimeTypeGroup | AnyString;
|
|
37
46
|
|
|
38
47
|
declare function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, FileError | null];
|
|
39
48
|
|
|
40
49
|
declare function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null];
|
|
41
50
|
|
|
42
|
-
export { type FileError, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
|
51
|
+
export { type ApplicationFileMimeType, type AudioFileMimeType, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ interface DownloadFileOptions {
|
|
|
25
25
|
}
|
|
26
26
|
declare function downloadFile(options: DownloadFileOptions): void;
|
|
27
27
|
|
|
28
|
-
declare function getAcceptAttrString(accept: Record<string, string[]> | string | undefined): string | undefined;
|
|
28
|
+
declare function getAcceptAttrString(accept: Record<string, string[]> | string | string[] | undefined): string | undefined;
|
|
29
29
|
|
|
30
30
|
declare const getFileDataUrl: (file: File | Blob) => Promise<string | undefined>;
|
|
31
31
|
|
|
@@ -33,10 +33,19 @@ declare const getTotalFileSize: (files: File[]) => number;
|
|
|
33
33
|
|
|
34
34
|
declare const isFileEqual: (file1: File, file2: File) => boolean;
|
|
35
35
|
|
|
36
|
-
type
|
|
36
|
+
type AnyString = string & {};
|
|
37
|
+
type FileError = "TOO_MANY_FILES" | "FILE_INVALID_TYPE" | "FILE_TOO_LARGE" | "FILE_TOO_SMALL" | "FILE_INVALID" | AnyString;
|
|
38
|
+
type ImageFileMimeType = "image/png" | "image/gif" | "image/jpeg" | "image/svg+xml" | "image/webp" | "image/avif" | "image/heic" | "image/bmp";
|
|
39
|
+
type ApplicationFileMimeType = "application/pdf" | "application/zip" | "application/json" | "application/xml" | "application/msword" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/vnd.ms-powerpoint" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" | "application/rtf" | "application/x-tar" | "application/vnd.microsoft.portable-executable";
|
|
40
|
+
type TextFileMimeType = "text/css" | "text/csv" | "text/html" | "text/markdown" | "text/plain";
|
|
41
|
+
type FontFileMimeType = "font/ttf" | "font/otf" | "font/woff" | "font/woff2" | "font/eot";
|
|
42
|
+
type VideoFileMimeType = "video/mp4" | "video/webm" | "video/ogg" | "video/quicktime" | "video/x-msvideo";
|
|
43
|
+
type AudioFileMimeType = "audio/mpeg" | "audio/ogg" | "audio/wav" | "audio/webm" | "audio/aac" | "audio/flac" | "audio/x-m4a";
|
|
44
|
+
type FileMimeTypeGroup = "image/*" | "audio/*" | "video/*" | "text/*";
|
|
45
|
+
type FileMimeType = ImageFileMimeType | ApplicationFileMimeType | TextFileMimeType | FontFileMimeType | VideoFileMimeType | AudioFileMimeType | FileMimeTypeGroup | AnyString;
|
|
37
46
|
|
|
38
47
|
declare function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, FileError | null];
|
|
39
48
|
|
|
40
49
|
declare function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null];
|
|
41
50
|
|
|
42
|
-
export { type FileError, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
|
51
|
+
export { type ApplicationFileMimeType, type AudioFileMimeType, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
package/dist/index.js
CHANGED
|
@@ -78,10 +78,16 @@ function isMIMEType(v) {
|
|
|
78
78
|
function isExt(v) {
|
|
79
79
|
return /^.*\.[\w]+$/.test(v);
|
|
80
80
|
}
|
|
81
|
+
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
81
82
|
function getAcceptAttrString(accept) {
|
|
82
83
|
if (!accept) return;
|
|
83
|
-
if (typeof accept === "string")
|
|
84
|
-
|
|
84
|
+
if (typeof accept === "string") {
|
|
85
|
+
return accept;
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(accept)) {
|
|
88
|
+
return accept.filter(isValidMIME).join(",");
|
|
89
|
+
}
|
|
90
|
+
return Object.entries(accept).reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], []).filter(isValidMIME).join(",");
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
// src/get-file-data-url.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/data-url-to-blob.ts","../src/download-file.ts","../src/get-accept-attr.ts","../src/get-file-data-url.ts","../src/get-total-file-size.ts","../src/is-file-equal.ts","../src/is-valid-file-size.ts","../src/is-valid-file-type.ts"],"sourcesContent":["export * from \"./data-url-to-blob\"\nexport * from \"./download-file\"\nexport * from \"./get-accept-attr\"\nexport * from \"./get-file-data-url\"\nexport * from \"./get-total-file-size\"\nexport * from \"./is-file-equal\"\nexport * from \"./is-valid-file-size\"\nexport * from \"./is-valid-file-type\"\nexport type * from \"./types.ts\"\n","export function dataURItoBlob(uri: string): Blob {\n const binary = atob(uri.split(\",\")[1])\n\n // separate out the mime component\n const mimeString = uri.split(\",\")[0].split(\":\")[1].split(\";\")[0]\n\n // write the bytes of the string to an ArrayBuffer\n const buffer = new ArrayBuffer(binary.length)\n\n // create a view into the buffer\n const intArray = new Uint8Array(buffer)\n\n for (let i = 0; i < binary.length; i++) {\n intArray[i] = binary.charCodeAt(i)\n }\n\n return new Blob([buffer], { type: mimeString })\n}\n","export function isMSEdge(win: Window): win is Window & { navigator: { msSaveOrOpenBlob: Function } } {\n // @ts-ignore\n return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob)\n}\n\ninterface DownloadFileOptions {\n /**\n * The name of the file\n */\n name?: string\n /**\n * The MIME type of the file\n */\n type?: string\n /**\n * The file contents\n */\n file: File | Blob | string\n /**\n * The window environment\n */\n win: typeof window\n}\n\nexport function downloadFile(options: DownloadFileOptions) {\n const { file, win, type, name } = options\n\n const doc = win.document\n\n const obj = typeof file === \"string\" ? new Blob([file], { type }) : file\n const fileName = typeof file === \"string\" ? name : file instanceof File ? file.name : undefined\n\n if (isMSEdge(win)) {\n win.navigator.msSaveOrOpenBlob(obj, fileName || \"file-download\")\n return\n }\n\n const url = win.URL.createObjectURL(obj)\n\n const anchor = doc.createElement(\"a\")\n anchor.style.display = \"none\"\n anchor.href = url\n anchor.rel = \"noopener\"\n anchor.download = fileName || \"file-download\"\n\n doc.documentElement.appendChild(anchor)\n anchor.click()\n\n setTimeout(() => {\n win.URL.revokeObjectURL(url)\n anchor.remove()\n }, 0)\n}\n","function isMIMEType(v: string) {\n return v === \"audio/*\" || v === \"video/*\" || v === \"image/*\" || v === \"text/*\" || /\\w+\\/[-+.\\w]+/g.test(v)\n}\n\nfunction isExt(v: string) {\n return /^.*\\.[\\w]+$/.test(v)\n}\n\nexport function getAcceptAttrString(accept: Record<string, string[]> | string | undefined) {\n if (!accept) return\n if (typeof accept === \"string\") return accept\n return Object.entries(accept)\n .reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], [] as string[])\n .filter(
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/data-url-to-blob.ts","../src/download-file.ts","../src/get-accept-attr.ts","../src/get-file-data-url.ts","../src/get-total-file-size.ts","../src/is-file-equal.ts","../src/is-valid-file-size.ts","../src/is-valid-file-type.ts"],"sourcesContent":["export * from \"./data-url-to-blob\"\nexport * from \"./download-file\"\nexport * from \"./get-accept-attr\"\nexport * from \"./get-file-data-url\"\nexport * from \"./get-total-file-size\"\nexport * from \"./is-file-equal\"\nexport * from \"./is-valid-file-size\"\nexport * from \"./is-valid-file-type\"\nexport type * from \"./types.ts\"\n","export function dataURItoBlob(uri: string): Blob {\n const binary = atob(uri.split(\",\")[1])\n\n // separate out the mime component\n const mimeString = uri.split(\",\")[0].split(\":\")[1].split(\";\")[0]\n\n // write the bytes of the string to an ArrayBuffer\n const buffer = new ArrayBuffer(binary.length)\n\n // create a view into the buffer\n const intArray = new Uint8Array(buffer)\n\n for (let i = 0; i < binary.length; i++) {\n intArray[i] = binary.charCodeAt(i)\n }\n\n return new Blob([buffer], { type: mimeString })\n}\n","export function isMSEdge(win: Window): win is Window & { navigator: { msSaveOrOpenBlob: Function } } {\n // @ts-ignore\n return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob)\n}\n\ninterface DownloadFileOptions {\n /**\n * The name of the file\n */\n name?: string\n /**\n * The MIME type of the file\n */\n type?: string\n /**\n * The file contents\n */\n file: File | Blob | string\n /**\n * The window environment\n */\n win: typeof window\n}\n\nexport function downloadFile(options: DownloadFileOptions) {\n const { file, win, type, name } = options\n\n const doc = win.document\n\n const obj = typeof file === \"string\" ? new Blob([file], { type }) : file\n const fileName = typeof file === \"string\" ? name : file instanceof File ? file.name : undefined\n\n if (isMSEdge(win)) {\n win.navigator.msSaveOrOpenBlob(obj, fileName || \"file-download\")\n return\n }\n\n const url = win.URL.createObjectURL(obj)\n\n const anchor = doc.createElement(\"a\")\n anchor.style.display = \"none\"\n anchor.href = url\n anchor.rel = \"noopener\"\n anchor.download = fileName || \"file-download\"\n\n doc.documentElement.appendChild(anchor)\n anchor.click()\n\n setTimeout(() => {\n win.URL.revokeObjectURL(url)\n anchor.remove()\n }, 0)\n}\n","function isMIMEType(v: string) {\n return v === \"audio/*\" || v === \"video/*\" || v === \"image/*\" || v === \"text/*\" || /\\w+\\/[-+.\\w]+/g.test(v)\n}\n\nfunction isExt(v: string) {\n return /^.*\\.[\\w]+$/.test(v)\n}\n\nconst isValidMIME = (v: string) => isMIMEType(v) || isExt(v)\n\nexport function getAcceptAttrString(accept: Record<string, string[]> | string | string[] | undefined) {\n if (!accept) return\n\n if (typeof accept === \"string\") {\n return accept\n }\n\n if (Array.isArray(accept)) {\n return accept.filter(isValidMIME).join(\",\")\n }\n\n return Object.entries(accept)\n .reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], [] as string[])\n .filter(isValidMIME)\n .join(\",\")\n}\n","export const getFileDataUrl = async (file: File | Blob) => {\n const reader = new FileReader()\n return new Promise<string | undefined>((resolve, reject) => {\n reader.onerror = () => {\n reader.abort()\n reject(new Error(\"There was an error reading a file\"))\n }\n\n reader.onloadend = () => {\n const { result } = reader\n if (result instanceof ArrayBuffer) {\n reject(new Error(\"Expected DataURL as string from Blob/File, got ArrayBuffer\"))\n } else {\n resolve(result || undefined)\n }\n }\n\n reader.readAsDataURL(file)\n })\n}\n","export const getTotalFileSize = (files: File[]) => {\n return files.reduce((acc, file) => acc + file.size, 0)\n}\n","export const isFileEqual = (file1: File, file2: File) => {\n return file1.name === file2.name && file1.size === file2.size && file1.type === file2.type\n}\n","import type { FileError } from \"./types\"\n\nconst isDefined = <T>(v: T | undefined): v is T => v !== undefined && v !== null\n\nexport function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, FileError | null] {\n if (isDefined(file.size)) {\n if (isDefined(minSize) && isDefined(maxSize)) {\n if (file.size > maxSize) return [false, \"FILE_TOO_LARGE\"]\n if (file.size < minSize) return [false, \"FILE_TOO_SMALL\"]\n } else if (isDefined(minSize) && file.size < minSize) {\n return [false, \"FILE_TOO_SMALL\"]\n } else if (isDefined(maxSize) && file.size > maxSize) {\n return [false, \"FILE_TOO_LARGE\"]\n }\n }\n return [true, null]\n}\n","import type { FileError } from \"./types\"\n\nfunction isFileAccepted(file: File | null, accept: string[] | string | undefined) {\n if (file && accept) {\n const types = Array.isArray(accept) ? accept : accept.split(\",\")\n\n const fileName = file.name || \"\"\n const mimeType = (file.type || \"\").toLowerCase()\n const baseMimeType = mimeType.replace(/\\/.*$/, \"\")\n\n return types.some((type) => {\n const validType = type.trim().toLowerCase()\n\n if (validType.charAt(0) === \".\") {\n return fileName.toLowerCase().endsWith(validType)\n }\n\n if (validType.endsWith(\"/*\")) {\n return baseMimeType === validType.replace(/\\/.*$/, \"\")\n }\n\n return mimeType === validType\n })\n }\n return true\n}\n\nexport function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null] {\n const isAcceptable = file.type === \"application/x-moz-file\" || isFileAccepted(file, accept)\n return [isAcceptable, isAcceptable ? null : \"FILE_INVALID_TYPE\"]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,SAAS,cAAc,KAAmB;AAC/C,QAAM,SAAS,KAAK,IAAI,MAAM,GAAG,EAAE,CAAC,CAAC;AAGrC,QAAM,aAAa,IAAI,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC;AAG/D,QAAM,SAAS,IAAI,YAAY,OAAO,MAAM;AAG5C,QAAM,WAAW,IAAI,WAAW,MAAM;AAEtC,WAAS,IAAI,GAAG,IAAI,OAAO,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,OAAO,gBAAgB;AAAA,IAC1D,WAAW,UAAU,OAAO,KAAK,KAAK,OAAO,SAAS;AACpD,aAAO,CAAC,OAAO,gBAAgB;AAAA,IACjC,WAAW,UAAU,OAAO,KAAK,KAAK,OAAO,SAAS;AACpD,aAAO,CAAC,OAAO,gBAAgB;AAAA,IACjC;AAAA,EACF;AACA,SAAO,CAAC,MAAM,IAAI;AACpB;;;ACdA,SAAS,eAAe,MAAmB,QAAuC;AAChF,MAAI,QAAQ,QAAQ;AAClB,UAAM,QAAQ,MAAM,QAAQ,MAAM,IAAI,SAAS,OAAO,MAAM,GAAG;AAE/D,UAAM,WAAW,KAAK,QAAQ;AAC9B,UAAM,YAAY,KAAK,QAAQ,IAAI,YAAY;AAC/C,UAAM,eAAe,SAAS,QAAQ,SAAS,EAAE;AAEjD,WAAO,MAAM,KAAK,CAAC,SAAS;AAC1B,YAAM,YAAY,KAAK,KAAK,EAAE,YAAY;AAE1C,UAAI,UAAU,OAAO,CAAC,MAAM,KAAK;AAC/B,eAAO,SAAS,YAAY,EAAE,SAAS,SAAS;AAAA,MAClD;AAEA,UAAI,UAAU,SAAS,IAAI,GAAG;AAC5B,eAAO,iBAAiB,UAAU,QAAQ,SAAS,EAAE;AAAA,MACvD;AAEA,aAAO,aAAa;AAAA,IACtB,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEO,SAAS,gBAAgB,MAAY,QAAyD;AACnG,QAAM,eAAe,KAAK,SAAS,4BAA4B,eAAe,MAAM,MAAM;AAC1F,SAAO,CAAC,cAAc,eAAe,OAAO,mBAAmB;AACjE;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -44,10 +44,16 @@ function isMIMEType(v) {
|
|
|
44
44
|
function isExt(v) {
|
|
45
45
|
return /^.*\.[\w]+$/.test(v);
|
|
46
46
|
}
|
|
47
|
+
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
47
48
|
function getAcceptAttrString(accept) {
|
|
48
49
|
if (!accept) return;
|
|
49
|
-
if (typeof accept === "string")
|
|
50
|
-
|
|
50
|
+
if (typeof accept === "string") {
|
|
51
|
+
return accept;
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(accept)) {
|
|
54
|
+
return accept.filter(isValidMIME).join(",");
|
|
55
|
+
}
|
|
56
|
+
return Object.entries(accept).reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], []).filter(isValidMIME).join(",");
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
// src/get-file-data-url.ts
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/data-url-to-blob.ts","../src/download-file.ts","../src/get-accept-attr.ts","../src/get-file-data-url.ts","../src/get-total-file-size.ts","../src/is-file-equal.ts","../src/is-valid-file-size.ts","../src/is-valid-file-type.ts"],"sourcesContent":["export function dataURItoBlob(uri: string): Blob {\n const binary = atob(uri.split(\",\")[1])\n\n // separate out the mime component\n const mimeString = uri.split(\",\")[0].split(\":\")[1].split(\";\")[0]\n\n // write the bytes of the string to an ArrayBuffer\n const buffer = new ArrayBuffer(binary.length)\n\n // create a view into the buffer\n const intArray = new Uint8Array(buffer)\n\n for (let i = 0; i < binary.length; i++) {\n intArray[i] = binary.charCodeAt(i)\n }\n\n return new Blob([buffer], { type: mimeString })\n}\n","export function isMSEdge(win: Window): win is Window & { navigator: { msSaveOrOpenBlob: Function } } {\n // @ts-ignore\n return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob)\n}\n\ninterface DownloadFileOptions {\n /**\n * The name of the file\n */\n name?: string\n /**\n * The MIME type of the file\n */\n type?: string\n /**\n * The file contents\n */\n file: File | Blob | string\n /**\n * The window environment\n */\n win: typeof window\n}\n\nexport function downloadFile(options: DownloadFileOptions) {\n const { file, win, type, name } = options\n\n const doc = win.document\n\n const obj = typeof file === \"string\" ? new Blob([file], { type }) : file\n const fileName = typeof file === \"string\" ? name : file instanceof File ? file.name : undefined\n\n if (isMSEdge(win)) {\n win.navigator.msSaveOrOpenBlob(obj, fileName || \"file-download\")\n return\n }\n\n const url = win.URL.createObjectURL(obj)\n\n const anchor = doc.createElement(\"a\")\n anchor.style.display = \"none\"\n anchor.href = url\n anchor.rel = \"noopener\"\n anchor.download = fileName || \"file-download\"\n\n doc.documentElement.appendChild(anchor)\n anchor.click()\n\n setTimeout(() => {\n win.URL.revokeObjectURL(url)\n anchor.remove()\n }, 0)\n}\n","function isMIMEType(v: string) {\n return v === \"audio/*\" || v === \"video/*\" || v === \"image/*\" || v === \"text/*\" || /\\w+\\/[-+.\\w]+/g.test(v)\n}\n\nfunction isExt(v: string) {\n return /^.*\\.[\\w]+$/.test(v)\n}\n\nexport function getAcceptAttrString(accept: Record<string, string[]> | string | undefined) {\n if (!accept) return\n if (typeof accept === \"string\") return accept\n return Object.entries(accept)\n .reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], [] as string[])\n .filter(
|
|
1
|
+
{"version":3,"sources":["../src/data-url-to-blob.ts","../src/download-file.ts","../src/get-accept-attr.ts","../src/get-file-data-url.ts","../src/get-total-file-size.ts","../src/is-file-equal.ts","../src/is-valid-file-size.ts","../src/is-valid-file-type.ts"],"sourcesContent":["export function dataURItoBlob(uri: string): Blob {\n const binary = atob(uri.split(\",\")[1])\n\n // separate out the mime component\n const mimeString = uri.split(\",\")[0].split(\":\")[1].split(\";\")[0]\n\n // write the bytes of the string to an ArrayBuffer\n const buffer = new ArrayBuffer(binary.length)\n\n // create a view into the buffer\n const intArray = new Uint8Array(buffer)\n\n for (let i = 0; i < binary.length; i++) {\n intArray[i] = binary.charCodeAt(i)\n }\n\n return new Blob([buffer], { type: mimeString })\n}\n","export function isMSEdge(win: Window): win is Window & { navigator: { msSaveOrOpenBlob: Function } } {\n // @ts-ignore\n return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob)\n}\n\ninterface DownloadFileOptions {\n /**\n * The name of the file\n */\n name?: string\n /**\n * The MIME type of the file\n */\n type?: string\n /**\n * The file contents\n */\n file: File | Blob | string\n /**\n * The window environment\n */\n win: typeof window\n}\n\nexport function downloadFile(options: DownloadFileOptions) {\n const { file, win, type, name } = options\n\n const doc = win.document\n\n const obj = typeof file === \"string\" ? new Blob([file], { type }) : file\n const fileName = typeof file === \"string\" ? name : file instanceof File ? file.name : undefined\n\n if (isMSEdge(win)) {\n win.navigator.msSaveOrOpenBlob(obj, fileName || \"file-download\")\n return\n }\n\n const url = win.URL.createObjectURL(obj)\n\n const anchor = doc.createElement(\"a\")\n anchor.style.display = \"none\"\n anchor.href = url\n anchor.rel = \"noopener\"\n anchor.download = fileName || \"file-download\"\n\n doc.documentElement.appendChild(anchor)\n anchor.click()\n\n setTimeout(() => {\n win.URL.revokeObjectURL(url)\n anchor.remove()\n }, 0)\n}\n","function isMIMEType(v: string) {\n return v === \"audio/*\" || v === \"video/*\" || v === \"image/*\" || v === \"text/*\" || /\\w+\\/[-+.\\w]+/g.test(v)\n}\n\nfunction isExt(v: string) {\n return /^.*\\.[\\w]+$/.test(v)\n}\n\nconst isValidMIME = (v: string) => isMIMEType(v) || isExt(v)\n\nexport function getAcceptAttrString(accept: Record<string, string[]> | string | string[] | undefined) {\n if (!accept) return\n\n if (typeof accept === \"string\") {\n return accept\n }\n\n if (Array.isArray(accept)) {\n return accept.filter(isValidMIME).join(\",\")\n }\n\n return Object.entries(accept)\n .reduce((a, [mimeType, ext]) => [...a, mimeType, ...ext], [] as string[])\n .filter(isValidMIME)\n .join(\",\")\n}\n","export const getFileDataUrl = async (file: File | Blob) => {\n const reader = new FileReader()\n return new Promise<string | undefined>((resolve, reject) => {\n reader.onerror = () => {\n reader.abort()\n reject(new Error(\"There was an error reading a file\"))\n }\n\n reader.onloadend = () => {\n const { result } = reader\n if (result instanceof ArrayBuffer) {\n reject(new Error(\"Expected DataURL as string from Blob/File, got ArrayBuffer\"))\n } else {\n resolve(result || undefined)\n }\n }\n\n reader.readAsDataURL(file)\n })\n}\n","export const getTotalFileSize = (files: File[]) => {\n return files.reduce((acc, file) => acc + file.size, 0)\n}\n","export const isFileEqual = (file1: File, file2: File) => {\n return file1.name === file2.name && file1.size === file2.size && file1.type === file2.type\n}\n","import type { FileError } from \"./types\"\n\nconst isDefined = <T>(v: T | undefined): v is T => v !== undefined && v !== null\n\nexport function isValidFileSize(file: File, minSize?: number, maxSize?: number): [boolean, FileError | null] {\n if (isDefined(file.size)) {\n if (isDefined(minSize) && isDefined(maxSize)) {\n if (file.size > maxSize) return [false, \"FILE_TOO_LARGE\"]\n if (file.size < minSize) return [false, \"FILE_TOO_SMALL\"]\n } else if (isDefined(minSize) && file.size < minSize) {\n return [false, \"FILE_TOO_SMALL\"]\n } else if (isDefined(maxSize) && file.size > maxSize) {\n return [false, \"FILE_TOO_LARGE\"]\n }\n }\n return [true, null]\n}\n","import type { FileError } from \"./types\"\n\nfunction isFileAccepted(file: File | null, accept: string[] | string | undefined) {\n if (file && accept) {\n const types = Array.isArray(accept) ? accept : accept.split(\",\")\n\n const fileName = file.name || \"\"\n const mimeType = (file.type || \"\").toLowerCase()\n const baseMimeType = mimeType.replace(/\\/.*$/, \"\")\n\n return types.some((type) => {\n const validType = type.trim().toLowerCase()\n\n if (validType.charAt(0) === \".\") {\n return fileName.toLowerCase().endsWith(validType)\n }\n\n if (validType.endsWith(\"/*\")) {\n return baseMimeType === validType.replace(/\\/.*$/, \"\")\n }\n\n return mimeType === validType\n })\n }\n return true\n}\n\nexport function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null] {\n const isAcceptable = file.type === \"application/x-moz-file\" || isFileAccepted(file, accept)\n return [isAcceptable, isAcceptable ? null : \"FILE_INVALID_TYPE\"]\n}\n"],"mappings":";AAAO,SAAS,cAAc,KAAmB;AAC/C,QAAM,SAAS,KAAK,IAAI,MAAM,GAAG,EAAE,CAAC,CAAC;AAGrC,QAAM,aAAa,IAAI,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC;AAG/D,QAAM,SAAS,IAAI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File API utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"clean-package": "../../../clean-package.config.json",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@zag-js/i18n-utils": "0.
|
|
27
|
+
"@zag-js/i18n-utils": "0.58.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"clean-package": "2.2.0"
|