-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
62 lines (51 loc) · 1.79 KB
/
helpers.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
/*
This is a file of data and helper functions that we can expose and use in our templating function
*/
// FS is a built in module to node that let's us read files from the system we're running on
const fs = require('fs');
const currency = require('currency.js');
// moment.js is a handy library for displaying dates. We need this in our templates to display things like "Posted 5 minutes ago"
exports.moment = require('moment');
// Dump is a handy debugging function we can use to sort of "console.log" our data
exports.dump = (obj) => JSON.stringify(obj, null, 2);
// Making a static map is really long - this is a handy helper function to make one
// inserting an SVG
exports.icon = (name) => {
try {
return fs.readFileSync(`./public/images/icons/${name}.svg`);
} catch (error) {
return null;
}
};
exports.image = (name) => fs.readFileSync(`./public/images/photos/${name}.jpg`);
// Some details about the site
exports.siteName = `Express.js / MongoBD / Rest Api`;
exports.timeRange = (start, end, format, interval) => {
if (format == undefined) {
format = 'HH:mm';
}
if (interval == undefined) {
interval = 60;
}
interval = interval > 0 ? interval : 60;
const range = [];
while (moment(start).isBefore(moment(end))) {
range.push(moment(start).format(format));
start = moment(start).add(interval, 'minutes');
}
return range;
};
exports.calculate = {
add: (firstValue, secondValue) => {
return currency(firstValue).add(secondValue).value;
},
sub: (firstValue, secondValue) => {
return currency(firstValue).subtract(secondValue).value;
},
multiply: (firstValue, secondValue) => {
return currency(firstValue).multiply(secondValue).value;
},
divide: (firstValue, secondValue) => {
return currency(firstValue).divide(secondValue).value;
},
};