main.js (3580B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 24 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 25 var numGraphemeClusters = require( './../../num-grapheme-clusters' ); 26 var nextGraphemeClusterBreak = require( './../../next-grapheme-cluster-break' ); 27 var format = require( './../../format' ); 28 var round = require( '@stdlib/math/base/special/round' ); 29 var floor = require( '@stdlib/math/base/special/floor' ); 30 31 32 // MAIN // 33 34 /** 35 * Truncates a string in the middle to a specified length. 36 * 37 * @param {string} str - input string 38 * @param {integer} len - output string length (including sequence) 39 * @param {string} [seq='...'] - custom replacement sequence 40 * @throws {TypeError} first argument must be a string 41 * @throws {TypeError} second argument must be a nonnegative integer 42 * @throws {TypeError} third argument must be a string 43 * @returns {string} truncated string 44 * 45 * @example 46 * var str = 'beep boop'; 47 * var out = truncateMiddle( str, 5 ); 48 * // returns 'b...p' 49 * 50 * @example 51 * var str = 'beep boop'; 52 * var out = truncateMiddle( str, 5, '>>>' ); 53 * // returns 'b>>>p' 54 * 55 * @example 56 * var str = 'beep boop'; 57 * var out = truncateMiddle( str, 10 ); 58 * // returns 'beep boop' 59 * 60 * @example 61 * var str = 'beep boop'; 62 * var out = truncateMiddle( str, 0 ); 63 * // returns '' 64 * 65 * @example 66 * var str = 'beep boop'; 67 * var out = truncateMiddle( str, 2 ); 68 * // returns '..' 69 * 70 * @example 71 * var str = '🐺 Wolf Brothers 🐺'; 72 * var out = truncateMiddle( str, 7 ); 73 * // returns '🐺 ... 🐺' 74 */ 75 function truncateMiddle( str, len, seq ) { 76 var seqLength; 77 var fromIndex; 78 var strLength; 79 var seqStart; 80 var nVisual; 81 var seqEnd; 82 var idx2; 83 var idx1; 84 if ( !isString( str ) ) { 85 throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); 86 } 87 if ( !isNonNegativeInteger( len ) ) { 88 throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', len ) ); 89 } 90 if ( arguments.length > 2 ) { 91 if ( !isString( seq ) ) { 92 throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', seq ) ); 93 } 94 } 95 seq = seq || '...'; 96 seqLength = numGraphemeClusters( seq ); 97 strLength = numGraphemeClusters( str ); 98 fromIndex = 0; 99 if ( len > strLength ) { 100 return str; 101 } 102 if ( len - seqLength < 0 ) { 103 return seq.slice( 0, len ); 104 } 105 seqStart = round( ( len - seqLength ) / 2 ); 106 seqEnd = strLength - floor( ( len - seqLength ) / 2 ); 107 nVisual = 0; 108 while ( nVisual < seqStart ) { 109 idx1 = nextGraphemeClusterBreak( str, fromIndex ); 110 fromIndex = idx1; 111 nVisual += 1; 112 } 113 idx2 = idx1; 114 while ( idx2 > 0 ) { 115 idx2 = nextGraphemeClusterBreak( str, fromIndex ); 116 if ( idx2 >= seqEnd + fromIndex - nVisual ) { 117 break; 118 } 119 fromIndex = idx2; 120 nVisual += 1; 121 } 122 return str.substring( 0, idx1 ) + seq + str.substring( idx2 ); 123 } 124 125 126 // EXPORTS // 127 128 module.exports = truncateMiddle;