-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (66 loc) · 2.17 KB
/
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
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
const moment = require('moment');
const Mustache = require('mustache');
const Wax = require('@jvitela/mustache-wax');
/**
* This package builds on the excellent Mustache, Mustache-Wax and Moment
* packages to provide a handy set of formatters to use within our templates
*/
Wax(Mustache, {
/**
* Take a date (for example in ISO8601) and format it using Moment)
* Usage: {{projectInstallationDate | date : "DD MMMM YYYY" }}
* @param dateStr
* @param format
*/
date: function (dateStr, format) {
if (!dateStr) {
return ''; // Don't display today's date if there isn't one
}
try {
const date = moment(dateStr);
return date.format(format);
} catch (err) {
return `Unable to parse ${dateStr}`;
}
},
/**
* Convert to a number and ensure there are two decimal places
* Usage: {{total | money}}
* @param value
*/
money: function (value) {
if (!value) {
return ''; // Don't display anything
}
const result = typeof value === 'string' ?
parseFloat(value).toFixed(2) :
value.toFixed(2);
return !isNaN(result) ? result : `Unable to parse ${value}`;
},
/**
* Add any number of variables (or numbers) to an initial one
* Usage: {{additional_works_price_1 | sum : additional_works_price_2 : additional_works_price_3 :
additional_works_price_4}}
* @param args
*/
sum: function (...args) {
const paramToFloat = (param) => {
return typeof param === 'string' ? parseFloat(param) : param;
};
try {
return args.reduce((previous, current) => {
if (!current) {
return previous; // Miss out any missing parameters
}
const result = paramToFloat(previous) + paramToFloat(current);
if (isNaN(result)) {
throw `Unable to compute ${previous} + ${current}`;
}
return result;
});
} catch (err) {
return err;
}
},
});
module.exports = Mustache;