string.js (581B)
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 "use strict"; 6 7 /** 8 * Truncate the string and add ellipsis to the middle of the string. 9 */ 10 function truncateString(str, maxLength) { 11 if (!str || str.length <= maxLength) { 12 return str; 13 } 14 15 return ( 16 str.substring(0, Math.ceil(maxLength / 2)) + 17 "…" + 18 str.substring(str.length - Math.floor(maxLength / 2)) 19 ); 20 } 21 22 exports.truncateString = truncateString;