-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
STRF-4803 - adding helper function to remove empty files from jquery … #1210
Conversation
Autotagging @bigcommerce/storefront-team @davidchin |
for (const pair of form.entries()) { | ||
const key = pair[0]; | ||
const val = pair[1]; | ||
if (typeof val === 'object' && val instanceof File) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍹 Are there any instances where val instanceof File
passes but typeof val === 'object'
does not? You can simplify this to just an instanceof
check if not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guess not - was trying to be safe for the undef case but realized that "undefined instanceof File" is safe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be better actually to leave the object check and just check if the size/name fields are propertyOf -> and then check the values are not empty/undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
*/ | ||
filterEmptyFilesFromForm(form) { | ||
try { | ||
for (const pair of form.entries()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍹 could use destructuring (i.e. const [key, val] of form.entries()
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have browser support requirements?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do but don't need to worry about them in this case. we've got webpack and babel to transpile down to the envs we specify in the browserlist file. :)
*/ | ||
filterEmptyFilesFromForm(form) { | ||
try { | ||
for (const [key, val] of form.entries()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like you should be able to use const [key, val] of form
An object implementing FormData can directly be used in a for...of structure, instead of entries(): for (var p of myFormData) is equivalent to for (var p of myFormData.entries()).
(https://developer.mozilla.org/en-US/docs/Web/API/FormData). maybe rename the param to formData to be explicit?
filterEmptyFilesFromForm(form) { | ||
try { | ||
for (const [key, val] of form.entries()) { | ||
if (val instanceof File) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might as well pull the second conditional into the first:
if (val instanceof File && val.name === '' && val.size === 0) {
could also simplify to if (val instanceof File and !val.name && !val.size) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - a couple nitpicks but nothing blocking
https://stackoverflow.com/questions/49672992/ajax-request-fails-when-sending-formdata-including-empty-file-input-in-safari
our version of jquery has a problem making ajax calls with empty files in safari. Appears the solution is to remove empty files before sending unless anyone has other suggestions.
@bigcommerce/storefront-team @bigcommerce/platform-engineering