-
-
Notifications
You must be signed in to change notification settings - Fork 208
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
fix: coerce undefined to string (#680) #681
base: master
Are you sure you want to change the base?
Conversation
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
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.
Hmm, this is a tricky one. When fjs receives an object with a property set to undefined
, it skips the property. I guess, the logic here might be somehow similar. Although skipping array's items might not be the best idea.
What should fjs do if it receives an array with undefined property? Possible solutions I see:
- skip the item
- leave the item equal to the undefined
- throw an error (not a bad solution btw)
@mcollina wdyt?
@kyleaupton thanks for your PR, I will block it until we get a consistent answer on what to do in this case. If we decide to coerse it to the empty string it would require to also make some changes for all other types + a few use cases.
Feels wrong to skip Elements. |
Yeah, this is not an easy one. |
@ivan-tymoshenko this issue is about strings, casting a undefined to an empty string seems ok to me. Dropping the value seems not good, or keeping it undefined. If we pass an number to a type string, it will be casted to a string. Why undefined should be different? |
I like this change for the same reasons as @mcollina |
Because if you have an object schema with a string property and pass an object with property equal to undefined it will skip the property instead of coercing it to the empty string. |
This issue can't be only about strings anyway. It should be about all types. |
Countering your argument |
Since when did we start use JSON.stringify as a rule to go? |
I use it now as an argument. |
Co-authored-by: Gürgün Dayıoğlu <[email protected]> Signed-off-by: Kyle Upton <[email protected]>
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.
Since we are casting null
to ""
, it makes sense to do the same for undefined
.
In the context of JSON.stringify
, both null
and undefined
result in null
, so maintaining consistency in our handling makes sense, so both serialize as string
.
If you want to cast the undefined to the empty string if it's not an object property and skip it if it's an object property, then it should also coerce every other type from undefined to it's nullish value. |
Do you mean for instance that in the same scenario but for a number, it should get coerced to 0? I would agree that it’s what this tool ought to behave like |
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.
So I agree that all functions need to conform to this behavior (nullish values get coerced to the falsy value of the corresponding type) if we're going to merge this:
fast-json-stringify/lib/serializer.js
Lines 53 to 62 in dfb627e
asNumber (i) { | |
const num = Number(i) | |
if (Number.isNaN(num)) { | |
throw new Error(`The value "${i}" cannot be converted to a number.`) | |
} else if (!Number.isFinite(num)) { | |
return 'null' | |
} else { | |
return '' + num | |
} | |
} |
Checklist
npm run test
andnpm run benchmark
and the Code of conduct
On my end all test pass, however coverage is at 96% which is causing an error.