-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (59 loc) · 2.72 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
66
67
68
69
"use strict";
exports.stringify = stringify;
var defaults = {
maxLength: 200,
doubleSpaceDepth: 1,
spaceBeforeColon: false,
spaceAfterColon: true,
spaceAfterComma: true,
spaceInsideBraces: true,
spaceInsideBrackets: true,
sameLineBraces: false,
sameLineBrackets: false,
typeOrder: {
boolean: 1,
number: 2,
string: 3,
default: 99,
}
};
function stringify(obj, opts, depth) {
var str, indent, type = typeof obj;
if (typeof depth !== 'number') { depth = 1; };
if (typeof opts !== 'object') { opts = {}; }
Object.keys(defaults).forEach(function(key){
if (!opts.hasOwnProperty(key)) {
opts[key] = defaults[key];
}
});
switch(type){
case "object":
indent = Array(depth).join('\t');
if (obj === null) {
str = "null";
} else if (Array.isArray(obj)) {
str = '[' + (opts.spaceInsideBrackets?' ':'') + '\n';
str += Object.keys(obj)
.map(function(key, i, arr){ return (depth <= opts.doubleSpaceDepth && i?'\n':'') + '\t' + indent + stringify(obj[key], opts, depth+1); })
.join(',' + (opts.spaceAfterComma?' ':'') + '\n');
str += (opts.spaceInsideBrackets?' ':'') + '\n' + indent + ']';
if (opts.sameLineBrackets) { str = str.replace(/\n\t*\[/g,'['); }
} else {
str = '{' + (opts.spaceInsideBraces?' ':'') + '\n';
str += Object.keys(obj)
.sort(function(a,b){ var sa=opts.typeOrder[typeof obj[a]]||opts.typeOrder.default, sb=opts.typeOrder[typeof obj[b]]||opts.typeOrder.default; return ((sa !== sb)?(sa - sb):(a>b?1:-1)); })
.map(function(key, i, arr){ return (depth <= opts.doubleSpaceDepth && i?'\n':'') + '\t' + indent + JSON.stringify(key) + (opts.spaceBeforeColon?' ':'') + ':' + (opts.spaceAfterColon?' ':'') + stringify(obj[key], opts, depth+1); })
.join(',' + (opts.spaceAfterComma?' ':'') + '\n');
str += (opts.spaceInsideBraces?' ':'') + '\n' + indent + '}';
if (opts.sameLineBraces) { str = str.replace(/\n\t*\{/g,'{'); }
}
if (str.length < opts.maxLength) { str = str.replace(/\n\t*/g,''); } // collapse short JSON sections
if (/ \n/.test(str)) { str = str.replace(/ \n/g,'\n'); } // remove extraneous spaces
if (/^{ +}/.test(str)) { str = str.replace(/^{ +}/,'{}'); } // collapse empty braces
if (/^\[ +\]/.test(str)) { str = str.replace(/^\[ +\]/,'[]'); } // collapse empty brackets
return str;
default:
str = JSON.stringify(obj);
}
return str;
}