| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * Returns sum of all numbers in array.
|
|---|
| 10 | * @param {number[]} array array of numbers
|
|---|
| 11 | * @returns {number} sum of all numbers in array
|
|---|
| 12 | */
|
|---|
| 13 | const arraySum = (array) => {
|
|---|
| 14 | let sum = 0;
|
|---|
| 15 | for (const item of array) sum += item;
|
|---|
| 16 | return sum;
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Returns truncated args.
|
|---|
| 21 | * @param {string[]} args items to be truncated
|
|---|
| 22 | * @param {number} maxLength maximum length of args including spaces between
|
|---|
| 23 | * @returns {string[]} truncated args
|
|---|
| 24 | */
|
|---|
| 25 | const truncateArgs = (args, maxLength) => {
|
|---|
| 26 | const lengths = args.map((a) => `${a}`.length);
|
|---|
| 27 | const availableLength = maxLength - lengths.length + 1;
|
|---|
| 28 |
|
|---|
| 29 | if (availableLength > 0 && args.length === 1) {
|
|---|
| 30 | if (availableLength >= args[0].length) {
|
|---|
| 31 | return args;
|
|---|
| 32 | } else if (availableLength > 3) {
|
|---|
| 33 | return [`...${args[0].slice(-availableLength + 3)}`];
|
|---|
| 34 | }
|
|---|
| 35 | return [args[0].slice(-availableLength)];
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | // Check if there is space for at least 4 chars per arg
|
|---|
| 39 | if (availableLength < arraySum(lengths.map((i) => Math.min(i, 6)))) {
|
|---|
| 40 | // remove args
|
|---|
| 41 | if (args.length > 1) return truncateArgs(args.slice(0, -1), maxLength);
|
|---|
| 42 | return [];
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | let currentLength = arraySum(lengths);
|
|---|
| 46 |
|
|---|
| 47 | // Check if all fits into maxLength
|
|---|
| 48 | if (currentLength <= availableLength) return args;
|
|---|
| 49 |
|
|---|
| 50 | // Try to remove chars from the longest items until it fits
|
|---|
| 51 | while (currentLength > availableLength) {
|
|---|
| 52 | const maxLength = Math.max(...lengths);
|
|---|
| 53 | const shorterItems = lengths.filter((l) => l !== maxLength);
|
|---|
| 54 | const nextToMaxLength =
|
|---|
| 55 | shorterItems.length > 0 ? Math.max(...shorterItems) : 0;
|
|---|
| 56 | const maxReduce = maxLength - nextToMaxLength;
|
|---|
| 57 | let maxItems = lengths.length - shorterItems.length;
|
|---|
| 58 | let overrun = currentLength - availableLength;
|
|---|
| 59 | for (let i = 0; i < lengths.length; i++) {
|
|---|
| 60 | if (lengths[i] === maxLength) {
|
|---|
| 61 | const reduce = Math.min(Math.floor(overrun / maxItems), maxReduce);
|
|---|
| 62 | lengths[i] -= reduce;
|
|---|
| 63 | currentLength -= reduce;
|
|---|
| 64 | overrun -= reduce;
|
|---|
| 65 | maxItems--;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | // Return args reduced to length in lengths
|
|---|
| 71 | return args.map((a, i) => {
|
|---|
| 72 | const str = `${a}`;
|
|---|
| 73 | const length = lengths[i];
|
|---|
| 74 | if (str.length === length) {
|
|---|
| 75 | return str;
|
|---|
| 76 | } else if (length > 5) {
|
|---|
| 77 | return `...${str.slice(-length + 3)}`;
|
|---|
| 78 | } else if (length > 0) {
|
|---|
| 79 | return str.slice(-length);
|
|---|
| 80 | }
|
|---|
| 81 | return "";
|
|---|
| 82 | });
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | module.exports = truncateArgs;
|
|---|