Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ describe('toFile()', () => {
expect(fileWithPath.path).toBe(`./${name}`);
});

it('always sets {path} and {relativePath} to a string, even for a bare File', () => {
const file = new File([], 'test.json');
const fileWithPath = toFileWithPath(file);
expect(typeof fileWithPath.path).toBe('string');
expect(typeof fileWithPath.relativePath).toBe('string');
});

it('uses the File {webkitRelativePath} as {path} if it exists', () => {
const name = 'test.json';
const path = 'test/test.json';
Expand Down
10 changes: 5 additions & 5 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,8 @@ export const COMMON_MIME_TYPES: Map<string, string> = new Map([
['zsh', 'text/x-scriptzsh']
]);

export function toFileWithPath(file: FileWithPath, path?: string, h?: FileSystemHandle): FileWithPath {
const f = withMimeType(file);
export function toFileWithPath(file: File, path?: string, h?: FileSystemHandle): FileWithPath {
const f = withMimeType(file) as FileWithPath;
const {webkitRelativePath} = file;
const p =
typeof path === 'string'
Expand Down Expand Up @@ -1232,12 +1232,12 @@ export function toFileWithPath(file: FileWithPath, path?: string, h?: FileSystem
}

export interface FileWithPath extends File {
readonly path?: string;
readonly path: string;
readonly relativePath: string;
readonly handle?: FileSystemFileHandle;
readonly relativePath?: string;
}

function withMimeType(file: FileWithPath) {
function withMimeType(file: File) {
const {name} = file;
const hasExtension = name && name.lastIndexOf('.') !== -1;

Expand Down