@zag-js/file-utils 1.47.0 → 1.48.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.
Files changed (53) hide show
  1. package/dist/data-transfer.d.mts +1 -3
  2. package/dist/data-transfer.d.ts +1 -3
  3. package/dist/data-transfer.js +47 -80
  4. package/dist/data-transfer.mjs +43 -55
  5. package/dist/data-url-to-blob.d.mts +1 -3
  6. package/dist/data-url-to-blob.d.ts +1 -3
  7. package/dist/data-url-to-blob.js +11 -36
  8. package/dist/data-url-to-blob.mjs +7 -11
  9. package/dist/download-file.d.mts +30 -32
  10. package/dist/download-file.d.ts +30 -32
  11. package/dist/download-file.js +43 -72
  12. package/dist/download-file.mjs +39 -47
  13. package/dist/get-accept-attr.d.mts +1 -3
  14. package/dist/get-accept-attr.d.ts +1 -3
  15. package/dist/get-accept-attr.js +16 -39
  16. package/dist/get-accept-attr.mjs +12 -14
  17. package/dist/get-file-data-url.d.mts +1 -3
  18. package/dist/get-file-data-url.d.ts +1 -3
  19. package/dist/get-file-data-url.js +19 -45
  20. package/dist/get-file-data-url.mjs +15 -20
  21. package/dist/get-file-mime-type.d.mts +2 -5
  22. package/dist/get-file-mime-type.d.ts +2 -5
  23. package/dist/get-file-mime-type.js +8 -31
  24. package/dist/get-file-mime-type.mjs +4 -6
  25. package/dist/get-total-file-size.d.mts +1 -3
  26. package/dist/get-total-file-size.d.ts +1 -3
  27. package/dist/get-total-file-size.js +7 -30
  28. package/dist/get-total-file-size.mjs +3 -5
  29. package/dist/index.d.mts +12 -11
  30. package/dist/index.d.ts +12 -11
  31. package/dist/index.js +25 -41
  32. package/dist/index.mjs +11 -11
  33. package/dist/is-file-equal.d.mts +2 -4
  34. package/dist/is-file-equal.d.ts +2 -4
  35. package/dist/is-file-equal.js +10 -34
  36. package/dist/is-file-equal.mjs +5 -8
  37. package/dist/is-valid-file-size.d.mts +2 -5
  38. package/dist/is-valid-file-size.d.ts +2 -5
  39. package/dist/is-valid-file-size.js +14 -40
  40. package/dist/is-valid-file-size.mjs +10 -15
  41. package/dist/is-valid-file-type.d.mts +3 -6
  42. package/dist/is-valid-file-type.d.ts +3 -6
  43. package/dist/is-valid-file-type.js +43 -60
  44. package/dist/is-valid-file-type.mjs +38 -34
  45. package/dist/mime-types.d.mts +2 -4
  46. package/dist/mime-types.d.ts +2 -4
  47. package/dist/mime-types.js +11 -37
  48. package/dist/mime-types.mjs +6 -11
  49. package/dist/types.d.mts +10 -11
  50. package/dist/types.d.ts +10 -11
  51. package/dist/types.js +4 -18
  52. package/dist/types.mjs +1 -0
  53. package/package.json +4 -4
@@ -1,3 +1 @@
1
- declare const getFileEntries: (items: DataTransferItemList, traverseDirectories: boolean | undefined) => Promise<(File | (File | null)[] | null | undefined)[]>;
2
-
3
- export { getFileEntries };
1
+ export declare const getFileEntries: (items: DataTransferItemList, traverseDirectories: boolean | undefined) => Promise<((File | null)[] | File | null | undefined)[]>;
@@ -1,3 +1 @@
1
- declare const getFileEntries: (items: DataTransferItemList, traverseDirectories: boolean | undefined) => Promise<(File | (File | null)[] | null | undefined)[]>;
2
-
3
- export { getFileEntries };
1
+ export declare const getFileEntries: (items: DataTransferItemList, traverseDirectories: boolean | undefined) => Promise<((File | null)[] | File | null | undefined)[]>;
@@ -1,83 +1,50 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/data-transfer.ts
21
- var data_transfer_exports = {};
22
- __export(data_transfer_exports, {
23
- getFileEntries: () => getFileEntries
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
24
4
  });
25
- module.exports = __toCommonJS(data_transfer_exports);
26
- var getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
27
- var isDirectoryEntry = (entry) => entry.isDirectory;
28
- var isFileEntry = (entry) => entry.isFile;
29
- var addRelativePath = (file, path) => {
30
- Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
31
- return file;
5
+ // src/data-transfer.ts
6
+ const getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
7
+ const isDirectoryEntry = (entry) => entry.isDirectory;
8
+ const isFileEntry = (entry) => entry.isFile;
9
+ const addRelativePath = (file, path) => {
10
+ Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
11
+ return file;
32
12
  };
33
- var getFileEntries = (items, traverseDirectories) => Promise.all(
34
- Array.from(items).filter((item) => item.kind === "file").map((item) => {
35
- const entry = getItemEntry(item);
36
- if (!entry) return null;
37
- if (isDirectoryEntry(entry) && traverseDirectories) {
38
- return getDirectoryFiles(entry.createReader(), `${entry.name}`);
39
- }
40
- if (isFileEntry(entry) && typeof item.getAsFile === "function") {
41
- const file = item.getAsFile();
42
- return Promise.resolve(file ? addRelativePath(file, "") : null);
43
- }
44
- if (isFileEntry(entry)) {
45
- return new Promise((resolve) => {
46
- entry.file((file) => {
47
- resolve(addRelativePath(file, ""));
48
- });
49
- });
50
- }
51
- }).filter((b) => b)
52
- );
53
- var getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
54
- const entryPromises = [];
55
- const readDirectoryEntries = () => {
56
- reader.readEntries((entries) => {
57
- if (entries.length === 0) {
58
- resolve(Promise.all(entryPromises).then((entries2) => entries2.flat()));
59
- return;
60
- }
61
- const promises = entries.map((entry) => {
62
- if (!entry) return null;
63
- if (isDirectoryEntry(entry)) {
64
- return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
65
- }
66
- if (isFileEntry(entry)) {
67
- return new Promise((resolve2) => {
68
- entry.file((file) => {
69
- resolve2(addRelativePath(file, path));
70
- });
71
- });
72
- }
73
- }).filter((b) => b);
74
- entryPromises.push(Promise.all(promises));
75
- readDirectoryEntries();
76
- });
77
- };
78
- readDirectoryEntries();
79
- });
80
- // Annotate the CommonJS export names for ESM import in node:
81
- 0 && (module.exports = {
82
- getFileEntries
13
+ const getFileEntries = (items, traverseDirectories) => Promise.all(Array.from(items).filter((item) => item.kind === "file").map((item) => {
14
+ const entry = getItemEntry(item);
15
+ if (!entry) return null;
16
+ if (isDirectoryEntry(entry) && traverseDirectories) return getDirectoryFiles(entry.createReader(), `${entry.name}`);
17
+ if (isFileEntry(entry) && typeof item.getAsFile === "function") {
18
+ const file = item.getAsFile();
19
+ return Promise.resolve(file ? addRelativePath(file, "") : null);
20
+ }
21
+ if (isFileEntry(entry)) return new Promise((resolve) => {
22
+ entry.file((file) => {
23
+ resolve(addRelativePath(file, ""));
24
+ });
25
+ });
26
+ }).filter((b) => b));
27
+ const getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
28
+ const entryPromises = [];
29
+ const readDirectoryEntries = () => {
30
+ reader.readEntries((entries) => {
31
+ if (entries.length === 0) {
32
+ resolve(Promise.all(entryPromises).then((entries) => entries.flat()));
33
+ return;
34
+ }
35
+ const promises = entries.map((entry) => {
36
+ if (!entry) return null;
37
+ if (isDirectoryEntry(entry)) return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
38
+ if (isFileEntry(entry)) return new Promise((resolve) => {
39
+ entry.file((file) => {
40
+ resolve(addRelativePath(file, path));
41
+ });
42
+ });
43
+ }).filter((b) => b);
44
+ entryPromises.push(Promise.all(promises));
45
+ readDirectoryEntries();
46
+ });
47
+ };
48
+ readDirectoryEntries();
83
49
  });
50
+ exports.getFileEntries = getFileEntries;
@@ -1,58 +1,46 @@
1
1
  // src/data-transfer.ts
2
- var getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
3
- var isDirectoryEntry = (entry) => entry.isDirectory;
4
- var isFileEntry = (entry) => entry.isFile;
5
- var addRelativePath = (file, path) => {
6
- Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
7
- return file;
2
+ const getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
3
+ const isDirectoryEntry = (entry) => entry.isDirectory;
4
+ const isFileEntry = (entry) => entry.isFile;
5
+ const addRelativePath = (file, path) => {
6
+ Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
7
+ return file;
8
8
  };
9
- var getFileEntries = (items, traverseDirectories) => Promise.all(
10
- Array.from(items).filter((item) => item.kind === "file").map((item) => {
11
- const entry = getItemEntry(item);
12
- if (!entry) return null;
13
- if (isDirectoryEntry(entry) && traverseDirectories) {
14
- return getDirectoryFiles(entry.createReader(), `${entry.name}`);
15
- }
16
- if (isFileEntry(entry) && typeof item.getAsFile === "function") {
17
- const file = item.getAsFile();
18
- return Promise.resolve(file ? addRelativePath(file, "") : null);
19
- }
20
- if (isFileEntry(entry)) {
21
- return new Promise((resolve) => {
22
- entry.file((file) => {
23
- resolve(addRelativePath(file, ""));
24
- });
25
- });
26
- }
27
- }).filter((b) => b)
28
- );
29
- var getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
30
- const entryPromises = [];
31
- const readDirectoryEntries = () => {
32
- reader.readEntries((entries) => {
33
- if (entries.length === 0) {
34
- resolve(Promise.all(entryPromises).then((entries2) => entries2.flat()));
35
- return;
36
- }
37
- const promises = entries.map((entry) => {
38
- if (!entry) return null;
39
- if (isDirectoryEntry(entry)) {
40
- return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
41
- }
42
- if (isFileEntry(entry)) {
43
- return new Promise((resolve2) => {
44
- entry.file((file) => {
45
- resolve2(addRelativePath(file, path));
46
- });
47
- });
48
- }
49
- }).filter((b) => b);
50
- entryPromises.push(Promise.all(promises));
51
- readDirectoryEntries();
52
- });
53
- };
54
- readDirectoryEntries();
9
+ const getFileEntries = (items, traverseDirectories) => Promise.all(Array.from(items).filter((item) => item.kind === "file").map((item) => {
10
+ const entry = getItemEntry(item);
11
+ if (!entry) return null;
12
+ if (isDirectoryEntry(entry) && traverseDirectories) return getDirectoryFiles(entry.createReader(), `${entry.name}`);
13
+ if (isFileEntry(entry) && typeof item.getAsFile === "function") {
14
+ const file = item.getAsFile();
15
+ return Promise.resolve(file ? addRelativePath(file, "") : null);
16
+ }
17
+ if (isFileEntry(entry)) return new Promise((resolve) => {
18
+ entry.file((file) => {
19
+ resolve(addRelativePath(file, ""));
20
+ });
21
+ });
22
+ }).filter((b) => b));
23
+ const getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
24
+ const entryPromises = [];
25
+ const readDirectoryEntries = () => {
26
+ reader.readEntries((entries) => {
27
+ if (entries.length === 0) {
28
+ resolve(Promise.all(entryPromises).then((entries) => entries.flat()));
29
+ return;
30
+ }
31
+ const promises = entries.map((entry) => {
32
+ if (!entry) return null;
33
+ if (isDirectoryEntry(entry)) return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
34
+ if (isFileEntry(entry)) return new Promise((resolve) => {
35
+ entry.file((file) => {
36
+ resolve(addRelativePath(file, path));
37
+ });
38
+ });
39
+ }).filter((b) => b);
40
+ entryPromises.push(Promise.all(promises));
41
+ readDirectoryEntries();
42
+ });
43
+ };
44
+ readDirectoryEntries();
55
45
  });
56
- export {
57
- getFileEntries
58
- };
46
+ export { getFileEntries };
@@ -1,3 +1 @@
1
- declare function dataURItoBlob(uri: string): Blob;
2
-
3
- export { dataURItoBlob };
1
+ export declare function dataURItoBlob(uri: string): Blob;
@@ -1,3 +1 @@
1
- declare function dataURItoBlob(uri: string): Blob;
2
-
3
- export { dataURItoBlob };
1
+ export declare function dataURItoBlob(uri: string): Blob;
@@ -1,39 +1,14 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/data-url-to-blob.ts
21
- var data_url_to_blob_exports = {};
22
- __export(data_url_to_blob_exports, {
23
- dataURItoBlob: () => dataURItoBlob
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
24
4
  });
25
- module.exports = __toCommonJS(data_url_to_blob_exports);
5
+ // src/data-url-to-blob.ts
26
6
  function dataURItoBlob(uri) {
27
- const binary = atob(uri.split(",")[1]);
28
- const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
29
- const buffer = new ArrayBuffer(binary.length);
30
- const intArray = new Uint8Array(buffer);
31
- for (let i = 0; i < binary.length; i++) {
32
- intArray[i] = binary.charCodeAt(i);
33
- }
34
- return new Blob([buffer], { type: mimeString });
7
+ const binary = atob(uri.split(",")[1]);
8
+ const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
9
+ const buffer = new ArrayBuffer(binary.length);
10
+ const intArray = new Uint8Array(buffer);
11
+ for (let i = 0; i < binary.length; i++) intArray[i] = binary.charCodeAt(i);
12
+ return new Blob([buffer], { type: mimeString });
35
13
  }
36
- // Annotate the CommonJS export names for ESM import in node:
37
- 0 && (module.exports = {
38
- dataURItoBlob
39
- });
14
+ exports.dataURItoBlob = dataURItoBlob;
@@ -1,14 +1,10 @@
1
1
  // src/data-url-to-blob.ts
2
2
  function dataURItoBlob(uri) {
3
- const binary = atob(uri.split(",")[1]);
4
- const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
5
- const buffer = new ArrayBuffer(binary.length);
6
- const intArray = new Uint8Array(buffer);
7
- for (let i = 0; i < binary.length; i++) {
8
- intArray[i] = binary.charCodeAt(i);
9
- }
10
- return new Blob([buffer], { type: mimeString });
3
+ const binary = atob(uri.split(",")[1]);
4
+ const mimeString = uri.split(",")[0].split(":")[1].split(";")[0];
5
+ const buffer = new ArrayBuffer(binary.length);
6
+ const intArray = new Uint8Array(buffer);
7
+ for (let i = 0; i < binary.length; i++) intArray[i] = binary.charCodeAt(i);
8
+ return new Blob([buffer], { type: mimeString });
11
9
  }
12
- export {
13
- dataURItoBlob
14
- };
10
+ export { dataURItoBlob };
@@ -1,33 +1,31 @@
1
- interface DownloadFileOptions {
2
- /**
3
- * The name of the file
4
- */
5
- name?: string | undefined;
6
- /**
7
- * The MIME type of the file
8
- */
9
- type: string;
10
- /**
11
- * The file contents
12
- */
13
- file: File | Blob | string;
14
- /**
15
- * The window environment
16
- */
17
- win?: typeof window | undefined;
18
- /**
19
- * Whether to add a BOM (Byte Order Mark) to the file.
20
- * Useful for CSV files.
21
- */
22
- appendBOM?: boolean | undefined;
23
- /**
24
- * The timeout in milliseconds to revoke the object URL.
25
- * This is a safeguard for when the browser has not finished reading the
26
- * file data from memory before the URL is revoked.
27
- * @default 0
28
- */
29
- revokeTimeout?: number | undefined;
1
+ export interface DownloadFileOptions {
2
+ /**
3
+ * The name of the file
4
+ */
5
+ name?: string | undefined;
6
+ /**
7
+ * The MIME type of the file
8
+ */
9
+ type: string;
10
+ /**
11
+ * The file contents
12
+ */
13
+ file: File | Blob | string;
14
+ /**
15
+ * The window environment
16
+ */
17
+ win?: typeof window | undefined;
18
+ /**
19
+ * Whether to add a BOM (Byte Order Mark) to the file.
20
+ * Useful for CSV files.
21
+ */
22
+ appendBOM?: boolean | undefined;
23
+ /**
24
+ * The timeout in milliseconds to revoke the object URL.
25
+ * This is a safeguard for when the browser has not finished reading the
26
+ * file data from memory before the URL is revoked.
27
+ * @default 0
28
+ */
29
+ revokeTimeout?: number | undefined;
30
30
  }
31
- declare function downloadFile(options: DownloadFileOptions): void;
32
-
33
- export { type DownloadFileOptions, downloadFile };
31
+ export declare function downloadFile(options: DownloadFileOptions): void;
@@ -1,33 +1,31 @@
1
- interface DownloadFileOptions {
2
- /**
3
- * The name of the file
4
- */
5
- name?: string | undefined;
6
- /**
7
- * The MIME type of the file
8
- */
9
- type: string;
10
- /**
11
- * The file contents
12
- */
13
- file: File | Blob | string;
14
- /**
15
- * The window environment
16
- */
17
- win?: typeof window | undefined;
18
- /**
19
- * Whether to add a BOM (Byte Order Mark) to the file.
20
- * Useful for CSV files.
21
- */
22
- appendBOM?: boolean | undefined;
23
- /**
24
- * The timeout in milliseconds to revoke the object URL.
25
- * This is a safeguard for when the browser has not finished reading the
26
- * file data from memory before the URL is revoked.
27
- * @default 0
28
- */
29
- revokeTimeout?: number | undefined;
1
+ export interface DownloadFileOptions {
2
+ /**
3
+ * The name of the file
4
+ */
5
+ name?: string | undefined;
6
+ /**
7
+ * The MIME type of the file
8
+ */
9
+ type: string;
10
+ /**
11
+ * The file contents
12
+ */
13
+ file: File | Blob | string;
14
+ /**
15
+ * The window environment
16
+ */
17
+ win?: typeof window | undefined;
18
+ /**
19
+ * Whether to add a BOM (Byte Order Mark) to the file.
20
+ * Useful for CSV files.
21
+ */
22
+ appendBOM?: boolean | undefined;
23
+ /**
24
+ * The timeout in milliseconds to revoke the object URL.
25
+ * This is a safeguard for when the browser has not finished reading the
26
+ * file data from memory before the URL is revoked.
27
+ * @default 0
28
+ */
29
+ revokeTimeout?: number | undefined;
30
30
  }
31
- declare function downloadFile(options: DownloadFileOptions): void;
32
-
33
- export { type DownloadFileOptions, downloadFile };
31
+ export declare function downloadFile(options: DownloadFileOptions): void;
@@ -1,81 +1,52 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/download-file.ts
21
- var download_file_exports = {};
22
- __export(download_file_exports, {
23
- downloadFile: () => downloadFile
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
24
4
  });
25
- module.exports = __toCommonJS(download_file_exports);
26
- var BOM_REGEX = /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i;
27
- var MAC_REGEX = /Macintosh/;
28
- var APPLE_WEBKIT_REGEX = /AppleWebKit/;
29
- var SAFARI_REGEX = /Safari/;
5
+ // src/download-file.ts
6
+ const BOM_REGEX = /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i;
7
+ const MAC_REGEX = /Macintosh/;
8
+ const APPLE_WEBKIT_REGEX = /AppleWebKit/;
9
+ const SAFARI_REGEX = /Safari/;
30
10
  function getBlob(blobOrString, type, appendBOM) {
31
- let blob = typeof blobOrString === "string" ? new Blob([blobOrString], { type }) : blobOrString;
32
- if (appendBOM && BOM_REGEX.test(blob.type)) {
33
- return new Blob([String.fromCharCode(65279), blob], { type: blob.type });
34
- }
35
- return blob;
11
+ let blob = typeof blobOrString === "string" ? new Blob([blobOrString], { type }) : blobOrString;
12
+ if (appendBOM && BOM_REGEX.test(blob.type)) return new Blob([String.fromCharCode(65279), blob], { type: blob.type });
13
+ return blob;
36
14
  }
37
15
  function isMSEdge(win) {
38
- return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob);
16
+ return Boolean(win.navigator && win.navigator.msSaveOrOpenBlob);
39
17
  }
40
18
  function isWebView(win) {
41
- return win.navigator && MAC_REGEX.test(win.navigator.userAgent) && APPLE_WEBKIT_REGEX.test(win.navigator.userAgent) && !SAFARI_REGEX.test(win.navigator.userAgent);
19
+ return win.navigator && MAC_REGEX.test(win.navigator.userAgent) && APPLE_WEBKIT_REGEX.test(win.navigator.userAgent) && !SAFARI_REGEX.test(win.navigator.userAgent);
42
20
  }
43
21
  function downloadFile(options) {
44
- const { file, win = window, type, name, appendBOM, revokeTimeout = 0 } = options;
45
- const doc = win.document;
46
- const blob = getBlob(file, type, appendBOM);
47
- const fileName = (file instanceof File ? name || file.name : name) || "file-download";
48
- if (isMSEdge(win)) {
49
- win.navigator.msSaveOrOpenBlob(blob, fileName);
50
- return;
51
- }
52
- const isMacOSWebView = isWebView(win);
53
- const anchor = doc.createElement("a");
54
- const canUseDownload = "download" in anchor && !isMacOSWebView;
55
- if (canUseDownload) {
56
- const url2 = win.URL.createObjectURL(blob);
57
- anchor.href = url2;
58
- anchor.rel = "noopener";
59
- anchor.download = fileName;
60
- anchor.style.display = "none";
61
- doc.body.appendChild(anchor);
62
- anchor.dispatchEvent(new win.MouseEvent("click"));
63
- setTimeout(() => {
64
- win.URL.revokeObjectURL(url2);
65
- anchor.remove();
66
- }, revokeTimeout);
67
- return;
68
- }
69
- const url = win.URL.createObjectURL(blob);
70
- const popup = win.open(url, "_blank");
71
- if (!popup) {
72
- win.location.href = url;
73
- }
74
- setTimeout(() => {
75
- win.URL.revokeObjectURL(url);
76
- }, revokeTimeout);
22
+ const { file, win = window, type, name, appendBOM, revokeTimeout = 0 } = options;
23
+ const doc = win.document;
24
+ const blob = getBlob(file, type, appendBOM);
25
+ const fileName = (file instanceof File ? name || file.name : name) || "file-download";
26
+ if (isMSEdge(win)) {
27
+ win.navigator.msSaveOrOpenBlob(blob, fileName);
28
+ return;
29
+ }
30
+ const isMacOSWebView = isWebView(win);
31
+ const anchor = doc.createElement("a");
32
+ if ("download" in anchor && !isMacOSWebView) {
33
+ const url = win.URL.createObjectURL(blob);
34
+ anchor.href = url;
35
+ anchor.rel = "noopener";
36
+ anchor.download = fileName;
37
+ anchor.style.display = "none";
38
+ doc.body.appendChild(anchor);
39
+ anchor.dispatchEvent(new win.MouseEvent("click"));
40
+ setTimeout(() => {
41
+ win.URL.revokeObjectURL(url);
42
+ anchor.remove();
43
+ }, revokeTimeout);
44
+ return;
45
+ }
46
+ const url = win.URL.createObjectURL(blob);
47
+ if (!win.open(url, "_blank")) win.location.href = url;
48
+ setTimeout(() => {
49
+ win.URL.revokeObjectURL(url);
50
+ }, revokeTimeout);
77
51
  }
78
- // Annotate the CommonJS export names for ESM import in node:
79
- 0 && (module.exports = {
80
- downloadFile
81
- });
52
+ exports.downloadFile = downloadFile;