Skip to content

Commit 94f1803

Browse files
jonkoopsrolandjitsu
authored andcommitted
refactor!: split file handles into seperate function
Signed-off-by: Jon Koops <[email protected]> BREAKING CHANGE: Arrays of `FileSystemFileHandle` are now handled in a dedicated `fromFileHandles()` function.
1 parent 91f497a commit 94f1803

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,13 @@ input.addEventListener("change", async (event) => {
107107
Convert [FileSystemFileHandle](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle) items to File objects:
108108

109109
```js
110-
import { fromEvent } from "file-selector";
110+
import { fromFileHandles } from "file-selector";
111111

112112
const handles = await window.showOpenFilePicker({ multiple: true });
113-
const files = await fromEvent(handles);
113+
const files = await fromFileHandles(handles);
114114
console.log(files);
115115
```
116116

117-
> [!NOTE]
118-
> The above is experimental and subject to change.
119-
120117
## Browser Support
121118

122119
Most browser support basic File selection with drag 'n' drop or file input:

src/file-selector.spec.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FileWithPath } from "./file.js";
2-
import { fromEvent } from "./file-selector.js";
2+
import { fromEvent, fromFileHandles } from "./file-selector.js";
33

44
it("returns a Promise", async () => {
55
const evt = new Event("test");
@@ -46,27 +46,29 @@ it("should return an empty array if the evt {target} has no {files} prop", async
4646
expect(files).toHaveLength(0);
4747
});
4848

49-
it("should return files if the arg is a list of FileSystemFileHandle", async () => {
50-
const name = "test.json";
51-
const [mockFile, mockHandle] = createFileSystemFileHandle(
52-
name,
53-
{ ping: true },
54-
{
55-
type: "application/json",
56-
},
57-
);
49+
describe("fromFileHandles", () => {
50+
it("retrieves files from file handles", async () => {
51+
const name = "test.json";
52+
const [mockFile, mockHandle] = createFileSystemFileHandle(
53+
name,
54+
{ ping: true },
55+
{
56+
type: "application/json",
57+
},
58+
);
5859

59-
const files = await fromEvent([mockHandle]);
60-
expect(files).toHaveLength(1);
61-
expect(files.every((file) => file instanceof File)).toBe(true);
60+
const files = await fromFileHandles([mockHandle]);
61+
expect(files).toHaveLength(1);
62+
expect(files.every((file) => file instanceof File)).toBe(true);
6263

63-
const [file] = files as FileWithPath[];
64+
const [file] = files as FileWithPath[];
6465

65-
expect(file.name).toBe(mockFile.name);
66-
expect(file.type).toBe(mockFile.type);
67-
expect(file.size).toBe(mockFile.size);
68-
expect(file.lastModified).toBe(mockFile.lastModified);
69-
expect(file.path).toBe(`./${name}`);
66+
expect(file.name).toBe(mockFile.name);
67+
expect(file.type).toBe(mockFile.type);
68+
expect(file.size).toBe(mockFile.size);
69+
expect(file.lastModified).toBe(mockFile.lastModified);
70+
expect(file.path).toBe(`./${name}`);
71+
});
7072
});
7173

7274
it("should return an empty array if the passed event is not a DragEvent", async () => {

src/file-selector.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const FILES_TO_IGNORE = [
1111
* NOTE: If some of the items are folders,
1212
* everything will be flattened and placed in the same list but the paths will be kept as a {path} property.
1313
*
14-
* EXPERIMENTAL: A list of https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle objects can also be passed as an arg
15-
* and a list of File objects will be returned.
16-
*
1714
* @param evt
1815
*/
1916
export async function fromEvent(
@@ -23,11 +20,6 @@ export async function fromEvent(
2320
return getDataTransferFiles(evt.dataTransfer, evt.type);
2421
} else if (isChangeEvt(evt)) {
2522
return getInputFiles(evt);
26-
} else if (
27-
Array.isArray(evt) &&
28-
evt.every((item) => "getFile" in item && typeof item.getFile === "function")
29-
) {
30-
return getFsHandleFiles(evt);
3123
}
3224
return [];
3325
}
@@ -52,10 +44,32 @@ function getInputFiles(event: Event): FileWithPath[] {
5244
return Array.from(event.target.files).map((file) => toFileWithPath(file));
5345
}
5446

55-
// Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
56-
async function getFsHandleFiles(handles: any[]) {
57-
const files = await Promise.all(handles.map((h) => h.getFile()));
58-
return files.map((file) => toFileWithPath(file));
47+
/**
48+
* Retrieves files from an array of `FileSystemHandle` objects from the File System API.
49+
*
50+
* @param handles The array of handles to convert.
51+
* @returns A promise that resolves to an array of files retrieved from the handles.
52+
*
53+
* @example
54+
* ```js
55+
* const handles = await window.showOpenFilePicker({ multiple: true });
56+
* const files = await fromFileHandles(handles);
57+
* ```
58+
*
59+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/File_System_API|MDN - File System API}
60+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle|MDN - `FileSystemHandle`}
61+
*/
62+
export function fromFileHandles(
63+
handles: FileSystemFileHandle[],
64+
): Promise<FileWithPath[]> {
65+
return Promise.all(handles.map((handle) => fromFileHandle(handle)));
66+
}
67+
68+
async function fromFileHandle(
69+
handle: FileSystemFileHandle,
70+
): Promise<FileWithPath> {
71+
const file = await handle.getFile();
72+
return toFileWithPath(file);
5973
}
6074

6175
async function getDataTransferFiles(dataTransfer: DataTransfer, type: string) {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { fromEvent } from "./file-selector.js";
1+
export { fromEvent, fromFileHandles } from "./file-selector.js";
22
export { FileWithPath } from "./file.js";

0 commit comments

Comments
 (0)