1 | import Hashids from '../lib/hashids'
|
2 |
|
3 | describe('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 | })
|