This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gravatar.js
93 lines (86 loc) · 2.26 KB
/
gravatar.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { md5 } from './hash.js';
import { createImage } from './elements.js';
import { setURLParams } from './utility.js';
import { REFERRER_POLICY } from './defaults.js';
const GRAVATAR = 'https://secure.gravatar.com/avatar/';
const SIZE = 80;
const FALLBACK = 'mm';
const FORCE = false;
const RATING = undefined;
export async function gravatarURL(email, {
size: s = SIZE,
fallback: d = FALLBACK,
force: f = FORCE,
rating: r = RATING,
} = {}) {
const hash = await md5(email);
return setURLParams(new URL(hash, GRAVATAR), { s, d, f: f ? 'y': undefined, r });
}
export function changeGravatarSize(url, size) {
const cpy = new URL(url, GRAVATAR);
cpy.searchParams.set('s', size);
return cpy;
}
export function gravatarSrcset(url, ...sizes) {
return sizes.map(size => changeGravatarSize(url, size));
}
/**
* @TODO figure out responsive images
*/
export async function createGravatarImage(email, {
size = SIZE,
fallback = FALLBACK,
force = FORCE,
rating = RATING,
alt = '',
id = null,
crossOrigin = 'anonymous',
'@type': type,
'@context': context,
itemprop = null,
itemtype = null,
itemscope = undefined,
// sizes = null,
// srcset = null,
referrerPolicy = REFERRER_POLICY,
fetchPriority = 'auto',
loading = 'eager',
decoding = 'async',
role = null,
slot = null,
part = [],
classList = [],
dataset = null,
styles = null,
events: {
capture,
passive,
once,
signal,
...events
} = {},
...attrs
} = {}) {
const url = await gravatarURL(email, { size, fallback, force, rating });
return createImage(url, {
alt, id, crossOrigin, itemprop, itemtype, itemscope, referrerPolicy,
fetchPriority, loading, decoding, role, slot, part, classList, dataset,
'@type': type, '@context': context, height: size, width: size,
styles, events: { capture, passive, once, signal, ...events }, ...attrs
});
}
export function setGravatarSrcset(img, ...sizes) {
if (! (img instanceof HTMLImageElement)) {
throw new TypeError('`setGravatarSrcset` requires an `<img>`');
} else if (sizes.length === 0) {
throw new Error('No sizes given');
} else {
const src = new URL(img.src, GRAVATAR);
const updateSize = size => {
src.searchParams.set('s', size);
return src.href;
};
img.srcset = sizes.map(size => `${updateSize(size)} ${size}w`).join(' ');
return img;
}
}