mirror of https://gitee.com/bigwinds/arangodb
ArangoDB-ified util.format/util.inspect.
This commit is contained in:
parent
0a89e0c113
commit
dd1dd2d9e0
|
@ -5,7 +5,7 @@
|
||||||
///
|
///
|
||||||
/// DISCLAIMER
|
/// DISCLAIMER
|
||||||
///
|
///
|
||||||
/// Copyright 2004-2013 triAGENS GmbH, Cologne, Germany
|
/// Copyright 2004-2015 triAGENS GmbH, Cologne, Germany
|
||||||
///
|
///
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
/// you may not use this file except in compliance with the License.
|
/// you may not use this file except in compliance with the License.
|
||||||
|
@ -21,8 +21,8 @@
|
||||||
///
|
///
|
||||||
/// Copyright holder is triAGENS GmbH, Cologne, Germany
|
/// Copyright holder is triAGENS GmbH, Cologne, Germany
|
||||||
///
|
///
|
||||||
/// @author Dr. Frank Celler
|
/// @author Dr. Frank Celler, Alan Plum
|
||||||
/// @author Copyright 2010-2013, triAGENS GmbH, Cologne, Germany
|
/// @author Copyright 2010-2015, triAGENS GmbH, Cologne, Germany
|
||||||
///
|
///
|
||||||
/// Parts of the code are based on:
|
/// Parts of the code are based on:
|
||||||
///
|
///
|
||||||
|
@ -55,13 +55,23 @@
|
||||||
var console = require('console');
|
var console = require('console');
|
||||||
var Buffer = require('buffer').Buffer;
|
var Buffer = require('buffer').Buffer;
|
||||||
|
|
||||||
|
function arangoPrint(obj) {
|
||||||
|
var ctx = {output: ''};
|
||||||
|
obj._PRINT(ctx);
|
||||||
|
return ctx.output;
|
||||||
|
}
|
||||||
|
|
||||||
var formatRegExp = /%[sdj%]/g;
|
var formatRegExp = /%[sdj%]/g;
|
||||||
exports.format = function(f) {
|
exports.format = function(f) {
|
||||||
if (!isString(f)) {
|
if (!isString(f)) {
|
||||||
var objects = [];
|
var objects = [];
|
||||||
for (var i = 0; i < arguments.length; i++) {
|
for (var i = 0; i < arguments.length; i++) {
|
||||||
|
if (arguments[i] && typeof arguments[i]._PRINT === 'function') {
|
||||||
|
objects.push(arangoPrint(arguments[i]));
|
||||||
|
} else {
|
||||||
objects.push(inspect(arguments[i]));
|
objects.push(inspect(arguments[i]));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return objects.join(' ');
|
return objects.join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,10 +97,14 @@ exports.format = function(f) {
|
||||||
for (var x = args[i]; i < len; x = args[++i]) {
|
for (var x = args[i]; i < len; x = args[++i]) {
|
||||||
if (isNull(x) || !isObject(x)) {
|
if (isNull(x) || !isObject(x)) {
|
||||||
str += ' ' + x;
|
str += ' ' + x;
|
||||||
|
} else {
|
||||||
|
if (x && typeof x._PRINT === 'function') {
|
||||||
|
str += ' ' + arangoPrint(x);
|
||||||
} else {
|
} else {
|
||||||
str += ' ' + inspect(x);
|
str += ' ' + inspect(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return str;
|
return str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -226,6 +240,16 @@ function formatValue(ctx, value, recurseTimes) {
|
||||||
keys = Object.getOwnPropertyNames(value);
|
keys = Object.getOwnPropertyNames(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof Object.getOwnPropertySymbols === 'function') {
|
||||||
|
var symbols = Object.getOwnPropertySymbols(value);
|
||||||
|
keys = keys.concat(symbols);
|
||||||
|
symbols.forEach(function (symbol) {
|
||||||
|
if (Object.getOwnPropertyDescriptor(value, symbol).enumerable) {
|
||||||
|
visibleKeys[symbol] = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// This could be a boxed primitive (new String(), etc.), check valueOf()
|
// This could be a boxed primitive (new String(), etc.), check valueOf()
|
||||||
// NOTE: Avoid calling `valueOf` on `Date` instance because it will return
|
// NOTE: Avoid calling `valueOf` on `Date` instance because it will return
|
||||||
// a number which, when object has some additional user-stored `keys`,
|
// a number which, when object has some additional user-stored `keys`,
|
||||||
|
@ -325,6 +349,10 @@ function formatValue(ctx, value, recurseTimes) {
|
||||||
base = ' ' + '[Boolean: ' + formatted + ']';
|
base = ' ' + '[Boolean: ' + formatted + ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!base && typeof value._PRINT === 'function') {
|
||||||
|
base = ' ' + arangoPrint(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (keys.length === 0 && (!array || value.length === 0)) {
|
if (keys.length === 0 && (!array || value.length === 0)) {
|
||||||
return braces[0] + base + braces[1];
|
return braces[0] + base + braces[1];
|
||||||
}
|
}
|
||||||
|
@ -358,9 +386,7 @@ function formatPrimitive(ctx, value) {
|
||||||
if (isUndefined(value))
|
if (isUndefined(value))
|
||||||
return ctx.stylize('undefined', 'undefined');
|
return ctx.stylize('undefined', 'undefined');
|
||||||
if (isString(value)) {
|
if (isString(value)) {
|
||||||
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
|
var simple = JSON.stringify(value);
|
||||||
.replace(/'/g, "\\'")
|
|
||||||
.replace(/\\"/g, '"') + '\'';
|
|
||||||
return ctx.stylize(simple, 'string');
|
return ctx.stylize(simple, 'string');
|
||||||
}
|
}
|
||||||
if (isNumber(value)) {
|
if (isNumber(value)) {
|
||||||
|
@ -455,6 +481,9 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isUndefined(name)) {
|
if (isUndefined(name)) {
|
||||||
|
if (isSymbol(key)) {
|
||||||
|
name = ctx.stylize(key.toString(), 'name');
|
||||||
|
} else {
|
||||||
if (array && key.match(/^\d+$/)) {
|
if (array && key.match(/^\d+$/)) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
@ -463,13 +492,10 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
||||||
name = name.substr(1, name.length - 2);
|
name = name.substr(1, name.length - 2);
|
||||||
name = ctx.stylize(name, 'name');
|
name = ctx.stylize(name, 'name');
|
||||||
} else {
|
} else {
|
||||||
name = name.replace(/'/g, "\\'")
|
|
||||||
.replace(/\\"/g, '"')
|
|
||||||
.replace(/(^"|"$)/g, "'")
|
|
||||||
.replace(/\\\\/g, '\\');
|
|
||||||
name = ctx.stylize(name, 'string');
|
name = ctx.stylize(name, 'string');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return name + ': ' + str;
|
return name + ': ' + str;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue