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

Add BigInt support #58

Closed

Conversation

prodrigestivill
Copy link

@prodrigestivill prodrigestivill commented Dec 3, 2018

As a proposal it would be helpfull to have calls to handle bigint in case of handling Big Integer Ids from a DB.

I implemented initially using npm big-integer library.

I also implemented as a complementary library on my repository hashids-bigint.

This library is not yet published to npm, because I believe it would be better to have support in the same library.

Let me know what do you think, I undestand that adding an optional dependency might not be desirable.

Possible solutions:

  • It can be implemented to support javascript native bigint but this might be too new.
  • It can be implemented as required dependency.

@coveralls
Copy link

coveralls commented Dec 3, 2018

Coverage Status

Coverage decreased (-6.5%) to 93.176% when pulling 7865be9 on prodrigestivill:bi-pull-request into d637de9 on ivanakimov:master.

@j-f1
Copy link

j-f1 commented Dec 3, 2018

Does this work with native BigInts?

@prodrigestivill
Copy link
Author

True, I didn't test it earlier.
It appears that no negative numbers are supported by the library yet.

Not sure how dificult it would be to add support for negative numbers for numbers, hex and bigints.

@prodrigestivill
Copy link
Author

Tested with native BigInts and working. But still relaying to the external library in order to perform hex conversion.

@dystopiandev
Copy link

Seeing as its over a month now, you should publish this to NPM. Override the encode and decode functions so one can easily swap from hashids to hashids-bigint without refactoring.

You can always deprecate the hashids-bigint package if the original hashids package decides to support arbitrary string numbers like BigInt & BigNumber.

@prodrigestivill
Copy link
Author

Override the encode and decode functions so one can easily swap from hashids to hashids-bigint without refactoring.

It seams a good idea, but it will need more work, currently I was using the BigInt to Hex and encodeHex function, but aparently the @ivanakimov implementation of encodeHex in hashids is not compatible with encode for regular numbers, so currently my patch and fork does the following:

encodeBI(1n) = encodeHex('01')
encodeHex('01') != encode(1)
encodeBI(1n) != encode(1)

I didn't expect this behavior, I will try to take a look and do at least encodeBI compatible with encode if the plan is to do a library backcompatible with the same calls.

So, it might take change all hashids calls to use BigInt modulus at least.
In this case it will end as a full refactor of the library. I will code it when I have time.

@dystopiandev
Copy link

I've deviced a workaround with hex using bignumber.js and the original hashids.js.

Encode:
hashids.encodeHex(new BigNumber(NUMBER_STRING).toString(16))

Decode:
new BigNumber(`0x${hashids.decodeHex(HASHED_STRING)}`).toString()

@jd327
Copy link
Collaborator

jd327 commented Jan 28, 2019

Thanks for experimenting with this, everyone.

There have been some requests in the past for supporting bigger numbers, but I haven't noticed many lately. It's true, we've tried to avoid depending on other modules to keep the codebase small and less complex, so the advice has usually been to split up the numbers yourself (if they're too big) before encoding. But I agree that having built-in support would of course be ideal.

If there's a way to use JavaScript's BigInt internally wherever it's available, I'd support this implementation. As far as I'm aware, some other Hashids implementations use a similar approach. I'd be cautious about relying on external dependencies, but am open to discussion if there's demand for it.

Otherwise, feel free to fork or build on top of Hashids, and I'll link to your repo on the website.

@Bessonov
Copy link

@ivanakimov interested in it for node, which supports BigInt after v10.4.0. But becase some browsers doesn't support it, may be you can offer an api to hook into and provide a custom polyfill or native implementation. Then you don't have any dependency on BigInt nor a specific implementation.

@niieani
Copy link
Owner

niieani commented Jul 24, 2019

@Bessonov I'll look into it. I'm curious - what's your use case?

@Bessonov
Copy link

Bessonov commented Jul 24, 2019

I want to switch to CockroachDB, which offers some compatibility with Postgresql and stores primary key as 64-bit integer. Currently, the pg driver returns it as a string, but it can be converted to BigInt.

@niieani
Copy link
Owner

niieani commented Aug 15, 2019

Thanks a lot @prodrigestivill for your proposal. I have decided it would be best if the BigInt support were transparent (rather than using a custom function for it).

A beta version with BigInt support (using standard APIs) is released as hashids@next (PR #65). See the updated README for details.

Looking forward to your feedback!

@niieani niieani closed this in #65 Aug 31, 2019
@niieani
Copy link
Owner

niieani commented Aug 31, 2019

🎉 This issue has been resolved in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants