generated from zayne-labs/create-ts-library
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4d1276
commit 9242b24
Showing
7 changed files
with
84 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zayne-labs/toolkit": patch | ||
--- | ||
|
||
refactor: remake handleFile util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
type ValidationRules = { | ||
allowedFileTypes?: string[]; | ||
disallowDuplicates?: boolean; | ||
fileLimit?: number; | ||
maxFileSize?: number; | ||
}; | ||
|
||
type ErrorContext = { | ||
message: string; | ||
}; | ||
|
||
type FileValidationOptions = { | ||
existingFileArray?: File[]; | ||
newFileList: FileList; | ||
onError?: (errorContext: ErrorContext) => void; | ||
validationRules?: ValidationRules; | ||
}; | ||
|
||
const toMegaByte = (size: number) => size * 1024 * 1024; | ||
|
||
const handleFileValidation = (options: FileValidationOptions) => { | ||
const { existingFileArray = [], newFileList, onError, validationRules } = options; | ||
|
||
const { allowedFileTypes, disallowDuplicates, fileLimit, maxFileSize } = validationRules ?? {}; | ||
|
||
const validFilesArray: File[] = []; | ||
|
||
const isFileUnique = (file: File) => { | ||
return existingFileArray.every((fileItem) => fileItem.name !== file.name); | ||
}; | ||
|
||
const isFileLimitReached = (limit: number) => { | ||
return existingFileArray.length === limit || validFilesArray.length === limit; | ||
}; | ||
|
||
// == Loop through fileList and validate each file | ||
for (const file of newFileList) { | ||
if (fileLimit && isFileLimitReached(fileLimit)) { | ||
const message = `Maximum file limit of ${fileLimit} files has been reached`; | ||
|
||
onError?.({ message }); | ||
|
||
break; | ||
} | ||
|
||
if (allowedFileTypes && !allowedFileTypes.includes(file.type)) { | ||
const acceptedFilesString = allowedFileTypes.join(" | "); | ||
|
||
const message = `File type must be of: ${acceptedFilesString}`; | ||
|
||
onError?.({ message }); | ||
|
||
continue; | ||
} | ||
|
||
if (maxFileSize && file.size > toMegaByte(maxFileSize)) { | ||
const message = `Cannot upload a file larger than ${maxFileSize}mb`; | ||
|
||
onError?.({ message }); | ||
|
||
continue; | ||
} | ||
|
||
if (disallowDuplicates && !isFileUnique(file)) { | ||
const message = `File: "${file.name}" has already been selected`; | ||
|
||
onError?.({ message }); | ||
|
||
continue; | ||
} | ||
|
||
validFilesArray.push(file); | ||
} | ||
|
||
return validFilesArray; | ||
}; | ||
|
||
export { handleFileValidation }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from "./getSlotElement"; | ||
export { handleFileValidation } from "./handleFileValidation"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.