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
5 changes: 5 additions & 0 deletions src/utils/__tests__/getMimeTypesUIKitAccepts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ describe('Global-utils/getMimeTypesUIKitAccepts', () => {
});
expect(getMimeTypesUIKitAccepts([])).toBe(allMimeTypes.join());
});
it('when given mime types, should return mime types string.', () => {
const accept = getMimeTypesUIKitAccepts(['image/png', 'image/heic']);
const expected = 'image/png,image/heic';
expect(accept).toBe(expected);
});
});
53 changes: 28 additions & 25 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,35 @@ export const SUPPORTED_MIMES = {
],
};

export const getMimeTypesUIKitAccepts = (acceptableMimeTypes?: Array<string>): string => {
const { IMAGE, VIDEO, AUDIO } = SUPPORTED_MIMES;

export const getMimeTypesUIKitAccepts = (acceptableMimeTypes?: string[]): string => {
if (Array.isArray(acceptableMimeTypes) && acceptableMimeTypes.length > 0) {
return (
acceptableMimeTypes
.reduce((accumulator: Array<string>, acceptableMimeType: string): Array<string> => (
accumulator.concat(
match(acceptableMimeType)
.with('image', () => IMAGE)
.with('video', () => VIDEO)
.with('audio', () => AUDIO)
.otherwise(() => []),
)
), [])
.join()
);
}
return (
Object.values(SUPPORTED_MIMES)
.reduce((accumulator: Array<string>, mimeTypes: Array<string>) => (
accumulator.concat(mimeTypes)
), [])
.join()
);
// concat() is fater than flat()
return acceptableMimeTypes
.reduce((prev, curr) => {
switch (curr) {
case 'image': {
prev.push(...SUPPORTED_MIMES.IMAGE);
break;
}
case 'video': {
prev.push(...SUPPORTED_MIMES.VIDEO);
break;
}
case 'audio': {
prev.push(...SUPPORTED_MIMES.AUDIO);
break;
}
default: {
prev.push(curr);
break;
}
}

return prev;
}, [] as string[])
.join();
}

return Object.values(SUPPORTED_MIMES).reduce((prev, curr) => (prev.concat(curr)), []).join();
};

/* eslint-disable no-redeclare */
Expand Down