-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
41 lines (30 loc) · 910 Bytes
/
index.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
import normalizeUrl from 'normalize-url';
export function parse(string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
string = string.trim();
const name = /^([^<(]+)/.exec(string);
const email = /<([^>]+)>/.exec(string);
const url = /\(([^)]+)\)/.exec(string);
const returnValue = {};
if (name && name[1].trim()) {
returnValue.name = name && name[1].trim();
}
if (email && email[1].trim()) {
returnValue.email = email && email[1].trim();
}
if (url && url[1].trim()) {
returnValue.url = normalizeUrl(url && url[1].trim());
}
return returnValue;
}
export function stringify(object) {
if (typeof object !== 'object') {
throw new TypeError('Expected an object');
}
const {name} = object;
const email = object.email && `<${object.email}>`;
const url = object.url && `(${object.url})`;
return [name, email, url].filter(Boolean).join(' ');
}