UNPKG

1.94 kBPlain TextView Raw
1import Hashids from '../lib/hashids'
2
3describe('custom alphabet', () => {
4 const testAlphabet = (alphabet: string) => {
5 const hashids = new Hashids('', 0, alphabet)
6 const numbers = [1, 2, 3]
7
8 const id = hashids.encode(numbers)
9 const decodedNumbers = hashids.decode(id)
10
11 expect(decodedNumbers).toEqual(numbers)
12 }
13
14 test(`should work with the worst alphabet`, () => {
15 testAlphabet('cCsSfFhHuUiItT01')
16 })
17
18 test(`should work with an alphabet containing spaces`, () => {
19 testAlphabet('cCsSfFhH uUiItT01')
20 })
21
22 test(`should work with half the alphabet being separators`, () => {
23 testAlphabet('abdegjklCFHISTUc')
24 })
25
26 test(`should work with exactly 2 separators`, () => {
27 testAlphabet('abdegjklmnopqrSF')
28 })
29
30 test(`should work with no separators`, () => {
31 testAlphabet('abdegjklmnopqrvwxyzABDEGJKLMNOPQRVWXYZ1234567890')
32 })
33
34 test(`should work with super long alphabet`, () => {
35 testAlphabet(
36 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_=+\\|\'";:/?.>,<{[}]',
37 )
38 })
39
40 test(`should work with a weird alphabet`, () => {
41 testAlphabet('`~!@#$%^&*()-_=+\\|\'";:/?.>,<{[}]')
42 })
43
44 test(`should work with an alphabet with unicode chars`, () => {
45 testAlphabet('πŸ˜€πŸ˜πŸ˜‚πŸ€£πŸ˜ƒπŸ˜„πŸ˜…πŸ˜†πŸ˜‰πŸ˜ŠπŸ˜‹πŸ˜ŽπŸ˜πŸ˜˜πŸ₯°πŸ˜—πŸ˜™πŸ˜š')
46 })
47
48 test(`should work with an alphabet with complex unicode chars`, () => {
49 testAlphabet('πŸ€ΊπŸ‘©πŸΏβ€πŸ¦³πŸ›πŸ‘©πŸ»πŸ¦·πŸ€¦β€β™‚οΈπŸβ˜πŸΌβœπŸΎπŸ‘‰πŸ½πŸ‡ΈπŸ‡°β€οΈπŸ­')
50 })
51
52 test(`should work with alphabet that contains emojis that are subsets of each other`, () => {
53 testAlphabet('πŸ˜πŸ§‘πŸ½β€πŸ¦³πŸ§‘πŸ·πŸ‘©πŸΏβ€πŸ¦°πŸ‘©πŸΎβ€πŸ¦°πŸ‘©πŸ½β€πŸ¦°πŸ‘©πŸ»β€πŸ¦°βœπŸΎπŸ‘‰πŸ½πŸ‘©πŸ»πŸ¦·πŸ€¦β€β™‚οΈ')
54 testAlphabet('πŸ˜πŸ§‘πŸ§‘πŸ½β€πŸ¦³πŸ·πŸ‘©πŸ»β€πŸ¦°πŸ‘©πŸΏβ€πŸ¦°πŸ‘©πŸ½β€πŸ¦°πŸ‘©πŸΎβ€πŸ¦°βœπŸΎπŸ‘‰πŸ½πŸ‘©πŸ»πŸ¦·πŸ€¦β€β™‚οΈ')
55 })
56})