From 85e1f078e957ff1e64d6b1833170411e8d50bac1 Mon Sep 17 00:00:00 2001 From: Camillo Positano <59197708+camillo-positano@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:36:45 +0200 Subject: [PATCH] Resolved Cannot convert a BigInt value to a number (#723) * Resolved Cannot convert a BigInt value to a number Signed-off-by: Camillo Positano <59197708+camillo-positano@users.noreply.github.com> * added test for asNumber throwing exception Exception thrown was Cannot convert a BigInt value to a number. This test verifies that it can convert a big int Signed-off-by: Camillo Positano <59197708+camillo-positano@users.noreply.github.com> --------- Signed-off-by: Camillo Positano <59197708+camillo-positano@users.noreply.github.com> --- lib/serializer.js | 2 +- test/asNumber.test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/asNumber.test.js diff --git a/lib/serializer.js b/lib/serializer.js index 24917433..1a006d5f 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -41,7 +41,7 @@ module.exports = class Serializer { asNumber (i) { // fast cast to number - const num = +i + const num = Number(i) // check if number is NaN // eslint-disable-next-line no-self-compare if (num !== num) { diff --git a/test/asNumber.test.js b/test/asNumber.test.js new file mode 100644 index 00000000..5e68b922 --- /dev/null +++ b/test/asNumber.test.js @@ -0,0 +1,13 @@ +'use strict' + +const test = require('tap').test + +test('asNumber should convert BigInt', (t) => { + t.plan(1) + const Serializer = require('../lib/serializer') + const serializer = new Serializer() + + const number = serializer.asNumber(11753021440n) + + t.equal(number, '11753021440') +})