Skip to content

Instantly share code, notes, and snippets.

@wouterds
Created February 26, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterds/3b27b9d09605e0bbbc512bbefbc56487 to your computer and use it in GitHub Desktop.
Save wouterds/3b27b9d09605e0bbbc512bbefbc56487 to your computer and use it in GitHub Desktop.
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