-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
67 lines (56 loc) · 1.87 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Hash } from "https://deno.land/x/checksum/mod.ts";
/**
* @description allowed options list
* @see https://en.gravatar.com/site/implement/images/
*/
const gravatarOptionsList = ['s', 'size', 'd', 'default', 'f', 'forcedefault', 'r', 'rating'];
/**
*
* @param email string The email you want to hash
* @returns hashed email by md5 method
*/
function emailHash(email: string) {
const emailHash: string = new Hash("md5").digestString(email).hex()
return emailHash;
}
/**
*
* @param email string
* @param options object gravatar options like default image an size ..
* @see https://en.gravatar.com/site/implement/images/
* @returns Gravatar full url
*/
export function getGravatarUrl(email:string, options?:any) {
// the Gravatar Url without any option
let gravatarUrl:string = "https://www.gravatar.com/avatar/" + emailHash(email)
// the Gravatar option adding
if (options) {
let optionsParams:string = Object.keys(options).map(key => {
// Filter the options from not allowed ones
if(!gravatarOptionsList.includes(key)){
return;
}
// Convert the options to url parameters
if (key === 'default') {
return key + '=' + encodeURI(options[key])
}else{
return key + '=' + options[key]
}
}).join('&');
// Add gravatar option as parameters to url if there is
gravatarUrl += (optionsParams) ? '?' + optionsParams : ''
}
// full gravatar url
return gravatarUrl;
}
/**
*
* @param email string
* @returns string the full Gravatar profile url
*/
export function getGravatarProfileUrl(email:string) {
// the Gravatar Profile Url
const gravatarProfileUrl:string = "https://www.gravatar.com/" + emailHash(email)
// return the Gravatar Profile Url
return gravatarProfileUrl;
}