json-bigint icon indicating copy to clipboard operation
json-bigint copied to clipboard

`BigInt` wrapper used with decimals

Open nicholaschiang opened this issue 3 years ago • 3 comments
trafficstars

When the useNativeBigInt option is enabled, this package attempts to wrap decimals (e.g. 0.990011990070343) in the native BigInt wrapper which fails (as they're obviously not integers). Instead, json-bigint should be smart enough to realize that a long floating point number is not an integer and thus should not be wrapped at all.

nicholaschiang avatar Apr 07 '22 04:04 nicholaschiang

Here's the error I'm encountering:

[dev:remix] SyntaxError: Cannot convert 0.990011990070343 to a BigInt
[dev:remix]     at BigInt (<anonymous>)
[dev:remix]     at number (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:215:15)
[dev:remix]     at value (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:397:41)
[dev:remix]     at object (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:367:27)
[dev:remix]     at value (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:389:16)
[dev:remix]     at array (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:314:22)
[dev:remix]     at value (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:391:16)
[dev:remix]     at /home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:410:14
[dev:remix]     at Array.parseJson (/home/nchiang/repos/tweetscape/app/db.server.ts:53:16)
[dev:remix]     at Result.parseRow (/home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/result.js:68:38)
[dev:remix]     at Query.handleDataRow (/home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/query.js:87:26)
[dev:remix]     at Client._handleDataRow (/home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/client.js:345:22)
[dev:remix]     at Connection.emit (node:events:520:28)
[dev:remix]     at /home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/connection.js:114:12
[dev:remix]     at Parser.parse (/home/nchiang/repos/tweetscape/node_modules/pg-protocol/src/parser.ts:104:9)
[dev:remix]     at Socket.<anonymous> (/home/nchiang/repos/tweetscape/node_modules/pg-protocol/src/index.ts:7:48)

nicholaschiang avatar Apr 07 '22 04:04 nicholaschiang

can you provide simple repro example?

sidorares avatar Apr 07 '22 05:04 sidorares

@sidorares I suspect this is actually already fixed in master:

          // Number with fractional part should be treated as number(double) including big integers in scientific notation, i.e 1.79e+308
          return _options.storeAsString
            ? string
            : /[\.eE]/.test(string)
            ? number
            : _options.useNativeBigInt
            ? BigInt(string)
            : new BigNumber(string);

It just hasn't been released to NPM yet (see #74) and thus I'm seeing this bug in production.

nicholaschiang avatar Apr 07 '22 05:04 nicholaschiang