Skip to content

Instantly share code, notes, and snippets.

@theredhead
Created March 16, 2021 11:29
Show Gist options
  • Save theredhead/ba493d662af833ef015b65c64740bc65 to your computer and use it in GitHub Desktop.
Save theredhead/ba493d662af833ef015b65c64740bc65 to your computer and use it in GitHub Desktop.
Gravatar url in typescript
// npm install md5
import * as md5 from 'md5';
export const enum GravatarType {
MysteryPerson,
Identicon,
MonsterId,
Wavatar,
Retro,
Robohash,
Blank
}
export const enum GravatarRating {
G,
PG,
R,
X
}
const knownGravatarRatings = {
[GravatarRating.G]: 'g',
[GravatarRating.PG]: 'pg',
[GravatarRating.R]: 'r',
[GravatarRating.X]: 'x',
};
const knownGravatarTypes = {
[GravatarType.MysteryPerson]: 'mp',
[GravatarType.Identicon]: 'identicon',
[GravatarType.MonsterId]: 'monsterid',
[GravatarType.Wavatar]: 'wavatar',
[GravatarType.Retro]: 'retro',
[GravatarType.Robohash]: 'robohash',
[GravatarType.Blank]: 'blank',
};
export const gravatarUrl = (email: string, size = 64, rating = GravatarRating.PG, type = GravatarType.Identicon): string => {
const hash = md5(email);
return `https://www.gravatar.com/avatar/${hash}?s=${size}&r=${knownGravatarRatings[rating]}&d=${knownGravatarTypes[type]}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment