source: imaps-frontend/node_modules/webpack/lib/logging/truncateArgs.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.3 KB
Line 
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 * @param {Array<number>} array array of numbers
10 * @returns {number} sum of all numbers in array
11 */
12const arraySum = array => {
13 let sum = 0;
14 for (const item of array) sum += item;
15 return sum;
16};
17
18/**
19 * @param {EXPECTED_ANY[]} args items to be truncated
20 * @param {number} maxLength maximum length of args including spaces between
21 * @returns {string[]} truncated args
22 */
23const truncateArgs = (args, maxLength) => {
24 const lengths = args.map(a => `${a}`.length);
25 const availableLength = maxLength - lengths.length + 1;
26
27 if (availableLength > 0 && args.length === 1) {
28 if (availableLength >= args[0].length) {
29 return args;
30 } else if (availableLength > 3) {
31 return [`...${args[0].slice(-availableLength + 3)}`];
32 }
33 return [args[0].slice(-availableLength)];
34 }
35
36 // Check if there is space for at least 4 chars per arg
37 if (availableLength < arraySum(lengths.map(i => Math.min(i, 6)))) {
38 // remove args
39 if (args.length > 1) return truncateArgs(args.slice(0, -1), maxLength);
40 return [];
41 }
42
43 let currentLength = arraySum(lengths);
44
45 // Check if all fits into maxLength
46 if (currentLength <= availableLength) return args;
47
48 // Try to remove chars from the longest items until it fits
49 while (currentLength > availableLength) {
50 const maxLength = Math.max(...lengths);
51 const shorterItems = lengths.filter(l => l !== maxLength);
52 const nextToMaxLength =
53 shorterItems.length > 0 ? Math.max(...shorterItems) : 0;
54 const maxReduce = maxLength - nextToMaxLength;
55 let maxItems = lengths.length - shorterItems.length;
56 let overrun = currentLength - availableLength;
57 for (let i = 0; i < lengths.length; i++) {
58 if (lengths[i] === maxLength) {
59 const reduce = Math.min(Math.floor(overrun / maxItems), maxReduce);
60 lengths[i] -= reduce;
61 currentLength -= reduce;
62 overrun -= reduce;
63 maxItems--;
64 }
65 }
66 }
67
68 // Return args reduced to length in lengths
69 return args.map((a, i) => {
70 const str = `${a}`;
71 const length = lengths[i];
72 if (str.length === length) {
73 return str;
74 } else if (length > 5) {
75 return `...${str.slice(-length + 3)}`;
76 } else if (length > 0) {
77 return str.slice(-length);
78 }
79 return "";
80 });
81};
82
83module.exports = truncateArgs;
Note: See TracBrowser for help on using the repository browser.