MCPcopy Create free account
hub / github.com/denoland/celld / reduceToSingleString

Function reduceToSingleString

crates/celld/js/node_util.js:2889–2952  ·  view source on GitHub ↗
(ctx, output, base, braces, extrasType, recurseTimes, value)

Source from the content-addressed store, hash-verified

2887 return base === '' || !base.includes('\n');
2888}
2889function reduceToSingleString(ctx, output, base, braces, extrasType, recurseTimes, value) {
2890 if (ctx.compact !== true) {
2891 if (typeof ctx.compact === 'number' && ctx.compact >= 1) {
2892 // Memorize the original output length. In case the output is grouped,
2893 // prevent lining up the entries on a single line.
2894 const entries = output.length;
2895 // Group array elements together if the array contains at least six
2896 // separate entries.
2897 if (extrasType === kArrayExtrasType && entries > 6) {
2898 output = groupArrayElements(ctx, output, value);
2899 }
2900 // `ctx.currentDepth` is set to the most inner depth of the currently
2901 // inspected object part while `recurseTimes` is the actual current depth
2902 // that is inspected.
2903 //
2904 // Example:
2905 //
2906 // const a = { first: [ 1, 2, 3 ], second: { inner: [ 1, 2, 3 ] } }
2907 //
2908 // The deepest depth of `a` is 2 (a.second.inner) and `a.first` has a max
2909 // depth of 1.
2910 //
2911 // Consolidate all entries of the local most inner depth up to
2912 // `ctx.compact`, as long as the properties are smaller than
2913 // `ctx.breakLength`.
2914 if (ctx.currentDepth - recurseTimes < ctx.compact &&
2915 entries === output.length) {
2916 // Line up all entries on a single line in case the entries do not
2917 // exceed `breakLength`. Add 10 as constant to start next to all other
2918 // factors that may reduce `breakLength`.
2919 const start = output.length +
2920 ctx.indentationLvl +
2921 braces[0].length +
2922 base.length +
2923 10;
2924 if (isBelowBreakLength(ctx, output, start, base)) {
2925 const joinedOutput = output.join(', ');
2926 if (!joinedOutput.includes('\n')) {
2927 return (`${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` +
2928 ` ${braces[1]}`);
2929 }
2930 }
2931 }
2932 }
2933 // Line up each entry on an individual line.
2934 const indentation = `\n${' '.repeat(ctx.indentationLvl)}`;
2935 return (`${base ? `${base} ` : ''}${braces[0]}${indentation} ` +
2936 `${output.join(`,${indentation} `)}${indentation}${braces[1]}`);
2937 }
2938 // Line up all entries on a single line in case the entries do not exceed
2939 // `breakLength`.
2940 if (isBelowBreakLength(ctx, output, 0, base)) {
2941 return (`${braces[0]}${base ? ` ${base}` : ''} ${output.join(', ')} ` + braces[1]);
2942 }
2943 const indentation = ' '.repeat(ctx.indentationLvl);
2944 // If the opening "brace" is too large, like in the case of "Set {",
2945 // we need to force the first item to be on the next line or the
2946 // items will not line up correctly.

Callers 3

formatProxyFunction · 0.85
formatRawFunction · 0.85
formatMapIterInnerFunction · 0.85

Calls 4

groupArrayElementsFunction · 0.85
isBelowBreakLengthFunction · 0.85
joinMethod · 0.80
includesMethod · 0.80

Tested by

no test coverage detected