Skip to content
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

Merged
merged 1 commit into from
May 23, 2024
Merged

Conversation

paullinator
Copy link
Member

@paullinator paullinator commented May 18, 2024

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?

  • Yes
  • No

Dependencies

none

Description

none

src/types.ts Outdated Show resolved Hide resolved
@@ -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
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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
Copy link
Contributor

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.

@paullinator
Copy link
Member Author

/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()
@github-actions github-actions bot force-pushed the paul/fixNullNumbers branch from 045f1d2 to 63d70e3 Compare May 23, 2024 17:52
@paullinator paullinator merged commit 7ffaaf2 into master May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants