UNPKG

972 BJavaScriptView Raw
1import typeson from './typeson.js'
2const { toStringTag } = typeson
3import file from './file.js';
4
5const filelist = {
6 file: file.file,
7 filelist: {
8 test(x) { return toStringTag(x) === 'FileList'; },
9 replace(fl) {
10 const arr = [];
11 for (let i = 0; i < fl.length; i++) {
12 arr[i] = fl.item(i);
13 }
14 return arr;
15 },
16 revive(o) {
17 return new FileList(o);
18 }
19 }
20};
21
22// Even if native impl is available, ctor can't be invoked from userland...
23
24/** `FileList` polyfill */
25export class FileList {
26 #files;
27 #length;
28
29 /** Set private properties and length */
30 constructor() {
31 this.#files = arguments[0];
32 this.#length = this.#files.length;
33 }
34
35 /**
36 * @param {number} index
37 * @returns {File}
38 */
39 item(index) { return this.#files[index] }
40
41 /** @returns {number} */
42 get length() { return this.#length }
43
44 /** @readonly @type {"FileList"} */
45 [Symbol.toStringTag] = 'FileList'
46}
47
48export default filelist;