(ctx, output, value)
| 2043 | } |
| 2044 | |
| 2045 | function groupArrayElements(ctx, output, value) { |
| 2046 | let totalLength = 0; |
| 2047 | let maxLength = 0; |
| 2048 | let i = 0; |
| 2049 | let outputLength = output.length; |
| 2050 | if (ctx.maxArrayLength < output.length) { |
| 2051 | // This makes sure the "... n more items" part is not taken into account. |
| 2052 | outputLength--; |
| 2053 | } |
| 2054 | const separatorSpace = 2; // Add 1 for the space and 1 for the separator. |
| 2055 | const dataLen = new Array(outputLength); |
| 2056 | // Calculate the total length of all output entries and the individual max |
| 2057 | // entries length of all output entries. We have to remove colors first, |
| 2058 | // otherwise the length would not be calculated properly. |
| 2059 | for (; i < outputLength; i++) { |
| 2060 | const len = getStringWidth(output[i], ctx.colors); |
| 2061 | dataLen[i] = len; |
| 2062 | totalLength += len + separatorSpace; |
| 2063 | if (maxLength < len) |
| 2064 | maxLength = len; |
| 2065 | } |
| 2066 | // Add two to `maxLength` as we add a single whitespace character plus a comma |
| 2067 | // in-between two entries. |
| 2068 | const actualMax = maxLength + separatorSpace; |
| 2069 | // Check if at least three entries fit next to each other and prevent grouping |
| 2070 | // of arrays that contains entries of very different length (i.e., if a single |
| 2071 | // entry is longer than 1/5 of all other entries combined). Otherwise the |
| 2072 | // space in-between small entries would be enormous. |
| 2073 | if (actualMax * 3 + ctx.indentationLvl < ctx.breakLength && |
| 2074 | (totalLength / actualMax > 5 || maxLength <= 6)) { |
| 2075 | |
| 2076 | const approxCharHeights = 2.5; |
| 2077 | const averageBias = MathSqrt(actualMax - totalLength / output.length); |
| 2078 | const biasedMax = MathMax(actualMax - 3 - averageBias, 1); |
| 2079 | // Dynamically check how many columns seem possible. |
| 2080 | const columns = MathMin( |
| 2081 | // Ideally a square should be drawn. We expect a character to be about 2.5 |
| 2082 | // times as high as wide. This is the area formula to calculate a square |
| 2083 | // which contains n rectangles of size `actualMax * approxCharHeights`. |
| 2084 | // Divide that by `actualMax` to receive the correct number of columns. |
| 2085 | // The added bias increases the columns for short entries. |
| 2086 | MathRound( |
| 2087 | MathSqrt( |
| 2088 | approxCharHeights * biasedMax * outputLength, |
| 2089 | ) / biasedMax, |
| 2090 | ), |
| 2091 | // Do not exceed the breakLength. |
| 2092 | MathFloor((ctx.breakLength - ctx.indentationLvl) / actualMax), |
| 2093 | // Limit array grouping for small `compact` modes as the user requested |
| 2094 | // minimal grouping. |
| 2095 | ctx.compact * 4, |
| 2096 | // Limit the columns to a maximum of fifteen. |
| 2097 | 15, |
| 2098 | ); |
| 2099 | // Return with the original output if no grouping should happen. |
| 2100 | if (columns <= 1) { |
| 2101 | return output; |
| 2102 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…