Created
October 26, 2022 04:25
-
-
Save azer/cb1b25a8ca61fedce0806c1425da4c20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HashID do | |
@coder Hashids.new( | |
salt: "HmPbtapoe1FGfTFbEEeZcWKuakIQp3L0", | |
min_en: 6 | |
) | |
def encode(id, type) do | |
type = | |
type | |
|> to_string | |
|> :erlang.binary_to_list() | |
|> Kernel.++([id]) | |
|> Enum.reverse() | |
Hashids.encode(@coder, type) | |
end | |
def decode(data) do | |
Hashids.decode(@coder, data) | |
|> case do | |
{:ok, [head | tail]} -> | |
type = | |
tail | |
|> Enum.reverse() | |
|> :erlang.list_to_binary() | |
|> String.to_atom() | |
{head, type} | |
_ -> | |
:error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment