/
index.js
67 lines (49 loc) · 1.7 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
module.exports = function(app) {
var config = app.config;
app.use(function(req, res, next) {
res.locals.expose = function expose(locals) {
var keys = locals.split(' ');
var Exposed = {};
keys.forEach(function(key) {
Exposed[key] = res.locals[key];
});
return '<script>Exposed = ' + JSON.stringify(Exposed) + ';</script>';
};
res.locals.current_user = req.session && req.session.user || null;
res.locals.is_logged_in = (res.locals.current_user !== null);
return next();
});
app.locals({
inspect: function (obj, title) {
if (!title) title = '';
return '<div class="debug_output"><h3>'+title+'</h3><pre>'+require('util').inspect(obj, true, 5)+'</pre></div>';
},
embedObj: function(obj, name) {
var escaped = JSON.stringify(obj);
return "<script> " + name + " = " + escaped + "; </script>";
},
embed: function(obj, name) {
return "<script> " + name + " = \"" + obj + "\"; </script>";
},
relative_date: function(olderDate) {
if (typeof olderDate == "string") olderDate = new Date(olderDate);
newerDate = new Date();
var milliseconds = newerDate - olderDate;
var conversions = [
["years", 31518720000],
["months", 2626560000 /* assumes there are 30.4 days in a month */],
["days", 86400000],
["hours", 3600000],
["minutes", 60000],
["seconds", 1000]
];
for (var i = 0; i < conversions.length; i++) {
var result = Math.floor(milliseconds / conversions[i][1]);
if (result >= 2) {
return result + " " + conversions[i][0] + " ago";
}
}
return "1 second ago";
}
});
};