inspect-with-kind
util.inspect
with additional type information
const util = require('util');
util.inspect([1, 2, 3]);
const inspectWithKind = require('inspect-with-kind');
inspectWithKind([1, 2, 3]);
Installation
Use npm.
npm install inspect-with-kind
API
const inspectWithKind = require('inspect-with-kind');
inspectWithKind(value [, options])
value: any type
options: Object
(util.inspect
options)
Return: String
Almost the same as util.inspect
, but:
- It appends a type information to the string if the first argument is one of
Boolean
, String
, Number
, Array
, RegExp
, Date
, arguments
or a plain Object
. - Error stack trace is omitted.
breakLength
option defaults to Infinity
.maxArrayLength
option defaults to 10
.
const util = require('util');
const inspectWithKind = require('inspect-with-kind');
util.inspect(1);
inspectWithKind(1);
util.inspect('1');
inspectWithKind('1');
util.inspect(Buffer.from('1'));
inspectWithKind(Buffer.from('1'));
util.inspect(new Error('error!'));
inspectWithKind(new Error('error!'));
Example
This module is useful for making TypeError
error messages in your Node.js library.
const inspectWithKind = require('inspect-with-kind');
module.exports = function reverse(v) {
if (typeof v !== 'boolean') {
throw new TypeError(`Expected a Boolean value, but got ${inspectWithKind(v)}.`);
}
return !v;
};
const reverse = require('./reverse.js');
reverse(/true/);
License
Copyright (c) 2017 Shinnosuke Watanabe
Licensed under the MIT License.