forked from tsivinsky/hi-mom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (26 loc) · 823 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
import intl from "./intl.js";
const cache = {};
export function hiMom(motherName, motherLang = "en") {
if (typeof intl[motherLang] !== "object") {
throw new Error("Language not yet supported, but hi mom anyway!");
}
const { name, defaultMessage, message } = intl[motherLang];
if (!motherName) {
return defaultMessage;
}
const key = `${message}-${name}`;
if (cache[key]) {
return cache[key];
}
const result = message.replace("{}", motherName || name);
cache[key] = result;
return result;
}
export function hiMoms(mothers) {
if (typeof mothers == "string") {
return hiMom(mothers);
} else if (mothers instanceof Array) {
return mothers.map(mother => hiMom(mother?.name, mother?.lang)).join(" ");
}
throw new Error("Mother type not supported, but hi mom anyway!");
}