-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
98 lines (85 loc) · 2.54 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
// 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).
const stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g;
export default function stringify(passedObj, options = {}) {
const indent = JSON.stringify(
[1],
undefined,
options.indent === undefined ? 2 : options.indent
).slice(2, -3);
const maxLength =
indent === ""
? Infinity
: options.maxLength === undefined
? 80
: options.maxLength;
let { replacer } = options;
return (function _stringify(obj, currentIndent, reserved) {
if (obj && typeof obj.toJSON === "function") {
obj = obj.toJSON();
}
const string = JSON.stringify(obj, replacer);
if (string === undefined) {
return string;
}
const length = maxLength - currentIndent.length - reserved;
if (string.length <= length) {
const prettified = string.replace(
stringOrChar,
(match, stringLiteral) => {
return stringLiteral || `${match} `;
}
);
if (prettified.length <= length) {
return prettified;
}
}
if (replacer != null) {
obj = JSON.parse(string);
replacer = undefined;
}
if (typeof obj === "object" && obj !== null) {
const nextIndent = currentIndent + indent;
const items = [];
let index = 0;
let start;
let end;
if (Array.isArray(obj)) {
start = "[";
end = "]";
const { length } = obj;
for (; index < length; index++) {
items.push(
_stringify(obj[index], nextIndent, index === length - 1 ? 0 : 1) ||
"null"
);
}
} else {
start = "{";
end = "}";
const keys = Object.keys(obj);
const { length } = keys;
for (; index < length; index++) {
const key = keys[index];
const keyPart = `${JSON.stringify(key)}: `;
const value = _stringify(
obj[key],
nextIndent,
keyPart.length + (index === length - 1 ? 0 : 1)
);
if (value !== undefined) {
items.push(keyPart + value);
}
}
}
if (items.length > 0) {
return [start, indent + items.join(`,\n${nextIndent}`), end].join(
`\n${currentIndent}`
);
}
}
return string;
})(passedObj, "", 0);
}