UNPKG

1.88 kBPlain TextView Raw
1import Hashids from '../lib/hashids'
2
3const minLength = 30
4const hashids = new Hashids(
5 'this is my salt',
6 minLength,
7 'xzal86grmb4jhysfoqp3we7291kuct5iv0nd',
8)
9
10describe.each([
11 ['0dbq3jwa8p4b3gk6gb8bv21goerm96', 'deadbeef'],
12 ['190obdnk4j02pajjdande7aqj628mr', 'abcdef123456'],
13 ['a1nvl5d9m3yo8pj1fqag8p9pqw4dyl', 'ABCDDD6666DDEEEEEEEEE'],
14 ['1nvlml93k3066oas3l9lr1wn1k67dy', '507f1f77bcf86cd799439011'],
15 ['mgyband33ye3c6jj16yq1jayh6krqjbo', 'f00000fddddddeeeee4444444ababab'],
16 [
17 '9mnwgllqg1q2tdo63yya35a9ukgl6bbn6qn8',
18 'abcdef123456abcdef123456abcdef123456',
19 ],
20 [
21 'edjrkn9m6o69s0ewnq5lqanqsmk6loayorlohwd963r53e63xmml29',
22 'f000000000000000000000000000000000000000000000000000f',
23 ],
24 [
25 'grekpy53r2pjxwyjkl9aw0k3t5la1b8d5r1ex9bgeqmy93eata0eq0',
26 'fffffffffffffffffffffffffffffffffffffffffffffffffffff',
27 ],
28 // bigint format:
29 ...(typeof BigInt === 'function'
30 ? require('./bigint-test-cases').customParamsHex
31 : []),
32] as [string, string | bigint][])(
33 'encodeHex/decodeHex using default params',
34 (id, hex) => {
35 test(`should encode ${typeof hex} '0x${hex
36 .toString(16)
37 .toUpperCase()}' to '${id}'`, () => {
38 expect(hashids.encodeHex(hex)).toEqual(id)
39 })
40
41 test(`should decode '${id}' to '0x${hex
42 .toString(16)
43 .toUpperCase()}'`, () => {
44 expect(hashids.decodeHex(id)).toEqual(hex.toString(16).toLowerCase())
45 })
46
47 test(`should encode ${typeof hex} '0x${hex
48 .toString(16)
49 .toUpperCase()}' to '${id}' and decode back correctly`, () => {
50 const encodedId = hashids.encodeHex(hex)
51 const decodedHex = hashids.decodeHex(encodedId)
52
53 expect(decodedHex).toBe(hex.toString(16).toLowerCase())
54 })
55
56 test(`id length should be at least ${minLength}`, () => {
57 expect(hashids.encodeHex(hex).length).toBeGreaterThanOrEqual(minLength)
58 })
59 },
60)