Created
February 26, 2019 20:32
-
-
Save wouterds/3b27b9d09605e0bbbc512bbefbc56487 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
import Hashids from 'hashids'; | |
const hashids = new Hashids( | |
process.env.HASHIDS_SALT, | |
4, | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', | |
); | |
export const encode = (id: number): string => hashids.encode(id); | |
export const decode = (id: string): number | null => { | |
const decodedIds = hashids.decode(id); | |
if (decodedIds.length === 0) { | |
return null; | |
} | |
return decodedIds[0]; | |
}; | |
export default { | |
encode, | |
decode, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment