-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
47 lines (40 loc) · 1.14 KB
/
utils.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
var Promise = require('bluebird');
var _ = require('lodash');
var utils = {};
utils.generateUUID = (function() {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return function() {
return s4() + s4() + s4() + s4();
};
})();
utils.handleResult = function handleResult(result, res, next) {
if (result) {
try {
res.send(result);
}
catch (error) {
log.error('Error catched on handleResult');
next(error);
}
}
else {
res.sendStatus(404);
}
};
utils.setEmptyStringsNull = function(object) {
// Set empty strings to null
// (since frontend might pass some of the integer fields as string, this allows unsetting those)
// fields - otherwise DB barfs up
for (var prop in object) {
if (_.isString(object[prop])) {
object[prop] = (object[prop].length > 0) ? object[prop] : null;
}
}
return object;
}
module.exports = utils;