-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplate.js
30 lines (28 loc) · 972 Bytes
/
template.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
import {DateTime} from "./third_party/luxon.min.js";
export function populateTemplate(template, obj) {
return template.replace(/\{\{\s*(\w+)(:[^}{\n]+)?\s*\}\}/g, function(match, property, format, offset, string) {
if (format !== undefined) {
// Trim spaces and the ':' at the beginning of format.
format = format.trim().slice(1);
}
if (obj.hasOwnProperty(property)) {
let value = obj[property];
if (value == null) {
value = "";
}
return value;
} else if (property === "date") {
if (!format) {
format = "yyyy-MM-dd";
}
return DateTime.now().toFormat(format);
} else if (property === "time") {
if (!format) {
format = "HH:mm";
}
return DateTime.now().toFormat(format);
} else {
return property;
}
});
}