-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (56 loc) · 1.71 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
var moment = require('moment');
module.exports = (function () {
var options = {
utc: false,
fnCheck: function (_, value) {
return (typeof value === 'string') && moment(value, moment.ISO_8601, true).isValid() && value.length >= 6;
},
};
function fnReviver(reviver) {
return function (key, value) {
if (options.fnCheck(key, value)) {
value = moment(value).toDate();
}
if (reviver) {
value = reviver(key, value);
}
return value;
};
}
function fnReplacer(replacer) {
var fn = replacer || function (key, value) { return value; };
if (!options.utc) {
fn = function (key, value) {
if (options.fnCheck(key, value)) {
value = moment(value).format('YYYY-MM-DDTHH:mm:ss.SSSZ');
}
if (replacer) {
value = replacer(key, value);
}
return value;
};
}
return fn;
}
return {
stringify: function (value, replacer, space) {
return JSON.stringify(value, fnReplacer(replacer), space);
},
parse: function (text, reviver) {
return JSON.parse(text, fnReviver(reviver));
},
getOptions: function () {
return Object.assign({}, options);
},
setOptions: function (opt) {
var key;
for (key in opt) {
if (opt.hasOwnProperty(key)) {
options[key] = opt[key];
}
}
},
getReviver: fnReviver,
getReplacer: fnReplacer
};
}());