| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a0074e2 commit a3f6854
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -286,13 +286,31 @@ invoke and use the result of when inspecting the object: | |||
| 286 | 286 | ```js | |
| 287 | 287 | const util = require('util'); | |
| 288 | 288 | ||
| 289 | - const obj = { name: 'nate' }; | ||
| 290 | - obj[util.inspect.custom] = function(depth) { | ||
| 291 | - return `{${this.name}}`; | ||
| 292 | - }; | ||
| 289 | + class Box { | ||
| 290 | + constructor(value) { | ||
| 291 | + this.value = value; | ||
| 292 | + } | ||
| 293 | 293 | ||
| 294 | - util.inspect(obj); | ||
| 295 | - // "{nate}" | ||
| 294 | + inspect(depth, options) { | ||
| 295 | + if (depth < 0) { | ||
| 296 | + return options.stylize('[Box]', 'special'); | ||
| 297 | + } | ||
| 298 | + | ||
| 299 | + const newOptions = Object.assign({}, options, { | ||
| 300 | + depth: options.depth === null ? null : options.depth - 1 | ||
| 301 | + }); | ||
| 302 | + | ||
| 303 | + // Five space padding because that's the size of "Box< ". | ||
| 304 | + const padding = ' '.repeat(5); | ||
| 305 | + const inner = util.inspect(this.value, newOptions).replace(/\n/g, '\n' + padding); | ||
| 306 | + return options.stylize('Box', 'special') + '< ' + inner + ' >'; | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + | ||
| 310 | + const box = new Box(true); | ||
| 311 | + | ||
| 312 | + util.inspect(box); | ||
| 313 | + // "Box< true >" | ||
| 296 | 314 | ``` | |
| 297 | 315 | ||
| 298 | 316 | Custom `[util.inspect.custom](depth, opts)` functions typically return a string | |
| Back | FazBrowse Home | New Git URL |
0 commit comments