| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 15a32dd commit 3b8914d
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -187,6 +187,9 @@ stream.write('It works!'); // Received data: "It works!" | |||
| 187 | 187 | `TypedArray` elements to include when formatting. Defaults to `100`. Set to | |
| 188 | 188 | `null` to show all array elements. Set to `0` or negative to show no array | |
| 189 | 189 | elements. | |
| 190 | + * `breakLength` {number} The length at which an object's keys are split | ||
| 191 | + across multiple lines. Set to `Infinity` to format an object as a single | ||
| 192 | + line. Defaults to 60 for legacy compatibility. | ||
| 190 | 193 | ||
| 191 | 194 | The `util.inspect()` method returns a string representation of `object` that is | |
| 192 | 195 | primarily useful for debugging. Additional `options` may be passed that alter | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,6 +194,7 @@ function inspect(obj, opts) { | |||
| 194 | 194 | if (ctx.colors) ctx.stylize = stylizeWithColor; | |
| 195 | 195 | if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength; | |
| 196 | 196 | if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity; | |
| 197 | + if (ctx.breakLength === undefined) ctx.breakLength = 60; | ||
| 197 | 198 | return formatValue(ctx, obj, ctx.depth); | |
| 198 | 199 | } | |
| 199 | 200 | exports.inspect = inspect; | |
@@ -575,7 +576,7 @@ function formatValue(ctx, value, recurseTimes) { | |||
| 575 | 576 | ||
| 576 | 577 | ctx.seen.pop(); | |
| 577 | 578 | ||
| 578 | - return reduceToSingleString(output, base, braces); | ||
| 579 | + return reduceToSingleString(output, base, braces, ctx.breakLength); | ||
| 579 | 580 | } | |
| 580 | 581 | ||
| 581 | 582 | ||
@@ -807,12 +808,12 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { | |||
| 807 | 808 | } | |
| 808 | 809 | ||
| 809 | 810 | ||
| 810 | - function reduceToSingleString(output, base, braces) { | ||
| 811 | + function reduceToSingleString(output, base, braces, breakLength) { | ||
| 811 | 812 | var length = output.reduce(function(prev, cur) { | |
| 812 | 813 | return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; | |
| 813 | 814 | }, 0); | |
| 814 | 815 | ||
| 815 | - if (length > 60) { | ||
| 816 | + if (length > breakLength) { | ||
| 816 | 817 | return braces[0] + | |
| 817 | 818 | // If the opening "brace" is too large, like in the case of "Set {", | |
| 818 | 819 | // we need to force the first item to be on the next line or the | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -709,3 +709,16 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; }))); | |||
| 709 | 709 | const x = new Uint8Array(101); | |
| 710 | 710 | assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity}))); | |
| 711 | 711 | } | |
| 712 | + | ||
| 713 | + { | ||
| 714 | + const obj = {foo: 'abc', bar: 'xyz'}; | ||
| 715 | + const oneLine = util.inspect(obj, {breakLength: Infinity}); | ||
| 716 | + // Subtract four for the object's two curly braces and two spaces of padding. | ||
| 717 | + // Add one more to satisfy the strictly greater than condition in the code. | ||
| 718 | + const breakpoint = oneLine.length - 5; | ||
| 719 | + const twoLines = util.inspect(obj, {breakLength: breakpoint}); | ||
| 720 | + | ||
| 721 | + assert.strictEqual(oneLine, '{ foo: \'abc\', bar: \'xyz\' }'); | ||
| 722 | + assert.strictEqual(oneLine, util.inspect(obj, {breakLength: breakpoint + 1})); | ||
| 723 | + assert.strictEqual(twoLines, '{ foo: \'abc\',\n bar: \'xyz\' }'); | ||
| 724 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments