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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bigint values #61

Open
ZeRego opened this issue Feb 11, 2025 · 1 comment · May be fixed by #62
Open

Support bigint values #61

ZeRego opened this issue Feb 11, 2025 · 1 comment · May be fixed by #62
Assignees

Comments

@ZeRego
Copy link

ZeRego commented Feb 11, 2025

At the moment, the format method throws "TypeError: Cannot mix BigInt and other types, use explicit conversions" when the value is a bigint.

More about the error: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_convert_BigInt_to_number

I was able to work around this by sanitizing the value:

    let sanitizedValue = value;

    if (typeof value === 'bigint') {
        if (value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER) {
            sanitizedValue = Number(value);
        } else {
            throw new Error('Can\'t format value as BigInt is out of safe integer range');
        }
    }

    format(expression, sanitizedValue)

Would be great if the library handled big int values.

@borgar
Copy link
Owner

borgar commented Feb 19, 2025

Yes, at the absolute minimum we should not be crashing when passed a bigint. 😅

It should be easy enough to stop crashing: Convert anything that can be into Number and proceed as normally and anything outside of that should be either emitted as "######" or as String(bigint) (maybe dependent on a setting). This would be very similar to how dates are handled.

I can't help wondering how difficult it would be to have full support bigint throughout the library. 🤔

@borgar borgar self-assigned this Feb 19, 2025
borgar added a commit that referenced this issue Feb 26, 2025
We should not crash when passed a bigint. This patch will make it so that
the formatter will convert the bigint to a number if possible, else it will
emit an overflow string (#####). A setting has been added so that instead
of the overflow string, a string version of the bigint is emitted instead.
This is similar how date overflows are handled.

I have made an attempt at supporting bigint throughout the library with
some success, but I have abandoned that because it was complicating the
code a lot and I had some worries about performance.

I think partial support would be possible with low cost. But I don't
have the resources to spare for that right now.

Closes #61
@borgar borgar linked a pull request Feb 26, 2025 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants