Skip to content

Commit 8685b38

Browse files
Nolskiokonet
authored andcommitted
feat: Allow arrays to be passed as accepted file types
1 parent d138bc0 commit 8685b38

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import 'core-js/fn/string/ends-with';
1414

1515
export default function(file, acceptedFiles) {
1616
if (file && acceptedFiles) {
17-
const acceptedFilesArray = acceptedFiles.split(',');
17+
const acceptedFilesArray = (Array.isArray(acceptedFiles) ?
18+
acceptedFiles :
19+
acceptedFiles.split(','));
1820
const fileName = file.name || '';
1921
const mimeType = file.type || '';
2022
const baseMimeType = mimeType.replace(/\/.*$/, '');

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,20 @@ describe('accept', () => {
135135
type: 'random/type'
136136
}, acceptedMimeTypes)).toBe(true);
137137
});
138+
139+
it('should allow accepted files passed to be an array', () => {
140+
const acceptedMimeTypes = ['img/jpeg', '.pdf'];
141+
expect(accept({
142+
name: 'testfile.pdf',
143+
type: 'random/type'
144+
}, acceptedMimeTypes)).toBe(true);
145+
expect(accept({
146+
name: 'testfile.jpg',
147+
type: 'img/jpeg'
148+
}, acceptedMimeTypes)).toBe(true);
149+
expect(accept({
150+
name: 'testfile',
151+
type: 'application/json'
152+
}, acceptedMimeTypes)).toBe(false);
153+
});
138154
});

0 commit comments

Comments
 (0)