From 3b6794e0d09f14e028a5648e7e41eec042b9e73b Mon Sep 17 00:00:00 2001 From: Ferit Date: Fri, 9 Oct 2026 23:09:02 +0300 Subject: [PATCH] fix: ignore empty accept entries Assisted-by: OpenAI:Codex --- src/index.spec.ts | 18 ++++++++++++++++++ src/index.ts | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index 7aad922..9617f8d 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,6 +1,24 @@ import accept from "./index"; describe("accept", () => { + it.each([ + {acceptedFiles: ".png,"}, + {acceptedFiles: ", .png"}, + {acceptedFiles: ".png, ,"}, + {acceptedFiles: [".png", ""]}, + {acceptedFiles: [" ", ".png"]} + ])("should ignore empty entries in $acceptedFiles", ({acceptedFiles}) => { + expect(accept({name: "document.pdf", type: ""}, acceptedFiles)).toBe(false); + expect(accept({name: "photo.png", type: ""}, acceptedFiles)).toBe(true); + }); + + it.each([{acceptedFiles: " , "}, {acceptedFiles: ["", " "]}])( + "should accept any file when all entries are empty", + ({acceptedFiles}) => { + expect(accept({name: "document.pdf", type: "application/pdf"}, acceptedFiles)).toBe(true); + } + ); + it("should return true if called without acceptedFiles", () => { expect( accept( diff --git a/src/index.ts b/src/index.ts index 63f0ae0..0fb8b91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,7 +25,9 @@ export interface FileWithType { */ export default function accept(file?: FileWithType | null, acceptedFiles?: string | string[]): boolean { if (file && acceptedFiles) { - const acceptedFilesArray = Array.isArray(acceptedFiles) ? acceptedFiles : acceptedFiles.split(","); + const acceptedFilesArray = (Array.isArray(acceptedFiles) ? acceptedFiles : acceptedFiles.split(",")).filter( + type => type.trim() !== "" + ); if (acceptedFilesArray.length === 0) { return true; }