-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathpretty_print_javascript.js
167 lines (135 loc) · 5.56 KB
/
pretty_print_javascript.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// https://github.com/cvadillo/js-object-pretty-print
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pretty = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
module.exports.pretty = function (jsObject, indentLength, outputTo, fullFunction) {
var indentString,
newLine,
newLineJoin,
TOSTRING,
TYPES,
valueType,
repeatString,
prettyObject,
prettyObjectJSON,
prettyObjectPrint,
prettyArray,
functionSignature,
pretty,
visited;
TOSTRING = Object.prototype.toString;
TYPES = {
'undefined' : 'undefined',
'number' : 'number',
'boolean' : 'boolean',
'string' : 'string',
'symbol' : 'symbol',
'[object Function]': 'function',
'[object RegExp]' : 'regexp',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object Error]' : 'error'
};
valueType = function (o) {
var type = TYPES[typeof o] || TYPES[TOSTRING.call(o)] || (o ? 'object' : 'null');
return type;
};
repeatString = function (src, length) {
var dst = '',
index;
for (index = 0; index < length; index += 1) {
dst += src;
}
return dst;
};
prettyObjectJSON = function (object, indent) {
var value = [],
property;
indent += indentString;
for (property in object) {
if (object.hasOwnProperty(property)) {
value.push(indent + '"' + property + '": ' + pretty(object[property], indent));
}
}
return value.join(newLineJoin) + newLine;
};
prettyObjectPrint = function (object, indent) {
var value = [],
property;
indent += indentString;
for (property in object) {
if (object.hasOwnProperty(property)) {
value.push(indent + property + ': ' + pretty(object[property], indent));
}
}
return value.join(newLineJoin) + newLine;
};
prettyArray = function (array, indent) {
var index,
length = array.length,
value = [];
indent += indentString;
for (index = 0; index < length; index += 1) {
value.push(pretty(array[index], indent, indent));
}
return value.join(newLineJoin) + newLine;
};
functionSignature = function (element) {
var signatureExpression,
signature;
element = element.toString();
signatureExpression = new RegExp('function\\s*.*\\s*\\(.*\\)');
signature = signatureExpression.exec(element);
signature = signature ? signature[0] : '[object Function]';
return fullFunction ? element : '"' + signature + '"';
};
pretty = function (element, indent, fromArray) {
var type;
type = valueType(element);
fromArray = fromArray || '';
if (visited.indexOf(element) === -1) {
switch (type) {
case 'array':
visited.push(element);
return fromArray + '[' + newLine + prettyArray(element, indent) + indent + ']';
case 'boolean':
return fromArray + (element ? 'true' : 'false');
case 'date':
return fromArray + '"' + element.toString() + '"';
case 'number':
return fromArray + element;
case 'object':
visited.push(element);
return fromArray + '{' + newLine + prettyObject(element, indent) + indent + '}';
case 'string':
return fromArray + JSON.stringify(element);
case 'function':
return fromArray + functionSignature(element);
case 'undefined':
return fromArray + 'undefined';
case 'null':
return fromArray + 'null';
default:
if (element.toString) {
return fromArray + '"' + element.toString() + '"';
}
return fromArray + '<<<ERROR>>> Cannot get the string value of the element';
}
}
return fromArray + 'circular reference to ' + element.toString();
};
if (jsObject) {
if (indentLength === undefined) {
indentLength = 4;
}
outputTo = (outputTo || 'print').toLowerCase();
indentString = repeatString(outputTo === 'html' ? ' ' : ' ', indentLength);
prettyObject = outputTo === 'print' ? prettyObjectPrint : prettyObjectJSON;
newLine = outputTo === 'html' ? '<br/>' : '\n';
newLineJoin = ',' + newLine;
visited = [];
return pretty(jsObject, '') + newLine;
}
return 'Error: no Javascript object provided';
};
},{}]},{},[1])(1)
});