Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSBI with bigints #52

Closed
s-cork opened this issue Nov 29, 2020 · 1 comment
Closed

JSBI with bigints #52

s-cork opened this issue Nov 29, 2020 · 1 comment

Comments

@s-cork
Copy link
Contributor

s-cork commented Nov 29, 2020

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.

export const JSBI = 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.pow
        multiply: (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 TypeError
        lessThan: (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,
    });
}
@jakobkummerow
Copy link
Collaborator

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!)

@s-cork s-cork closed this as completed Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants