Created
March 28, 2017 16:22
-
-
Save JonathanTech/80b97d0bfb2d9c50e0ef54d66912a9fa to your computer and use it in GitHub Desktop.
Used for the presentation of Node.js debugging features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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