@prestodb/presto-js-client
Version:
This is a Presto JavaScript client that connects to Presto via Presto's REST API to run queries.
34 lines (33 loc) • 1.31 kB
JavaScript
/**
* Parses a JSON including bigger numbers into BigInts
* This function checks if JSON.parse reviver callback has a context parameter
* and falls back onto the default parsing if not.
* 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
* @param _ Key
* @param value Parsed value
* @param context Context with source text
* @returns Parsed object with BigInts where required
*/ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "parseWithBigInts", {
enumerable: true,
get: function() {
return parseWithBigInts;
}
});
function parseWithBigInts(_, value, context) {
if (!(context == null ? void 0 : context.source)) return value // Context source is not available, fallback to default parse
;
// 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(context.source);
}