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
Posting here since it might be useful to someone else, it's something I've used in a project that seems to be working well.
(someone will probably tell me why this is a bad idea 馃槄 )
It has the benefit of being able to use bigint when it's available and JSBI when it's not.
exportconstJSBI=window.BigInt!==undefined ? {} : require("jsbi");if(window.BigInt!==undefined){Object.assign(JSBI,{BigInt: window.BigInt,toNumber: (x)=>Number(x),toString: (x)=>x.toString(),unaryMinus: (x)=>-x,bitwiseNot: (x)=>~x,bitwiseAnd: (x,y)=>x&y,bitwiseOr: (x,y)=>x|y,bitwiseXor: (x,y)=>x^y,exponentiate: (x,y)=>x**y,// you may need to adjust this (like i did) if your transpiler changes it to Number.powmultiply: (x,y)=>x*y,divide: (x,y)=>x/y,remainder: (x,y)=>x%y,add: (x,y)=>x+y,subtract: (x,y)=>x-y,leftShift: (x,y)=>x<<y,signedRightShift: (x,y)=>x>>y,unsignedRightShift: (x,y)=>x>>>y,// will raise TypeErrorlessThan: (x,y)=>x<y,lessThanOrEqual: (x,y)=>x<=y,greaterThan: (x,y)=>x>y,greaterThanOrEqual: (x,y)=>x>=y,equal: (x,y)=>x===y,notEqual: (x,y)=>x!==y,});}
The text was updated successfully, but these errors were encountered:
It's not a bad idea; very early versions of JSBI did something like this internally.
The biggest issue is the ** operator. As you note, transpilers might eliminate it; that's because without transpilation this code will generate SyntaxErrors on sufficiently old JS engines (IIRC: Node 6, IE11).
So if you don't care about supporting such very old engines, then this solution will probably work just fine for you. Otherwise, JSBI's official conclusion is that using https://github.com/GoogleChromeLabs/babel-plugin-transform-jsbi-to-bigint is more robust, and probably fits many projects' workflows well.
(Aside from that, the good news is that nowadays all major browsers support BigInts (see: https://caniuse.com/bigint), so it's only a question of time until developers can just rely on native BigInts being available. Just a few more years!)
Posting here since it might be useful to someone else, it's something I've used in a project that seems to be working well.
(someone will probably tell me why this is a bad idea 馃槄 )
It has the benefit of being able to use
bigint
when it's available andJSBI
when it's not.The text was updated successfully, but these errors were encountered: