Created
March 16, 2021 11:29
-
-
Save theredhead/ba493d662af833ef015b65c64740bc65 to your computer and use it in GitHub Desktop.
Gravatar url in typescript
This file contains hidden or 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
// 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