-
Notifications
You must be signed in to change notification settings - Fork 8
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
Properly handle nulls in number type fields #190
Conversation
@@ -122,3 +122,8 @@ export const getStartOfMonthsFromNow = ( | |||
const month = date.getUTCMonth() | |||
return new Date(Date.UTC(year, month + months, 1, 0, 0, 0, 0)) | |||
} | |||
|
|||
export const safeParseFloat = (val: string): number => { | |||
if (val === '') return 0 |
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.
I was expecting this:
const parsed = parseFloat(val)
if (isNaN(parsed)) return 0
return parsed
That would catch everything, and not just blank strings. If the intent is to just filter blank strings. then maybe we should document that instead:
/**
* Parses number strings, treating blank inputs as 0
*/
Otherwise, the word "safe" seems to imply that it always returns valid values, regardless of input.
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.
I initially did the isNaN check but switched to only check blank strings since that "feels" like it could be interpreted as 0. If parseFloat actually got passed a "NaN" or null
which would also return null
then I was expecting to return null since that is not easily considered a 0 value. This matches biggystring which currently interprets empty string as a zero but not "NaN" or null.
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.
Then add the comment, and I will approve it.
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.
Fine, I'll do it myself.
@@ -122,3 +122,8 @@ export const getStartOfMonthsFromNow = ( | |||
const month = date.getUTCMonth() | |||
return new Date(Date.UTC(year, month + months, 1, 0, 0, 0, 0)) | |||
} | |||
|
|||
export const safeParseFloat = (val: string): number => { | |||
if (val === '') return 0 |
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.
Fine, I'll do it myself.
/rebase |
Null values can occur when empty strings ('') are sent to parseFloat which returns a NaN that when JSON.stringified returns a null. Properly handle existing data that has a null by cleaning it into a 0 and also preventing nulls by cleaning empty strings into a 0 with safeParseFloat()
045f1d2
to
63d70e3
Compare
Null values can occur when empty strings ('') are sent to parseFloat which returns a NaN that when JSON.stringified returns a null.
Properly handle existing data that has a null by cleaning it into a 0 and also preventing nulls by cleaning empty strings into a 0 with safeParseFloat()
CHANGELOG
Does this branch warrant an entry to the CHANGELOG?
Dependencies
noneDescription
none