Refactor filterFileRequirements

This commit is contained in:
Leonardo Murça 2022-11-18 15:48:00 -03:00
parent c2c7cf18a7
commit 817f48b527

View file

@ -1,20 +1,21 @@
const formattedFilenameExt = (name) =>
`.${name.split(".").pop().toLowerCase()}`;
const areRequirementsFulfilled = (requirements, file) =>
requirements.maxSize >= file.size &&
requirements.supportedFormats.includes(formattedFilenameExt(file.name));
export function filterFileRequirements(files, requirements) {
let accepted = [];
let rejected = [];
for (var i = 0, file; (file = files[i]); i++) {
Array.from(files).forEach((file) => {
if (file) {
if (
requirements.maxSize >= file.size &&
requirements.supportedFormats.includes(
`.${file.name.split(".").pop().toLowerCase()}`
)
) {
if (areRequirementsFulfilled(requirements, file)) {
accepted.push(file);
} else {
rejected.push(file);
}
}
}
});
return { accepted, rejected };
}