forked from lydell/json-stringify-pretty-compact
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
146 lines (123 loc) · 3.7 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function isObject (obj) {
return typeof obj === 'object' && obj !== null
}
function forEach (obj, cb) {
if (Array.isArray(obj)) {
obj.forEach(cb)
} else if (isObject(obj)) {
Object.keys(obj).forEach(function (key) {
var val = obj[key]
cb(val, key)
})
}
}
function getTreeDepth (obj) {
var depth = 0
if (Array.isArray(obj) || isObject(obj)) {
forEach(obj, function (val) {
if (Array.isArray(val) || isObject(val)) {
var tmpDepth = getTreeDepth(val)
if (tmpDepth > depth) {
depth = tmpDepth
}
}
})
return depth + 1
}
return depth
}
function stringify (obj, options) {
options = options || {}
var indent = JSON.stringify([1], null, get(options, 'indent', 2)).slice(2, -3)
var addMargin = get(options, 'margins', false)
var addArrayMargin = get(options, 'arrayMargins', false)
var addObjectMargin = get(options, 'objectMargins', false)
var maxLength = (indent === '' ? Infinity : get(options, 'maxLength', 80))
var maxNesting = get(options, 'maxNesting', Infinity)
return (function _stringify (obj, currentIndent, reserved) {
if (obj && typeof obj.toJSON === 'function') {
obj = obj.toJSON()
}
var string = JSON.stringify(obj)
if (string === undefined) {
return string
}
var length = maxLength - currentIndent.length - reserved
var treeDepth = getTreeDepth(obj)
if (treeDepth <= maxNesting && string.length <= length) {
var prettified = prettify(string, {
addMargin: addMargin,
addArrayMargin: addArrayMargin,
addObjectMargin: addObjectMargin
})
if (prettified.length <= length) {
return prettified
}
}
if (isObject(obj)) {
var nextIndent = currentIndent + indent
var items = []
var delimiters
var comma = function (array, index) {
return (index === array.length - 1 ? 0 : 1)
}
if (Array.isArray(obj)) {
for (var index = 0; index < obj.length; index++) {
items.push(
_stringify(obj[index], nextIndent, comma(obj, index)) || 'null'
)
}
delimiters = '[]'
} else {
Object.keys(obj).forEach(function (key, index, array) {
var keyPart = JSON.stringify(key) + ': '
var value = _stringify(obj[key], nextIndent,
keyPart.length + comma(array, index))
if (value !== undefined) {
items.push(keyPart + value)
}
})
delimiters = '{}'
}
if (items.length > 0) {
return [
delimiters[0],
indent + items.join(',\n' + nextIndent),
delimiters[1]
].join('\n' + currentIndent)
}
}
return string
}(obj, '', 0))
}
// Note: This regex matches even invalid JSON strings, but since we’re
// working on the output of `JSON.stringify` we know that only valid strings
// are present (unless the user supplied a weird `options.indent` but in
// that case we don’t care since the output would be invalid anyway).
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,\][}{]/g
function prettify (string, options) {
options = options || {}
var tokens = {
'{': '{',
'}': '}',
'[': '[',
']': ']',
',': ', ',
':': ': '
}
if (options.addMargin || options.addObjectMargin) {
tokens['{'] = '{ '
tokens['}'] = ' }'
}
if (options.addMargin || options.addArrayMargin) {
tokens['['] = '[ '
tokens[']'] = ' ]'
}
return string.replace(stringOrChar, function (match, string) {
return string ? match : tokens[match]
})
}
function get (options, name, defaultValue) {
return (name in options ? options[name] : defaultValue)
}
module.exports = stringify