-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
39 lines (35 loc) · 1.29 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
// 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
}