custom-params-hex.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import Hashids from '../lib/hashids';
  2. import { assert } from 'chai';
  3. const minLength = 30;
  4. const hashids = new Hashids('this is my salt', minLength, 'xzal86grmb4jhysfoqp3we7291kuct5iv0nd');
  5. const map = {
  6. '0dbq3jwa8p4b3gk6gb8bv21goerm96': 'deadbeef',
  7. '190obdnk4j02pajjdande7aqj628mr': 'abcdef123456',
  8. 'a1nvl5d9m3yo8pj1fqag8p9pqw4dyl': 'ABCDDD6666DDEEEEEEEEE',
  9. '1nvlml93k3066oas3l9lr1wn1k67dy': '507f1f77bcf86cd799439011',
  10. 'mgyband33ye3c6jj16yq1jayh6krqjbo': 'f00000fddddddeeeee4444444ababab',
  11. '9mnwgllqg1q2tdo63yya35a9ukgl6bbn6qn8': 'abcdef123456abcdef123456abcdef123456',
  12. 'edjrkn9m6o69s0ewnq5lqanqsmk6loayorlohwd963r53e63xmml29': 'f000000000000000000000000000000000000000000000000000f',
  13. 'grekpy53r2pjxwyjkl9aw0k3t5la1b8d5r1ex9bgeqmy93eata0eq0': 'fffffffffffffffffffffffffffffffffffffffffffffffffffff'
  14. };
  15. describe('encodeHex/decodeHex using default params', () => {
  16. for (const id in map) {
  17. const hex = map[id];
  18. it(`should encode '0x${hex.toUpperCase()}' to '${id}'`, () => {
  19. assert.equal(id, hashids.encodeHex(hex));
  20. });
  21. it(`should encode '0x${hex.toUpperCase()}' to '${id}' and decode back correctly`, () => {
  22. const encodedId = hashids.encodeHex(hex);
  23. const decodedHex = hashids.decodeHex(encodedId);
  24. assert.deepEqual(hex.toLowerCase(), decodedHex);
  25. });
  26. it(`id length should be at least ${minLength}`, () => {
  27. assert.isAtLeast(hashids.encodeHex(hex).length, minLength);
  28. });
  29. }
  30. });