-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (42 loc) · 1.43 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
'use strict';
var beautify = require('js-beautify').js_beautify;
/**
* Convert Object into string
* @param {Object} object
* @param {Object} opt
*/
module.exports = function objectStringify(object, opt) {
opt = opt || {};
// Object stringification
var o = JSON.stringify(object, function(key, value) {
var dateRegex = /^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))$/;
if(dateRegex.test(value)){
return "new Date('" + value + "')";
}
else if(typeof value === 'function'){
return value.toString()
.replace('"', '\'')
.replace(/(\r)/g, '')
.replace(/(\n)/g, '')
.replace(/(\t)/g, '');
}
else {
return value;
}
});
// Remove quote that surrounds functions
o = o.replace(/"(function.*?})"/g, '$1');
// Remove quote that surrounds date object
var dateObjRegex = /('|")new Date\('[0-Z-.]+'\)('|")/g;
var dates = o.match(dateObjRegex);
if(dates){
dates.forEach(function(date){
o = o.replace(date, date.slice(1, -1));
});
}
// Apply options
if (opt.beautify) {
o = beautify(o, { indent_size: opt.indent_size || 4 });
}
return o;
};