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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ var id = hashids.encodeHex('507f1f77bcf86cd799439011'); // y42LW46J9luq3Xq9XMly
var hex = hashids.decodeHex(id); // 507f1f77bcf86cd799439011
```

**Encode bigint numbers:**

Useful if you want to encode [PostgreSQL](https://www.postgresql.org/)'s bigint. Note that currently pg javascript driver handles bigint as string since it's support is a current draft.

```javascript
var hashids = new Hashids();

var id = hashids.encodeBI('50374626472636434472463271'); // ww2Vn9oypBtv1PZ2Rpp
var hex = hashids.decodeBI(id); // 50374626472636434472463271
```

Pitfalls
-------

Expand Down
25 changes: 25 additions & 0 deletions lib/hashids.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ export default class Hashids {

}

encodeBI(id) {

if (!/^[0-9]+$/.test(id)) {
return '';
}

const bi = require('big-integer')(id, 10);
return this.encodeHex(bi.toString(16));

}

decodeBI(id) {

let ret = [];

const hex = this.decodeHex(id);
if (typeof hex === 'string' && hex.length) {
const bi = require('big-integer')(hex, 16);
ret = bi.toString(10);
}

return ret;

}

_encode(numbers) {

let ret,
Expand Down
45 changes: 23 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,8 @@
"decode",
"encrypt",
"decrypt"
]
],
"optionalDependencies": {
"big-integer": "^1.6.38"
}
}
10 changes: 10 additions & 0 deletions tests/bad-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,14 @@ describe('bad input', () => {
assert.deepEqual(numbers, []);
});

it(`should return an empty string when encoding non-integer input`, () => {
const id = hashids.encodeBI('z');
assert.equal(id, '');
});

it(`should return an empty array when decoding invalid BI id`, () => {
const numbers = hashids.decodeBI('f');
assert.deepEqual(numbers, []);
});

});
44 changes: 44 additions & 0 deletions tests/custom-params-bi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

import Hashids from '../lib/hashids';
import { assert } from 'chai';

const minLength = 30;
const hashids = new Hashids('this is my salt', minLength, 'xzal86grmb4jhysfoqp3we7291kuct5iv0nd');

const map = {
'0dbq3jwa8p4b3gk6gb8bv21goerm96': '3735928559',
'190obdnk4j02pajjdande7aqj628mr': '188900967593046',
'a1nvl5d9m3yo8pj1fqag8p9pqw4dyl': '12981155274522450858012398',
'1nvlml93k3066oas3l9lr1wn1k67dy': '24912482966938930280208240657',
'mgyband33ye3c6jj16yq1jayh6krqjbo': '19938421193860583634706025513334057899',
'9mnwgllqg1q2tdo63yya35a9ukgl6bbn6qn8': '14966276559563682702364767401166784150254678',
'edjrkn9m6o69s0ewnq5lqanqsmk6loayorlohwd963r53e63xmml29': '6170642089954522658081134434590064393685259496125924487557283855',
'grekpy53r2pjxwyjkl9aw0k3t5la1b8d5r1ex9bgeqmy93eata0eq0': '6582018229284824168619876730229402019930943462534319453394436095'
};

describe('encodeBI/decodeBI using custom params', () => {

for (const id in map) {

const bi = map[id];

it(`should encode '${bi}' to '${id}'`, () => {
assert.equal(id, hashids.encodeBI(bi));
});

it(`should encode '${bi}' to '${id}' and decode back correctly`, () => {

const encodedId = hashids.encodeBI(bi);
const decodedBI = hashids.decodeBI(encodedId);

assert.deepEqual(bi, decodedBI);

});

it(`id length should be at least ${minLength}`, () => {
assert.isAtLeast(hashids.encodeBI(bi).length, minLength);
});

}

});
39 changes: 39 additions & 0 deletions tests/default-params-bi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import Hashids from '../lib/hashids';
import { assert } from 'chai';

const hashids = new Hashids();

const map = {
'wpVL4j9g': '3735928559',
'kmP69lB3xv': '188900967593046',
'47JWg0kv4VU0G2KBO2': '12981155274522450858012398',
'y42LW46J9luq3Xq9XMly': '24912482966938930280208240657',
'm1rO8xBQNquXmLvmO65BUO9KQmj': '19938421193860583634706025513334057899',
'wBlnMA23NLIQDgw7XxErc2mlNyAjpw': '14966276559563682702364767401166784150254678',
'VwLAoD9BqlT7xn4ZnBXJFmGZ51ZqrBhqrymEyvYLIP199': '6170642089954522658081134434590064393685259496125924487557283855',
'nBrz1rYyV0C0XKNXxB54fWN0yNvVjlip7127Jo3ri0Pqw': '6582018229284824168619876730229402019930943462534319453394436095'
};

describe('encodeBI/decodeBI using default params', () => {

for (const id in map) {

const bi = map[id];

it(`should encode '${bi}' to '${id}'`, () => {
assert.equal(id, hashids.encodeBI(bi));
});

it(`should encode '${bi}' to '${id}' and decode back correctly`, () => {

const encodedId = hashids.encodeBI(bi);
const decodedBI = hashids.decodeBI(encodedId);

assert.deepEqual(bi, decodedBI);

});

}

});