-
Notifications
You must be signed in to change notification settings - Fork 2
fix(client): parse Presto JSON response with custom reviver and BigInts #25
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9ed52f
fix(client): parse Presto JSON response with custom reviver and BigInts
alonsovb 797f9af
fix(client): improve big int parsing
alonsovb 1b2a3b7
fix(client): check compatibility with check function
alonsovb c933c9e
refactor(client): move compatibility check to custom reviver
alonsovb 9cbe8de
docs(readme): update readme to mention the BigInt parsing and precisi…
alonsovb d363dde
ci(nvm): use latest lts node version
alonsovb 0564065
refactor(client): minor style changes
alonsovb 02686b4
test(client): add some initial tests for the utility JSON parser
alonsovb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
18.17.1 | ||
21.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Parses a JSON including bigger numbers into BigInts | ||
* @param _ Key | ||
* @param value Parsed value | ||
* @param context Context with source text | ||
* @returns Parsed object with BigInts where required | ||
*/ | ||
export function parseWithBigInts(_: string, value: unknown, { source }: { source: string }) { | ||
// Ignore non-numbers | ||
if (typeof value !== 'number') return value | ||
alonsovb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// If not an integer, use the value | ||
// TODO: Check if Presto can return floats that could also lose precision | ||
if (!Number.isInteger(value)) return value | ||
|
||
// If number is a safe integer, we can use it | ||
if (Number.isSafeInteger(value)) return value | ||
|
||
return BigInt(source) | ||
} | ||
|
||
/** | ||
* Checks if JSON.parse reviver callback has a context parameter | ||
* See also: | ||
* - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#browser_compatibility | ||
* - https://github.com/tc39/proposal-json-parse-with-source | ||
* | ||
* This implementation is based on suggestion here: | ||
* - https://github.com/tc39/proposal-json-parse-with-source/issues/40 | ||
*/ | ||
export function isJsonParseContextAvailable() { | ||
let contextAvailable | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
JSON.parse('"x"', function (key, value, x) { | ||
contextAvailable = typeof x !== 'undefined' | ||
}) | ||
return contextAvailable | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.