Inspect JavaScript object methods and properties in the console.

Provides colourful functions to inspect JavaScript objects.

The probe() function outputs a prototype hierarchy tree to the console.

function outputs a prototype hierarchy tree to the console. The json() function safely writes a stringified object output to the console.

function safely writes a stringified object output to the console. The yaml() function converts objects into yaml format and outputs to the console.

function converts objects into yaml format and outputs to the console. The ls() function converts objects into a colourful format and outputs to the console.

Installing

Node: v6.0.0 or later.

Browser: Not tested

npm install --save-dev console-probe

Quick Start

Not recommended for production environments

const cp = require ( ' ./console-probe ' ) const arrLen = 2 const aussieSlang = { ' name ' : ' Aussie Slang Words ' , ' gday ' : Infinity , ' maccas ' : Number . NaN , ' arvo ' : undefined , ' straya ' : null , ' footy ' : { specky : true } , ' biccy ' : ( size , toppings ) => { } , ' servo ' : true , ' choccy ' : Symbol ( ' Mmmmm... ' ) , ' bottle-o ' : Error ( ' Cheers mate! My shout next ' ) , ' tinny ' : 42 , ' coppa ' : new Date ( ) , ' tradie ' : ' She \' ll be right mate? ' , ' postie ' : / a . long . regexp . that . keeps . giving / , ' garbo ' : [ 1 , 2 , 3 ] , ' muso ' : new Int8Array ( arrLen ) , ' cabbie ' : new Uint8Array ( arrLen ) , ' ambo ' : new Uint8ClampedArray ( arrLen ) , ' prezzie ' : new Int16Array ( arrLen ) , ' chrissie ' : new Uint16Array ( arrLen ) , ' cuppa ' : new Int32Array ( arrLen ) , ' mate ' : new Uint32Array ( arrLen ) , ' snag ' : new Float32Array ( arrLen ) , ' drongo ' : new Float64Array ( arrLen ) , ' fairDinkum ' : new Map ( [ [ ' foo ' , ' bar ' ] ] ) , ' bonza ' : new Set ( [ [ ' foo ' , ' bar ' ] ] ) , ' tooRight ' : new WeakMap ( ) , ' dunny ' : new WeakSet ( ) , ' cobber ' : new ArrayBuffer ( arrLen ) , ' barbie ' : new SharedArrayBuffer ( arrLen ) , ' stickybeak ' : Atomics , ' stoked ' : new DataView ( new ArrayBuffer ( arrLen ) ) , ' ripper ' : Promise . resolve ( ) , ' mongrel ' : ( function * ( ) { } ) ( ) , ' holyDooley ' : function * ( foo , bar ) { } , ' roo ' : async function ( foo , bar ) { } } const secret = Symbol ( ' Hidden Property ' ) aussieSlang [ secret ] = ' Bogan ' cp . probe ( aussieSlang ) cp . json ( aussieSlang ) cp . yaml ( aussieSlang ) cp . ls ( aussieSlang ) console . probe ( aussieSlang ) console . json ( aussieSlang ) console . yaml ( aussieSlang ) console . ls ( aussieSlang ) cp . apply ( ) console . probe ( aussieSlang ) console . json ( aussieSlang ) console . yaml ( aussieSlang ) console . ls ( aussieSlang ) const foo = { } cp . apply ( foo ) foo . probe ( aussieSlang ) foo . json ( aussieSlang ) foo . yaml ( aussieSlang ) foo . ls ( aussieSlang )

The above code will produce the following results when it writes to the console.

The probe function output:

Note: Type detection errors will display as [Unknown] .

The json function output:

The yaml function output:

The ls function output:

Rational

There are many amazing packages on npm . Many of those packages are not well documented. Rather than go straight to reading source code I wrote console-probe to inspect objects and discover methods and properties. Using Node.js with inspect is often a better approach however I don't always have it running; this is when console-probe comes in handy.

Function

The console-probe package provides four functions that will write to the console:

probe(obj) : The probe function uses Object.getOwnPropertyNames() and Object.getOwnPropertySymbols() to enumerate the members of an object through its prototype hierarchy. Using the type list from MDN the types are detected. After a little formatting the result is written to the console using the archy package with some colour added by chalk.

: The probe function uses and to enumerate the members of an object through its prototype hierarchy. Using the type list from MDN the types are detected. After a little formatting the result is written to the console using the archy package with some colour added by chalk. json(obj, replacer, spacer, color) : Uses fast-safe-stringify and json-colorizer to safely write the stringified object out to the console.

: Uses fast-safe-stringify and json-colorizer to safely write the stringified object out to the console. yaml(obj, options, indentation) : A simple wrapper around the prettyjson package render function.

: A simple wrapper around the prettyjson package render function. ls(obj) : A simple wrapper around the jsome package render function.

API

probe Function

Description: Inspects the passed objects properties and methods, then the prototype of the passed object, and so on till the last prototype is analyzed. A tree of the properties and methods on each prototype is written to the console.

Method Signature: probe(object)

Parameter: object can be any JavaScript type.

Details:

Passing either null or undefined will write [console-probe] Invalid Type: to the console.

or will write to the console. String values with newline characters are stripped from string stubs.

Example:

const cp = require ( ' console-probe ' ) cp . probe ( { key : ' value ' } )

json Function

Description: This function simply calls fast-safe-stringify and then adds color via json-colorizer. Once that is done it writes the result to the console.

Method Signature: json(object, replacer, spacer, color)

Parameter:

object can be any object you wish to stringify.

can be any object you wish to stringify. replacer alters the behavior of the stringification process.

alters the behavior of the stringification process. spacer inserts white space into the output JSON string for readability purposes.

inserts white space into the output JSON string for readability purposes. color enables customization of the colour displayed.

Details:

The json function defaults to replacer = null and spacer = 2 .

function defaults to and . See both fast-safe-stringify and JSON.stringify for more details.

Change the color displayed using a color object from the json-colorizer options.

Example:

const cp = require ( ' console-probe ' ) cp . json ( { key : ' value ' } )

yaml Function

Description: This function wraps the prettyjson render function and writes the result to the console. The result is a colorized formatted YAML representation of the object data.

Signature: yaml(object, options, indentation)

Parameter:

object can be any object you wish to display in YAML format.

can be any object you wish to display in YAML format. options should hold options for the prettyjson render function.

should hold options for the prettyjson render function. indentation controls the indentation for the YAML output.

Details:

The yaml function is simply a wrapper around the prettyjson package.

function is simply a wrapper around the prettyjson package. See the prettyjson documentation and code for the options and indentation.

Example:

const cp = require ( ' console-probe ' ) cp . yaml ( { key : ' value ' } )

ls Function

Description: This function wraps the jsome render function and writes the result to the console. The result is a colorized formatted representation of the object data.

Signature: ls(object)

Parameter:

object can be any object you wish to display.

Details:

The ls function is simply a wrapper around the jsome package.

function is simply a wrapper around the jsome package. See the jsome documentation for more detail.

Example:

const cp = require ( ' console-probe ' ) cp . ls ( { key : ' value ' } )

apply Function

Signature: apply(object)

Parameter: object can be any object you would like to add console-probe functions to.

Details:

The apply function is a convenience method to add the console-probe functions to an object.

function is a convenience method to add the functions to an object. If no object is passed to the apply function then the console-probe functions will be added to the console object.

is passed to the function then the functions will be added to the object. Passing an object, such as a logger, will add the console-probe functions to the object.

Example:

const cp = require ( ' console-probe ' ) cp . apply ( ) const foo = { } cp . apply ( foo )

Another approach to simply augment the console:

require ( ' console-probe ' ) . apply ( )

About the Owner

I, Grant Carthew, am a technologist, trainer, and Dad from Queensland, Australia. I work on code in a number of personal projects and when the need arises I build my own packages.

This project exists because I wanted to inspect objects from the console.

Everything I do in open source is done in my own time and as a contribution to the open source community.

If you are using my projects and would like to thank me or support me, please click the Patreon link below.

See my other projects on NPM.

Contributing

Fork it! Create your feature branch: git checkout -b my-new-feature Commit your changes: git commit -am 'Add some feature' Push to the branch: git push origin my-new-feature Submit a pull request :D

Change Log