24
24
25
25
## Usage
26
26
27
- Hashids encodes a list of integers into a char list . Some of the encoding
27
+ Hashids encodes a list of integers into a string (technically, iodata) . Some of the encoding
28
28
parameters can be customized.
29
29
30
30
``` elixir
@@ -49,15 +49,26 @@ Hashids.decode!(s, cipher2)
49
49
```
50
50
51
51
It is also possible to customize the character set used for the cipher text by
52
- providing an alphabet as a char list . It has to be at least 16 characters long.
52
+ providing a custom alphabet . It has to be at least 16 characters long.
53
53
54
54
``` elixir
55
- s = Hashids .new (alphabet: " 1234567890абвгдежизклмн" )
55
+ defmodule MyAccessToken do
56
+ @cyrillic_alphabet " 123456789абвгґдеєжзиіїйклмнопрстуфцчшщьюяАБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФЦЧШЩЬЮЯ"
57
+ @coder Hashids .new (alphabet: @cyrillic_alphabet )
56
58
57
- cipher = Hashids .encode (s, [1234 , 786 , 21 , 0 ])
58
- # => "имнк40же3ги1з"
59
+ def encode (token_ids) do
60
+ Hashids .encode (@coder , token_ids)
61
+ end
59
62
60
- Hashids .decode (s, cipher)
63
+ def decode (data) do
64
+ Hashids .decode (@coder , data)
65
+ end
66
+ end
67
+
68
+ data = MyAccessToken .encode ([1234 , 786 , 21 , 0 ])
69
+ # => "ЦфюєИНаЛ1И"
70
+
71
+ MyAccessToken .decode (data)
61
72
# => {:ok, [1234, 786, 21, 0]}
62
73
```
63
74
0 commit comments