@zag-js/file-utils 0.82.1 → 1.0.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 +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +56 -2
- package/dist/index.mjs +56 -3
- package/package.json +10 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
declare const getFileEntries: (items: DataTransferItemList, traverseDirectories: boolean | undefined) => Promise<(File | (File | null)[] | null | undefined)[]>;
|
|
2
|
+
|
|
1
3
|
declare function dataURItoBlob(uri: string): Blob;
|
|
2
4
|
|
|
3
5
|
declare function isMSEdge(win: Window): win is Window & {
|
|
@@ -48,4 +50,4 @@ declare function isValidFileSize(file: File, minSize?: number, maxSize?: number)
|
|
|
48
50
|
|
|
49
51
|
declare function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null];
|
|
50
52
|
|
|
51
|
-
export { type ApplicationFileMimeType, type AudioFileMimeType, type DownloadFileOptions, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
|
53
|
+
export { type ApplicationFileMimeType, type AudioFileMimeType, type DownloadFileOptions, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getFileEntries, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
declare const getFileEntries: (items: DataTransferItemList, traverseDirectories: boolean | undefined) => Promise<(File | (File | null)[] | null | undefined)[]>;
|
|
2
|
+
|
|
1
3
|
declare function dataURItoBlob(uri: string): Blob;
|
|
2
4
|
|
|
3
5
|
declare function isMSEdge(win: Window): win is Window & {
|
|
@@ -48,4 +50,4 @@ declare function isValidFileSize(file: File, minSize?: number, maxSize?: number)
|
|
|
48
50
|
|
|
49
51
|
declare function isValidFileType(file: File, accept: string | undefined): [boolean, FileError | null];
|
|
50
52
|
|
|
51
|
-
export { type ApplicationFileMimeType, type AudioFileMimeType, type DownloadFileOptions, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
|
53
|
+
export { type ApplicationFileMimeType, type AudioFileMimeType, type DownloadFileOptions, type FileError, type FileMimeType, type FileMimeTypeGroup, type FontFileMimeType, type ImageFileMimeType, type TextFileMimeType, type VideoFileMimeType, dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getFileEntries, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// src/data-transfer.ts
|
|
4
|
+
var getItemEntry = (item) => typeof item.getAsEntry === "function" ? item.getAsEntry() : typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null;
|
|
5
|
+
var isDirectoryEntry = (entry) => entry.isDirectory;
|
|
6
|
+
var isFileEntry = (entry) => entry.isFile;
|
|
7
|
+
var addRelativePath = (file, path) => {
|
|
8
|
+
Object.defineProperty(file, "relativePath", { value: path ? `${path}/${file.name}` : file.name });
|
|
9
|
+
return file;
|
|
10
|
+
};
|
|
11
|
+
var getFileEntries = (items, traverseDirectories) => Promise.all(
|
|
12
|
+
Array.from(items).filter((item) => item.kind === "file").map((item) => {
|
|
13
|
+
const entry = getItemEntry(item);
|
|
14
|
+
if (!entry) return null;
|
|
15
|
+
if (isDirectoryEntry(entry) && traverseDirectories) {
|
|
16
|
+
return getDirectoryFiles(entry.createReader(), `${entry.name}`);
|
|
17
|
+
}
|
|
18
|
+
if (isFileEntry(entry)) {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
entry.file((file) => {
|
|
21
|
+
resolve(addRelativePath(file, ""));
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}).filter((b) => b)
|
|
26
|
+
);
|
|
27
|
+
var 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((entries2) => entries2.flat()));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const promises = entries.map((entry) => {
|
|
36
|
+
if (!entry) return null;
|
|
37
|
+
if (isDirectoryEntry(entry)) {
|
|
38
|
+
return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
|
|
39
|
+
}
|
|
40
|
+
if (isFileEntry(entry)) {
|
|
41
|
+
return new Promise((resolve2) => {
|
|
42
|
+
entry.file((file) => {
|
|
43
|
+
resolve2(addRelativePath(file, path));
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}).filter((b) => b);
|
|
48
|
+
entryPromises.push(Promise.all(promises));
|
|
49
|
+
readDirectoryEntries();
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
readDirectoryEntries();
|
|
53
|
+
});
|
|
54
|
+
|
|
3
55
|
// src/data-url-to-blob.ts
|
|
4
56
|
function dataURItoBlob(uri) {
|
|
5
57
|
const binary = atob(uri.split(",")[1]);
|
|
@@ -48,7 +100,7 @@ function isExt(v) {
|
|
|
48
100
|
}
|
|
49
101
|
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
50
102
|
function getAcceptAttrString(accept) {
|
|
51
|
-
if (
|
|
103
|
+
if (accept == null) return;
|
|
52
104
|
if (typeof accept === "string") {
|
|
53
105
|
return accept;
|
|
54
106
|
}
|
|
@@ -107,7 +159,8 @@ function isValidFileSize(file, minSize, maxSize) {
|
|
|
107
159
|
// src/is-valid-file-type.ts
|
|
108
160
|
function isFileAccepted(file, accept) {
|
|
109
161
|
if (file && accept) {
|
|
110
|
-
const types = Array.isArray(accept) ? accept : accept.split(",");
|
|
162
|
+
const types = Array.isArray(accept) ? accept : typeof accept === "string" ? accept.split(",") : [];
|
|
163
|
+
if (types.length === 0) return true;
|
|
111
164
|
const fileName = file.name || "";
|
|
112
165
|
const mimeType = (file.type || "").toLowerCase();
|
|
113
166
|
const baseMimeType = mimeType.replace(/\/.*$/, "");
|
|
@@ -133,6 +186,7 @@ exports.dataURItoBlob = dataURItoBlob;
|
|
|
133
186
|
exports.downloadFile = downloadFile;
|
|
134
187
|
exports.getAcceptAttrString = getAcceptAttrString;
|
|
135
188
|
exports.getFileDataUrl = getFileDataUrl;
|
|
189
|
+
exports.getFileEntries = getFileEntries;
|
|
136
190
|
exports.getTotalFileSize = getTotalFileSize;
|
|
137
191
|
exports.isFileEqual = isFileEqual;
|
|
138
192
|
exports.isMSEdge = isMSEdge;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
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;
|
|
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)) {
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
entry.file((file) => {
|
|
19
|
+
resolve(addRelativePath(file, ""));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}).filter((b) => b)
|
|
24
|
+
);
|
|
25
|
+
var getDirectoryFiles = (reader, path = "") => new Promise((resolve) => {
|
|
26
|
+
const entryPromises = [];
|
|
27
|
+
const readDirectoryEntries = () => {
|
|
28
|
+
reader.readEntries((entries) => {
|
|
29
|
+
if (entries.length === 0) {
|
|
30
|
+
resolve(Promise.all(entryPromises).then((entries2) => entries2.flat()));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const promises = entries.map((entry) => {
|
|
34
|
+
if (!entry) return null;
|
|
35
|
+
if (isDirectoryEntry(entry)) {
|
|
36
|
+
return getDirectoryFiles(entry.createReader(), `${path}${entry.name}`);
|
|
37
|
+
}
|
|
38
|
+
if (isFileEntry(entry)) {
|
|
39
|
+
return new Promise((resolve2) => {
|
|
40
|
+
entry.file((file) => {
|
|
41
|
+
resolve2(addRelativePath(file, path));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}).filter((b) => b);
|
|
46
|
+
entryPromises.push(Promise.all(promises));
|
|
47
|
+
readDirectoryEntries();
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
readDirectoryEntries();
|
|
51
|
+
});
|
|
52
|
+
|
|
1
53
|
// src/data-url-to-blob.ts
|
|
2
54
|
function dataURItoBlob(uri) {
|
|
3
55
|
const binary = atob(uri.split(",")[1]);
|
|
@@ -46,7 +98,7 @@ function isExt(v) {
|
|
|
46
98
|
}
|
|
47
99
|
var isValidMIME = (v) => isMIMEType(v) || isExt(v);
|
|
48
100
|
function getAcceptAttrString(accept) {
|
|
49
|
-
if (
|
|
101
|
+
if (accept == null) return;
|
|
50
102
|
if (typeof accept === "string") {
|
|
51
103
|
return accept;
|
|
52
104
|
}
|
|
@@ -105,7 +157,8 @@ function isValidFileSize(file, minSize, maxSize) {
|
|
|
105
157
|
// src/is-valid-file-type.ts
|
|
106
158
|
function isFileAccepted(file, accept) {
|
|
107
159
|
if (file && accept) {
|
|
108
|
-
const types = Array.isArray(accept) ? accept : accept.split(",");
|
|
160
|
+
const types = Array.isArray(accept) ? accept : typeof accept === "string" ? accept.split(",") : [];
|
|
161
|
+
if (types.length === 0) return true;
|
|
109
162
|
const fileName = file.name || "";
|
|
110
163
|
const mimeType = (file.type || "").toLowerCase();
|
|
111
164
|
const baseMimeType = mimeType.replace(/\/.*$/, "");
|
|
@@ -127,4 +180,4 @@ function isValidFileType(file, accept) {
|
|
|
127
180
|
return [isAcceptable, isAcceptable ? null : "FILE_INVALID_TYPE"];
|
|
128
181
|
}
|
|
129
182
|
|
|
130
|
-
export { dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
|
183
|
+
export { dataURItoBlob, downloadFile, getAcceptAttrString, getFileDataUrl, getFileEntries, getTotalFileSize, isFileEqual, isMSEdge, isValidFileSize, isValidFileType };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/file-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "JS 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": "1.0.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"clean-package": "2.2.0"
|
|
@@ -33,9 +33,14 @@
|
|
|
33
33
|
"types": "dist/index.d.ts",
|
|
34
34
|
"exports": {
|
|
35
35
|
".": {
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/index.d.mts",
|
|
38
|
+
"default": "./dist/index.mjs"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"default": "./dist/index.js"
|
|
43
|
+
}
|
|
39
44
|
},
|
|
40
45
|
"./package.json": "./package.json"
|
|
41
46
|
},
|