You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Structural Cloning a BigNumber instance removes all prototype methods and leaves a plain object, e. g.
const plainObj = { s: 1, e: -2, c: [ 50 ] }
I thought that it would be possible to rehydrate/restore a BigNumber obj like const bn = new BigNumber(plainObj), however the resulting value is alway NaN.
Why is this? I thought the object would contain all "state" that is needed to recreate a BigNumber. Is there a way to achieve what I would like to do without converting the value to a string / number first?
EDIT
The cause of this issue is that
(a) BigNumber.js accepts an object literal, but you need to provide the property _isBigNumber: true to this literal
(b) Structured cloning removes _isBigNumber: true from the object
importBigNumberfrom'bignumber.js';import{deserialize,serialize}from'v8';constobj={c: [50],e: 1,s: 1,_isBigNumber: true};constrehydrated=newBigNumber(obj);// workingconstclone=deserialize(serialize(rehydrated));// structure cloning node.jsconstrehydratedClone=newBigNumber(clone);// not working, since clone is missing _isBigNumber: true
Could we skip the _isBigNumber: true check in the constructor? If someone provides an object that adheres to BigNumber's data structure, couldn't we safely assume that this is a BigNumber? This would add the benefit, that BigNumber objects would be serializable via StructuredClone as well as JSON.
Changing this to if (v && typeof v.c !== "undefined" && typeof v.s !== "undefined" && typeof v.e !== "undefined") { would solve the issue, while all tests still pass. So, basically an obj { s: 1, e: -2, c: [ 50 ] } is not a BigNumber, hence BigNumber.isBigNumber(obj) is false, but you can construct a BigNumber from it: new BigNumber(obj).
Then it only becomes a habit of coding to re-encode your BigNumber values before you use them, e. g.
Structural Cloning a BigNumber instance removes all prototype methods and leaves a plain object, e. g.
const plainObj = { s: 1, e: -2, c: [ 50 ] }
I thought that it would be possible to rehydrate/restore a BigNumber obj like
const bn = new BigNumber(plainObj)
, however the resulting value is alway NaN.TDLR
Why is this? I thought the object would contain all "state" that is needed to recreate a BigNumber. Is there a way to achieve what I would like to do without converting the value to a string / number first?
EDIT
The cause of this issue is that
(a) BigNumber.js accepts an object literal, but you need to provide the property
_isBigNumber: true
to this literal(b) Structured cloning removes
_isBigNumber: true
from the objectCould we skip the
_isBigNumber: true
check in the constructor? If someone provides an object that adheres to BigNumber's data structure, couldn't we safely assume that this is a BigNumber? This would add the benefit, that BigNumber objects would be serializable via StructuredClone as well as JSON.Possible solution
bignumber.js/bignumber.js
Line 191 in 690d996
Changing this to
if (v && typeof v.c !== "undefined" && typeof v.s !== "undefined" && typeof v.e !== "undefined") {
would solve the issue, while all tests still pass. So, basically an obj{ s: 1, e: -2, c: [ 50 ] }
is not a BigNumber, hence BigNumber.isBigNumber(obj) is false, but you can construct a BigNumber from it: new BigNumber(obj).Then it only becomes a habit of coding to re-encode your BigNumber values before you use them, e. g.
The text was updated successfully, but these errors were encountered: