What is object-inspect?
The object-inspect npm package is a utility for inspecting JavaScript objects. It is often used for debugging and logging purposes, as it can provide a string representation of an object, including its properties and their values, even if they are not enumerable or are symbols. It can handle various types of JavaScript values, including objects, arrays, functions, and primitives.
What are object-inspect's main functionalities?
Inspecting objects
This feature allows you to get a string representation of an object, which includes all its properties and their values.
var inspect = require('object-inspect');
var obj = { foo: 'bar', baz: 42 };
console.log(inspect(obj));
Inspecting arrays
This feature allows you to get a string representation of an array, including its elements.
var inspect = require('object-inspect');
var arr = ['foo', 'bar', 42];
console.log(inspect(arr));
Inspecting functions
This feature allows you to get a string representation of a function, including its name and the function keyword.
var inspect = require('object-inspect');
function exampleFunc() {}
console.log(inspect(exampleFunc));
Inspecting symbols
This feature allows you to get a string representation of a symbol, including its description.
var inspect = require('object-inspect');
var sym = Symbol('foo');
console.log(inspect(sym));
Inspecting non-enumerable properties
This feature allows you to inspect objects with non-enumerable properties, which are included in the string representation.
var inspect = require('object-inspect');
var obj = Object.create(null, { foo: { value: 'bar', enumerable: false } });
console.log(inspect(obj));
Other packages similar to object-inspect
util-inspect
This package is similar to object-inspect as it provides inspection capabilities for JavaScript objects. It is a part of Node.js's built-in 'util' module and can be used in a similar way to object-inspect. However, it is specific to Node.js and not intended for use in browsers.
pretty-format
Pretty-format is another package that provides similar functionality to object-inspect. It is used by Jest to format JavaScript object structures for display in test results. It offers customizable plugins to handle different data types and can serialize complex objects for snapshot testing.
object-inspect
string representations of objects in node and the browser
example
circular
var inspect = require('object-inspect');
var obj = { a: 1, b: [3,4] };
obj.c = obj;
console.log(inspect(obj));
dom element
var inspect = require('object-inspect');
var d = document.createElement('div');
d.setAttribute('id', 'beep');
d.innerHTML = '<b>wooo</b><i>iiiii</i>';
console.log(inspect([ d, { a: 3, b : 4, c: [5,6,[7,[8,[9]]]] } ]));
output:
[ <div id="beep">...</div>, { a: 3, b: 4, c: [ 5, 6, [ 7, [ 8, [ ... ] ] ] ] } ]
methods
var inspect = require('object-inspect')
var s = inspect(obj, opts={})
Return a string s
with the string representation of obj
up to a depth of opts.depth
.
Additional options:
quoteStyle
: must be "single" or "double", if present
install
With npm do:
npm install object-inspect
license
MIT