MCPcopy Index your code
hub / github.com/nodejs/node / reduceToSingleString

Function reduceToSingleString

lib/internal/util/inspect.js:2638–2699  ·  view source on GitHub ↗
(
  ctx, output, base, braces, extrasType, recurseTimes, value)

Source from the content-addressed store, hash-verified

2636}
2637
2638function reduceToSingleString(
2639 ctx, output, base, braces, extrasType, recurseTimes, value) {
2640 if (ctx.compact !== true) {
2641 if (typeof ctx.compact === 'number' && ctx.compact >= 1) {
2642 // Memorize the original output length. In case the output is grouped,
2643 // prevent lining up the entries on a single line.
2644 const entries = output.length;
2645 // Group array elements together if the array contains at least six
2646 // separate entries.
2647 if (extrasType === kArrayExtrasType && entries > 6) {
2648 output = groupArrayElements(ctx, output, value);
2649 }
2650 // `ctx.currentDepth` is set to the most inner depth of the currently
2651 // inspected object part while `recurseTimes` is the actual current depth
2652 // that is inspected.
2653 //
2654 // Example:
2655 //
2656 // const a = { first: [ 1, 2, 3 ], second: { inner: [ 1, 2, 3 ] } }
2657 //
2658 // The deepest depth of `a` is 2 (a.second.inner) and `a.first` has a max
2659 // depth of 1.
2660 //
2661 // Consolidate all entries of the local most inner depth up to
2662 // `ctx.compact`, as long as the properties are smaller than
2663 // `ctx.breakLength`.
2664 if (ctx.currentDepth - recurseTimes < ctx.compact &&
2665 entries === output.length) {
2666 // Line up all entries on a single line in case the entries do not
2667 // exceed `breakLength`. Add 10 as constant to start next to all other
2668 // factors that may reduce `breakLength`.
2669 const start = output.length + ctx.indentationLvl +
2670 braces[0].length + base.length + 10;
2671 if (isBelowBreakLength(ctx, output, start, base)) {
2672 const joinedOutput = join(output, ', ');
2673 if (!StringPrototypeIncludes(joinedOutput, '\n')) {
2674 return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
2675 ` ${braces[1]}`;
2676 }
2677 }
2678 }
2679 }
2680 // Line up each entry on an individual line.
2681 const indentation = `\n${StringPrototypeRepeat(' ', ctx.indentationLvl)}`;
2682 return `${base ? `${base} ` : ''}${braces[0]}${indentation} ` +
2683 `${join(output, `,${indentation} `)}${indentation}${braces[1]}`;
2684 }
2685 // Line up all entries on a single line in case the entries do not exceed
2686 // `breakLength`.
2687 if (isBelowBreakLength(ctx, output, 0, base)) {
2688 return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` +
2689 braces[1];
2690 }
2691 const indentation = StringPrototypeRepeat(' ', ctx.indentationLvl);
2692 // If the opening "brace" is too large, like in the case of "Set {",
2693 // we need to force the first item to be on the next line or the
2694 // items will not line up correctly.
2695 const ln = base === '' && braces[0].length === 1 ?

Callers 3

formatProxyFunction · 0.85
formatRawFunction · 0.85
formatMapIterInnerFunction · 0.85

Calls 3

groupArrayElementsFunction · 0.85
isBelowBreakLengthFunction · 0.85
joinFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…