| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2025-08-28 18:34:30 +08:00
										 |  |  |  * @param {number[]} array array of numbers | 
					
						
							| 
									
										
										
										
											2023-06-17 06:33:17 +08:00
										 |  |  |  * @returns {number} sum of all numbers in array | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | const arraySum = (array) => { | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 	let sum = 0; | 
					
						
							|  |  |  | 	for (const item of array) sum += item; | 
					
						
							|  |  |  | 	return sum; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-10-25 02:13:59 +08:00
										 |  |  |  * @param {EXPECTED_ANY[]} args items to be truncated | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  |  * @param {number} maxLength maximum length of args including spaces between | 
					
						
							|  |  |  |  * @returns {string[]} truncated args | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const truncateArgs = (args, maxLength) => { | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	const lengths = args.map((a) => `${a}`.length); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 	const availableLength = maxLength - lengths.length + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (availableLength > 0 && args.length === 1) { | 
					
						
							| 
									
										
										
										
											2019-08-05 18:15:03 +08:00
										 |  |  | 		if (availableLength >= args[0].length) { | 
					
						
							|  |  |  | 			return args; | 
					
						
							|  |  |  | 		} else if (availableLength > 3) { | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 			return [`...${args[0].slice(-availableLength + 3)}`]; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 		return [args[0].slice(-availableLength)]; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Check if there is space for at least 4 chars per arg
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 	if (availableLength < arraySum(lengths.map((i) => Math.min(i, 6)))) { | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		// remove args
 | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 		if (args.length > 1) return truncateArgs(args.slice(0, -1), maxLength); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		return []; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 01:54:56 +08:00
										 |  |  | 	let currentLength = arraySum(lengths); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Check if all fits into maxLength
 | 
					
						
							|  |  |  | 	if (currentLength <= availableLength) return args; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Try to remove chars from the longest items until it fits
 | 
					
						
							|  |  |  | 	while (currentLength > availableLength) { | 
					
						
							|  |  |  | 		const maxLength = Math.max(...lengths); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		const shorterItems = lengths.filter((l) => l !== maxLength); | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		const nextToMaxLength = | 
					
						
							|  |  |  | 			shorterItems.length > 0 ? Math.max(...shorterItems) : 0; | 
					
						
							|  |  |  | 		const maxReduce = maxLength - nextToMaxLength; | 
					
						
							|  |  |  | 		let maxItems = lengths.length - shorterItems.length; | 
					
						
							|  |  |  | 		let overrun = currentLength - availableLength; | 
					
						
							|  |  |  | 		for (let i = 0; i < lengths.length; i++) { | 
					
						
							|  |  |  | 			if (lengths[i] === maxLength) { | 
					
						
							|  |  |  | 				const reduce = Math.min(Math.floor(overrun / maxItems), maxReduce); | 
					
						
							|  |  |  | 				lengths[i] -= reduce; | 
					
						
							|  |  |  | 				currentLength -= reduce; | 
					
						
							|  |  |  | 				overrun -= reduce; | 
					
						
							|  |  |  | 				maxItems--; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return args reduced to length in lengths
 | 
					
						
							|  |  |  | 	return args.map((a, i) => { | 
					
						
							|  |  |  | 		const str = `${a}`; | 
					
						
							|  |  |  | 		const length = lengths[i]; | 
					
						
							|  |  |  | 		if (str.length === length) { | 
					
						
							|  |  |  | 			return str; | 
					
						
							| 
									
										
										
										
											2019-08-01 14:18:45 +08:00
										 |  |  | 		} else if (length > 5) { | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 			return `...${str.slice(-length + 3)}`; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 		} else if (length > 0) { | 
					
						
							|  |  |  | 			return str.slice(-length); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 		return ""; | 
					
						
							| 
									
										
										
										
											2019-07-28 01:48:10 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = truncateArgs; |