Skip to content

Commit 81875cf

Browse files
authored
fix: acceptableMimeTypes to support web standard format. (#966)
- Fixed `acceptableMimeTypes` to support web standard format.
1 parent d17862f commit 81875cf

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/utils/__tests__/getMimeTypesUIKitAccepts.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ describe('Global-utils/getMimeTypesUIKitAccepts', () => {
2222
});
2323
expect(getMimeTypesUIKitAccepts([])).toBe(allMimeTypes.join());
2424
});
25+
it('when given mime types, should return mime types string.', () => {
26+
const accept = getMimeTypesUIKitAccepts(['image/png', 'image/heic']);
27+
const expected = 'image/png,image/heic';
28+
expect(accept).toBe(expected);
29+
});
2530
});

src/utils/index.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,35 @@ export const SUPPORTED_MIMES = {
9696
],
9797
};
9898

99-
export const getMimeTypesUIKitAccepts = (acceptableMimeTypes?: Array<string>): string => {
100-
const { IMAGE, VIDEO, AUDIO } = SUPPORTED_MIMES;
101-
99+
export const getMimeTypesUIKitAccepts = (acceptableMimeTypes?: string[]): string => {
102100
if (Array.isArray(acceptableMimeTypes) && acceptableMimeTypes.length > 0) {
103-
return (
104-
acceptableMimeTypes
105-
.reduce((accumulator: Array<string>, acceptableMimeType: string): Array<string> => (
106-
accumulator.concat(
107-
match(acceptableMimeType)
108-
.with('image', () => IMAGE)
109-
.with('video', () => VIDEO)
110-
.with('audio', () => AUDIO)
111-
.otherwise(() => []),
112-
)
113-
), [])
114-
.join()
115-
);
116-
}
117-
return (
118-
Object.values(SUPPORTED_MIMES)
119-
.reduce((accumulator: Array<string>, mimeTypes: Array<string>) => (
120-
accumulator.concat(mimeTypes)
121-
), [])
122-
.join()
123-
);
124-
// concat() is fater than flat()
101+
return acceptableMimeTypes
102+
.reduce((prev, curr) => {
103+
switch (curr) {
104+
case 'image': {
105+
prev.push(...SUPPORTED_MIMES.IMAGE);
106+
break;
107+
}
108+
case 'video': {
109+
prev.push(...SUPPORTED_MIMES.VIDEO);
110+
break;
111+
}
112+
case 'audio': {
113+
prev.push(...SUPPORTED_MIMES.AUDIO);
114+
break;
115+
}
116+
default: {
117+
prev.push(curr);
118+
break;
119+
}
120+
}
121+
122+
return prev;
123+
}, [] as string[])
124+
.join();
125+
}
126+
127+
return Object.values(SUPPORTED_MIMES).reduce((prev, curr) => (prev.concat(curr)), []).join();
125128
};
126129

127130
/* eslint-disable no-redeclare */

0 commit comments

Comments
 (0)