graphql
Version:
A Query Language and Runtime which can target any service.
60 lines (46 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = inspect;
var _nodejsCustomInspectSymbol = _interopRequireDefault(require("./nodejsCustomInspectSymbol"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* Used to print values in error messages.
*/
function inspect(value) {
switch (_typeof(value)) {
case 'string':
return JSON.stringify(value);
case 'function':
return value.name ? "[function ".concat(value.name, "]") : '[function]';
case 'object':
if (value) {
var customInspectFn = getCustomFn(value);
if (customInspectFn) {
// $FlowFixMe(>=0.90.0)
var customValue = customInspectFn.call(value);
return typeof customValue === 'string' ? customValue : inspect(customValue);
} else if (Array.isArray(value)) {
return '[' + value.map(inspect).join(', ') + ']';
}
var properties = Object.keys(value).map(function (k) {
return "".concat(k, ": ").concat(inspect(value[k]));
}).join(', ');
return properties ? '{ ' + properties + ' }' : '{}';
}
return String(value);
default:
return String(value);
}
}
function getCustomFn(object) {
var customInspectFn = object[String(_nodejsCustomInspectSymbol.default)];
if (typeof customInspectFn === 'function') {
return customInspectFn;
}
if (typeof object.inspect === 'function') {
return object.inspect;
}
}