我認爲這會很有趣,我嘗試修改代碼作爲起點,但很快發現在正確的位置管理逗號,並且回車變得非常複雜。
因此,我將它轉換爲傳遞給它的對象的代碼,並在它前進時發出令牌以格式化輸出字符串。我粘貼了下面的代碼。
prettyPrint.js:
prettyPrint = function (data) {
return new prettyPrint.processor(data).output;
}
prettyPrint.indentString = ' ';
prettyPrint.processor = function (data) {
var indent = 0,
output = '',
tokens = {
value: 1,
openArray: 2,
arrayValueSeparator: 3,
closeArray: 4,
openObject: 5,
objectValueName: 6,
objectValueSeparator: 7,
closeObject: 8
};
function isArray(unknown) {
return Object.prototype.toString.call(unknown) === '[object Array]';
}
function isObject(unknown) {
return Object.prototype.toString.call(unknown) === '[object Object]';
}
function space() {
var count = indent;
var result = '';
while (count--) result += prettyPrint.indentString;
return result;
}
function emit(tokenType, value) {
switch (tokenType) {
case tokens.value:
output += value;
break;
case tokens.openArray:
output += '[';
break;
case tokens.arrayValueSeparator:
output += ', ';
break;
case tokens.closeArray:
output += ']';
break;
case tokens.openObject:
output += '{';
indent += 1;
break;
case tokens.objectValueName:
output += '\n' + space() + (/^[a-z][a-z0-9_]*$/i.test(value) ? value : "'" + value + "'") + ': ';
break;
case tokens.objectValueSeparator:
output += ',';
break;
case tokens.closeObject:
indent -= 1;
output += '\n' + space() + '}';
break;
}
}
function process(data) {
var p, first;
if (data === undefined) {
return;
}
// Don't surround null with quotes.
if (data === null) {
emit(prettyPrint.tokens.value, 'null');
}
else if (isArray(data)) {
emit(tokens.openArray);
first = true;
for (p in data) {
if (!first) {
emit(tokens.arrayValueSeparator);
}
process(data[p]);
first = false;
}
emit(tokens.closeArray);
}
else if (isObject(data)) {
emit(tokens.openObject);
first = true;
for (p in data) {
if (data.hasOwnProperty(p) && data[p] !== undefined) {
if (!first) {
emit(tokens.objectValueSeparator);
}
emit(tokens.objectValueName, p);
process(data[p]);
first = false;
}
}
emit(tokens.closeObject);
}
else if (data instanceof Date) {
emit(tokens.value, "'" + data.toISOString() + "'");
}
else if (typeof data === 'number') {
emit(tokens.value, isNaN(data) ? 'null' : data.toString());
}
else {
emit(tokens.value, "'" + data.toString() + "'");
}
}
// Start processing the data.
process(data);
// Export the data.
this.output = output;
}
prettyPrint.html:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Pretty Print Testing</title>
<script type="text/javascript" src="prettyPrint.js"></script>
</head>
<body>
<pre id="pretty1"></pre>
<pre id="pretty2"></pre>
<script type="text/javascript">
document.getElementById('pretty1').innerHTML = prettyPrint({ 'a': 'b', 'b': { 'c': 'd' } });
document.getElementById('pretty2').innerHTML = prettyPrint([1, 2, "three", { 'complex-name': [1] }, { simpleName: { subArray: [1, 2, 3], subString: "Hello" } }]);
</script>
</body>
</html>
輸出:
{
a: 'b',
b: {
c: 'd'
}
}
[1, 2, 'three', {
'complex-name': [1]
}, {
simpleName: {
subArray: [1, 2, 3],
subString: 'Hello'
}
}]
我希望這是對你有用。
爲什麼你不能使用polyfills? – Bart
如何通過API使用XMLHttpRequest或ActiveXObject和漂亮的打印? – shiva