Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Inspect JavaScript object methods and properties in the console.

License

NotificationsYou must be signed in to change notification settings

grantcarthew/node-console-probe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inspect JavaScript object methods and properties in the console.

MaintainabilityBuild Statusjs-standard-stylePatreon Donation

Event Emitter Example

NPM

Provides colourful functions to inspect JavaScript objects.

  • Theprobe() function outputs a prototype hierarchy tree to the console.
  • Thejson() function safely writes a stringified object output to the console.
  • Theyaml() function converts objects into yaml format and outputs to the console.
  • Thels() 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

constcp=require('./console-probe')constarrLen=2constaussieSlang={'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':newDate(),'tradie':'She\'ll be right mate?','postie':/a.long.regexp.that.keeps.giving/,'garbo':[1,2,3],'muso':newInt8Array(arrLen),'cabbie':newUint8Array(arrLen),'ambo':newUint8ClampedArray(arrLen),'prezzie':newInt16Array(arrLen),'chrissie':newUint16Array(arrLen),'cuppa':newInt32Array(arrLen),'mate':newUint32Array(arrLen),'snag':newFloat32Array(arrLen),'drongo':newFloat64Array(arrLen),'fairDinkum':newMap([['foo','bar']]),'bonza':newSet([['foo','bar']]),'tooRight':newWeakMap(),'dunny':newWeakSet(),'cobber':newArrayBuffer(arrLen),'barbie':newSharedArrayBuffer(arrLen),'stickybeak':Atomics,'stoked':newDataView(newArrayBuffer(arrLen)),'ripper':Promise.resolve(),'mongrel':(function*(){})(),'holyDooley':function*(foo,bar){},'roo':asyncfunction(foo,bar){}}constsecret=Symbol('Hidden Property')aussieSlang[secret]='Bogan'// Calling console-probe functions.cp.probe(aussieSlang)// Writes a prototype tree to the consolecp.json(aussieSlang)// Writes a JSON formatted object to the consolecp.yaml(aussieSlang)// Writes a YAML formatted object to the consolecp.ls(aussieSlang)// Writes a formatted object to the console// Adding console-probe functions to the console.console.probe(aussieSlang)// Throws exception 'console.probe is not a function'console.json(aussieSlang)// Throws exception 'console.json is not a function'console.yaml(aussieSlang)// Throws exception 'console.yaml is not a function'console.ls(aussieSlang)// Throws exception 'console.ls is not a function'cp.apply()console.probe(aussieSlang)// Writes a prototype tree to the consoleconsole.json(aussieSlang)// Writes a JSON formatted object to the consoleconsole.yaml(aussieSlang)// Writes a YAML formatted object to the consoleconsole.ls(aussieSlang)// Writes a formatted object to the console// Adding console-probe functions to an object.constfoo={}cp.apply(foo)foo.probe(aussieSlang)// Writes prototype tree to the consolefoo.json(aussieSlang)// Writes a JSON formatted object to the consolefoo.yaml(aussieSlang)// Writes a YAML formatted object to the consolefoo.ls(aussieSlang)// Writes a formatted object to the console

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

Theprobe function output:

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

Example Probe Output

Thejson function output:

Example Json Output

Theyaml function output:

Example Yaml Output

Thels function output:

Example ls Output

Rational

There are many amazing packages onnpm. Many of those packages are not well documented. Rather than go straight to reading source code I wroteconsole-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 whenconsole-probe comes in handy.

Function

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

  • probe(obj): The probe function usesObject.getOwnPropertyNames() andObject.getOwnPropertySymbols() to enumerate the members of an object through its prototype hierarchy. Using the type list fromMDN the types are detected. After a little formatting the result is written to the console using thearchy package with some colour added bychalk.
  • json(obj, replacer, spacer, color): Usesfast-safe-stringify andjson-colorizer to safely write the stringified object out to the console.
  • yaml(obj, options, indentation): A simple wrapper around theprettyjson package render function.
  • ls(obj): A simple wrapper around thejsome 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 eithernull orundefined will write[console-probe] Invalid Type: to the console.
  • String values with newline characters are stripped from string stubs.

Example:

constcp=require('console-probe')cp.probe({key:'value'})// Writes the object prototype hierarchy to the console// See above for an example of the output

json Function

Description: This function simply callsfast-safe-stringify and then adds color viajson-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.
  • replacer alters the behavior of the stringification process.
  • spacer inserts white space into the output JSON string for readability purposes.
  • color enables customization of the colour displayed.

Details:

Example:

constcp=require('console-probe')cp.json({key:'value'})// Outputs the following to the console:// {//   "key": "value"// }

yaml Function

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

Signature:yaml(object, options, indentation)

Parameter:

  • object can be any object you wish to display inYAML format.
  • options should hold options for theprettyjson render function.
  • indentation controls the indentation for the YAML output.

Details:

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

Example:

constcp=require('console-probe')cp.yaml({key:'value'})// Outputs the following to the console:// key: value

ls Function

Description: This function wraps thejsome 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:

  • Thels function is simply a wrapper around thejsome package.
  • See thejsome documentation for more detail.

Example:

constcp=require('console-probe')cp.ls({key:'value'})// Outputs the following to the console:// {//   key: "value"// }

apply Function

Signature:apply(object)

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

Details:

  • Theapply function is a convenience method to add theconsole-probe functions to an object.
  • If noobject is passed to theapply function then theconsole-probe functions will be added to theconsole object.
  • Passing an object, such as a logger, will add theconsole-probe functions to the object.

Example:

constcp=require('console-probe')cp.apply()// console now has a probe, json, yaml, and ls functions.constfoo={}cp.apply(foo)// foo now has a probe, json, yaml, and ls functions.

Another approach to simply augment the console:

require('console-probe').apply()// console.probe, console.json, console.yaml, and console.ls are now ready for use.

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.

Patreon Donation

See myother projects on NPM.

Contributing

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

Change Log

  • v3.3.2 [2019-10-14]: Added 'name' to the name getter.
  • v3.3.1 [2019-10-14]: Fixed Getter support (#5). Removed NSP from readme.
  • v3.3.0 [2018-07-05]: Added new methodls. Fixed README badges. Fixed null/undefined error.
  • v3.2.1 [2018-07-05]: AddedBigInt type support. Updated dependencies.
  • v3.2.0 [2018-03-02]: Multiple type support. Probe format updated.
  • v3.1.0 [2018-02-19]: Added colour to json. Added yaml function.
  • v3.0.0 [2018-02-18]: Added json function. Improved API. Removed newline chrs.
  • v2.0.4 [2018-01-29]: Changed node label format.
  • v2.0.3 [2018-01-26]: Fix example image url.
  • v2.0.2 [2018-01-26]: Added support for arrays and values. Fixed sort.
  • v2.0.1 [2018-01-25]: Added repository url to package.json.
  • v2.0.0 [2018-01-24]: Changed API. Improved type support.
  • v1.0.2 [2018-01-23]: Updated Readme.
  • v1.0.1 [2018-01-23]: Updated NSP link.
  • v1.0.0 [2018-01-23]: Initial release.

About

Inspect JavaScript object methods and properties in the console.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp