Skip to content

Instantly share code, notes, and snippets.

@JonathanTech
Created March 28, 2017 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanTech/80b97d0bfb2d9c50e0ef54d66912a9fa to your computer and use it in GitHub Desktop.
Save JonathanTech/80b97d0bfb2d9c50e0ef54d66912a9fa to your computer and use it in GitHub Desktop.
Used for the presentation of Node.js debugging features
const util = require('util');
const assert = require('assert');
let complexObject = {complex: {object: {that: {is: ['really', 'deeply', {nested: ':('}]}}}};
debugger;
assert(complexObject !== null)
try{
assert(complexObject === 1)
}
catch (error){
console.log(error)
}
console.log('stringify', JSON.stringify(complexObject, null, 2))
let showHidden = true;
let depth = 2;
console.log('inspect - depth 2', util.inspect(complexObject, showHidden, depth))
console.log('inspect - depth infinite', util.inspect(complexObject, {
showHidden: true,
depth : null
}))
/*
Custom inspect function
*/
class Box {
constructor(value) {
this.value = value;
}
inspect(depth, options) {
if (depth < 0) {
return options.stylize('[Box]', 'special');
}
const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1
});
// Five space padding because that's the size of "Box< ".
const padding = ' '.repeat(5);
const inner = util.inspect(this.value, newOptions).replace(/\n/g, '\n' + padding);
return options.stylize('Box', 'special') + '< ' + inner + ' >';
}
}
const box = new Box(true);
console.log('Box object', util.inspect(box));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment