text.js (934B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 /** 6 * Utils for keyboard command strings 7 * 8 * @module utils/text 9 */ 10 11 /** 12 * Truncates the received text to the maxLength in the format: 13 * Original: 'this is a very long text and ends here' 14 * Truncated: 'this is a ver...and ends here' 15 * 16 * @param {string} sourceText - Source text 17 * @param {number} maxLength - Max allowed length 18 * @memberof utils/text 19 * @static 20 */ 21 export function truncateMiddleText(sourceText, maxLength) { 22 let truncatedText = sourceText; 23 if (sourceText.length > maxLength) { 24 truncatedText = `${sourceText.substring( 25 0, 26 Math.round(maxLength / 2) - 2 27 )}…${sourceText.substring( 28 sourceText.length - Math.round(maxLength / 2 - 1) 29 )}`; 30 } 31 return truncatedText; 32 }