| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 33f24c8 commit 91ab769
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -183,6 +183,10 @@ formatted string: | |||
| 183 | 183 | will be introspected to show their `target` and `hander` objects. Defaults to | |
| 184 | 184 | `false`. | |
| 185 | 185 | ||
| 186 | + - `maxArrayLength` - specifies the maximum number of Array and TypedArray | ||
| 187 | + elements to include when formatting. Defaults to `100`. Set to `null` to | ||
| 188 | + show all array elements. Set to `0` or negative to show no array elements. | ||
| 189 | + | ||
| 186 | 190 | Example of inspecting all properties of the `util` object: | |
| 187 | 191 | ||
| 188 | 192 | ```js | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ const internalUtil = require('internal/util'); | |||
| 6 | 6 | const binding = process.binding('util'); | |
| 7 | 7 | ||
| 8 | 8 | const isError = internalUtil.isError; | |
| 9 | + const kDefaultMaxLength = 100; | ||
| 9 | 10 | ||
| 10 | 11 | var Debug; | |
| 11 | 12 | ||
@@ -141,6 +142,8 @@ function inspect(obj, opts) { | |||
| 141 | 142 | if (ctx.customInspect === undefined) ctx.customInspect = true; | |
| 142 | 143 | if (ctx.showProxy === undefined) ctx.showProxy = false; | |
| 143 | 144 | if (ctx.colors) ctx.stylize = stylizeWithColor; | |
| 145 | + if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength; | ||
| 146 | + if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity; | ||
| 144 | 147 | return formatValue(ctx, obj, ctx.depth); | |
| 145 | 148 | } | |
| 146 | 149 | exports.inspect = inspect; | |
@@ -579,14 +582,19 @@ function formatObject(ctx, value, recurseTimes, visibleKeys, keys) { | |||
| 579 | 582 | ||
| 580 | 583 | function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { | |
| 581 | 584 | var output = []; | |
| 582 | - for (var i = 0, l = value.length; i < l; ++i) { | ||
| 585 | + const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); | ||
| 586 | + const remaining = value.length - maxLength; | ||
| 587 | + for (var i = 0; i < maxLength; ++i) { | ||
| 583 | 588 | if (hasOwnProperty(value, String(i))) { | |
| 584 | 589 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, | |
| 585 | 590 | String(i), true)); | |
| 586 | 591 | } else { | |
| 587 | 592 | output.push(''); | |
| 588 | 593 | } | |
| 589 | 594 | } | |
| 595 | + if (remaining > 0) { | ||
| 596 | + output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | ||
| 597 | + } | ||
| 590 | 598 | keys.forEach(function(key) { | |
| 591 | 599 | if (typeof key === 'symbol' || !key.match(/^\d+$/)) { | |
| 592 | 600 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, | |
@@ -598,9 +606,14 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { | |||
| 598 | 606 | ||
| 599 | 607 | ||
| 600 | 608 | function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) { | |
| 601 | - var output = new Array(value.length); | ||
| 602 | - for (var i = 0, l = value.length; i < l; ++i) | ||
| 609 | + const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length); | ||
| 610 | + const remaining = value.length - maxLength; | ||
| 611 | + var output = new Array(maxLength); | ||
| 612 | + for (var i = 0; i < maxLength; ++i) | ||
| 603 | 613 | output[i] = formatNumber(ctx, value[i]); | |
| 614 | + if (remaining > 0) { | ||
| 615 | + output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`); | ||
| 616 | + } | ||
| 604 | 617 | for (const key of keys) { | |
| 605 | 618 | if (typeof key === 'symbol' || !key.match(/^\d+$/)) { | |
| 606 | 619 | output.push( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -648,3 +648,58 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; }))); | |||
| 648 | 648 | const x = Object.create(null); | |
| 649 | 649 | assert.equal(util.inspect(x), '{}'); | |
| 650 | 650 | } | |
| 651 | + | ||
| 652 | + // The following maxArrayLength tests were introduced after v6.0.0 was released. | ||
| 653 | + // Do not backport to v5/v4 unless all of | ||
| 654 | + // https://github.com/nodejs/node/pull/6334 is backported. | ||
| 655 | + { | ||
| 656 | + const x = Array(101); | ||
| 657 | + assert(/1 more item/.test(util.inspect(x))); | ||
| 658 | + } | ||
| 659 | + | ||
| 660 | + { | ||
| 661 | + const x = Array(101); | ||
| 662 | + assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101}))); | ||
| 663 | + } | ||
| 664 | + | ||
| 665 | + { | ||
| 666 | + const x = Array(101); | ||
| 667 | + assert(/^\[ ... 101 more items \]$/.test( | ||
| 668 | + util.inspect(x, {maxArrayLength: 0}))); | ||
| 669 | + } | ||
| 670 | + | ||
| 671 | + { | ||
| 672 | + const x = new Uint8Array(101); | ||
| 673 | + assert(/1 more item/.test(util.inspect(x))); | ||
| 674 | + } | ||
| 675 | + | ||
| 676 | + { | ||
| 677 | + const x = new Uint8Array(101); | ||
| 678 | + assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101}))); | ||
| 679 | + } | ||
| 680 | + | ||
| 681 | + { | ||
| 682 | + const x = new Uint8Array(101); | ||
| 683 | + assert(/\[ ... 101 more items \]$/.test( | ||
| 684 | + util.inspect(x, {maxArrayLength: 0}))); | ||
| 685 | + } | ||
| 686 | + | ||
| 687 | + { | ||
| 688 | + const x = Array(101); | ||
| 689 | + assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null}))); | ||
| 690 | + } | ||
| 691 | + | ||
| 692 | + { | ||
| 693 | + const x = Array(101); | ||
| 694 | + assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity}))); | ||
| 695 | + } | ||
| 696 | + | ||
| 697 | + { | ||
| 698 | + const x = new Uint8Array(101); | ||
| 699 | + assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null}))); | ||
| 700 | + } | ||
| 701 | + | ||
| 702 | + { | ||
| 703 | + const x = new Uint8Array(101); | ||
| 704 | + assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity}))); | ||
| 705 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments