Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
//
2
// Eyes.js - a customizable value inspector for Node.js
3
//
4
// usage:
5
//
6
// var inspect = require('eyes').inspector({styles: {all: 'magenta'}});
7
// inspect(something); // inspect with the settings passed to `inspector`
8
//
9
// or
10
//
11
// var eyes = require('eyes');
12
// eyes.inspect(something); // inspect with the default settings
13
//
14
var eyes = exports,
15
stack = [];
16
17
eyes.defaults = {
18
styles: { // Styles applied to stdout
19
all: 'cyan', // Overall style applied to everything
20
label: 'underline', // Inspection labels, like 'array' in `array: [1, 2, 3]`
21
other: 'inverted', // Objects which don't have a literal representation, such as functions
22
key: 'bold', // The keys in object literals, like 'a' in `{a: 1}`
23
special: 'grey', // null, undefined...
24
string: 'green',
25
number: 'magenta',
26
bool: 'blue', // true false
27
regexp: 'green', // /\d+/
28
},
29
pretty: true, // Indent object literals
30
hideFunctions: false,
31
showHidden: false,
32
stream: process.stdout,
33
maxLength: 2048 // Truncate output if longer
34
};
35
36
// Return a curried inspect() function, with the `options` argument filled in.
37
eyes.inspector = function (options) {
38
var that = this;
39
return function (obj, label, opts) {
40
return that.inspect.call(that, obj, label,
41
merge(options || {}, opts || {}));
42
};
43
};
44
45
// If we have a `stream` defined, use it to print a styled string,
46
// if not, we just return the stringified object.
47
eyes.inspect = function (obj, label, options) {
48
options = merge(this.defaults, options || {});
49
50
if (options.stream) {
51
return this.print(stringify(obj, options), label, options);
52
} else {
53
return stringify(obj, options) + (options.styles ? '\033[39m' : '');
54
}
55
};
56
57
// Output using the 'stream', and an optional label
58
// Loop through `str`, and truncate it after `options.maxLength` has been reached.
59
// Because escape sequences are, at this point embeded within
60
// the output string, we can't measure the length of the string
61
// in a useful way, without separating what is an escape sequence,
62
// versus a printable character (`c`). So we resort to counting the
63
// length manually.
64
eyes.print = function (str, label, options) {
65
for (var c = 0, i = 0; i < str.length; i++) {
66
if (str.charAt(i) === '\033') { i += 4 } // `4` because '\033[25m'.length + 1 == 5
67
else if (c === options.maxLength) {
68
str = str.slice(0, i - 1) + '…';
69
break;
70
} else { c++ }
71
}
72
return options.stream.write.call(options.stream, (label ?
73
this.stylize(label, options.styles.label, options.styles) + ': ' : '') +
74
this.stylize(str, options.styles.all, options.styles) + '\033[0m' + "\n");
75
};
76
77
// Apply a style to a string, eventually,
78
// I'd like this to support passing multiple
79
// styles.
80
eyes.stylize = function (str, style, styles) {
81
var codes = {
82
'bold' : [1, 22],
83
'underline' : [4, 24],
84
'inverse' : [7, 27],
85
'cyan' : [36, 39],
86
'magenta' : [35, 39],
87
'blue' : [34, 39],
88
'yellow' : [33, 39],
89
'green' : [32, 39],
90
'red' : [31, 39],
91
'grey' : [90, 39]
92
}, endCode;
93
94
if (style && codes[style]) {
95
endCode = (codes[style][1] === 39 && styles.all) ? codes[styles.all][0]
96
: codes[style][1];
97
return '\033[' + codes[style][0] + 'm' + str +
98
'\033[' + endCode + 'm';
99
} else { return str }
100
};
101
102
// Convert any object to a string, ready for output.
103
// When an 'array' or an 'object' are encountered, they are
104
// passed to specialized functions, which can then recursively call
105
// stringify().
106
function stringify(obj, options) {
107
var that = this, stylize = function (str, style) {
108
return eyes.stylize(str, options.styles[style], options.styles)
109
}, index, result;
110
111
if ((index = stack.indexOf(obj)) !== -1) {
112
return stylize(new(Array)(stack.length - index + 1).join('.'), 'special');
113
}
114
stack.push(obj);
115
116
result = (function (obj) {
117
switch (typeOf(obj)) {
118
case "string" : obj = stringifyString(obj.indexOf("'") === -1 ? "'" + obj + "'"
119
: '"' + obj + '"');
120
return stylize(obj, 'string');
121
case "regexp" : return stylize('/' + obj.source + '/', 'regexp');
122
case "number" : return stylize(obj + '', 'number');
123
case "function" : return options.stream ? stylize("Function", 'other') : '[Function]';
124
case "null" : return stylize("null", 'special');
125
case "undefined": return stylize("undefined", 'special');
126
case "boolean" : return stylize(obj + '', 'bool');
127
case "date" : return stylize(obj.toUTCString());
128
case "array" : return stringifyArray(obj, options, stack.length);
129
case "object" : return stringifyObject(obj, options, stack.length);
130
}
131
})(obj);
132
133
stack.pop();
134
return result;
135
};
136
137
// Escape invisible characters in a string
138
function stringifyString (str, options) {
139
return str.replace(/\\/g, '\\\\')
140
.replace(/\n/g, '\\n')
141
.replace(/[\u0001-\u001F]/g, function (match) {
142
return '\\0' + match[0].charCodeAt(0).toString(8);
143
});
144
}
145
146
// Convert an array to a string, such as [1, 2, 3].
147
// This function calls stringify() for each of the elements
148
// in the array.
149
function stringifyArray(ary, options, level) {
150
var out = [];
151
var pretty = options.pretty && (ary.length > 4 || ary.some(function (o) {
152
return (o !== null && typeof(o) === 'object' && Object.keys(o).length > 0) ||
153
(Array.isArray(o) && o.length > 0);
154
}));
155
var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' ';
156
157
for (var i = 0; i < ary.length; i++) {
158
out.push(stringify(ary[i], options));
159
}
160
161
if (out.length === 0) {
162
return '[]';
163
} else {
164
return '[' + ws
165
+ out.join(',' + (pretty ? ws : ' '))
166
+ (pretty ? ws.slice(0, -4) : ws) +
167
']';
168
}
169
};
170
171
// Convert an object to a string, such as {a: 1}.
172
// This function calls stringify() for each of its values,
173
// and does not output functions or prototype values.
174
function stringifyObject(obj, options, level) {
175
var out = [];
176
var pretty = options.pretty && (Object.keys(obj).length > 2 ||
177
Object.keys(obj).some(function (k) { return typeof(obj[k]) === 'object' }));
178
var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' ';
179
180
var keys = options.showHidden ? Object.keys(obj) : Object.getOwnPropertyNames(obj);
181
keys.forEach(function (k) {
182
if (Object.prototype.hasOwnProperty.call(obj, k)
183
&& !(obj[k] instanceof Function && options.hideFunctions)) {
184
out.push(eyes.stylize(k, options.styles.key, options.styles) + ': ' +
185
stringify(obj[k], options));
186
}
187
});
188
189
if (out.length === 0) {
190
return '{}';
191
} else {
192
return "{" + ws
193
+ out.join(',' + (pretty ? ws : ' '))
194
+ (pretty ? ws.slice(0, -4) : ws) +
195
"}";
196
}
197
};
198
199
// A better `typeof`
200
function typeOf(value) {
201
var s = typeof(value),
202
types = [Object, Array, String, RegExp, Number, Function, Boolean, Date];
203
204
if (s === 'object' || s === 'function') {
205
if (value) {
206
types.forEach(function (t) {
207
if (value instanceof t) { s = t.name.toLowerCase() }
208
});
209
} else { s = 'null' }
210
}
211
return s;
212
}
213
214
function merge(/* variable args */) {
215
var objs = Array.prototype.slice.call(arguments);
216
var target = {};
217
218
objs.forEach(function (o) {
219
Object.keys(o).forEach(function (k) {
220
if (k === 'styles') {
221
if (! o.styles) {
222
target.styles = false;
223
} else {
224
target.styles = {}
225
for (var s in o.styles) {
226
target.styles[s] = o.styles[s];
227
}
228
}
229
} else {
230
target[k] = o[k];
231
}
232
});
233
});
234
return target;
235
}
236
237
238