-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
48 lines (41 loc) · 1020 Bytes
/
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
48
var utils = module.exports = {};
// YYYYMMDDTHHMMSSZ -> YYYY-MM-DDTHH:MM:SSZ
utils.fixISODate = function (d)
{
return new Date
(
d.substring( 0, 4) + '-' +
d.substring( 4, 6) + '-' +
d.substring( 6, 11) + ':' +
d.substring(11, 13) + ':' +
d.substring(13, 16)
);
};
// Removes indices starting with an underscore (_) (on the original object!)
utils.removeMetadata = function (obj, stack)
{
if (typeof(stack) != 'number') stack = 100;
if (stack <= 0) return;
for (var key in obj)
{
if (/^(_|\$)/.test(key))
{
delete obj[key];
continue;
}
if (typeof(obj[key]) == 'object' || obj[key] instanceof Array)
{
utils.removeMetadata(obj[key], stack - 1);
}
}
};
// '0' -> '00'
utils.padZero = function (str)
{
return (str < 10) ? '0' + str : str;
};
// Use like: mongooseResultSet.map(utils.toObject);
utils.toObject = function (v)
{
return v.toObject();
};