We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Object._trim(obj)
undefined
var x = { foo: undefined, bar: undefined, baz: undefined, qux: 'quux', }; var y = Object._trim(x); // { qux: 'quux' }
Depending on how the rest of the language is implemented, this may not be necessary.
However, often times you'll want and expect undefined values to be ignored, but instead they get stringified or cause an error, such as with:
new URLSearchParams({ foo: undefined, bar: 'baz' })
foo=undefined&bar=baz
db.query({ id: undefined, username: 'coolaj86' })
This has to be implemented with care to keep type safety and to not break JIT optimizations.
For example:
delete
JSON.stringify
Date
Object._trim = function (obj) { let copied = {}; for (let k in obj) { if ('undefined' !== typeof obj[k]) { copied[k] = obj[k]; } } return copied; }
Object._trim = function (obj) { for (let k in obj) { if ('undefined' === typeof obj[k]) { delete obj[k]; } } return obj; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Rationalé
Depending on how the rest of the language is implemented, this may not be necessary.
However, often times you'll want and expect undefined values to be ignored, but instead they get stringified or cause an error, such as with:
new URLSearchParams({ foo: undefined, bar: 'baz' })
//foo=undefined&bar=baz
db.query({ id: undefined, username: 'coolaj86' })
Considerations
This has to be implemented with care to keep type safety and to not break JIT optimizations.
For example:
delete
may convert an optimized struct instance into an unoptimized key-value mapJSON.stringify
will convertDate
s to stringsOption A
Option B
The text was updated successfully, but these errors were encountered: