README.md (5619B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # truncateMiddle 22 23 > Truncate a string in the middle to a specified length. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var truncateMiddle = require( '@stdlib/string/truncate-middle' ); 41 ``` 42 43 #### truncate( str, len\[, seq] ) 44 45 Truncates a string in the middle to a specified length. 46 47 ```javascript 48 var out = truncateMiddle( 'beep boop', 7 ); 49 // returns 'be...op' 50 ``` 51 52 By default, the truncated string uses the replacement sequence `'...'`. To customize the replacement sequence, provide a `seq` argument: 53 54 ```javascript 55 var out = truncateMiddle( 'beep boop', 7, '!' ); 56 // returns 'bee!oop' 57 58 out = truncateMiddle( 'beep boop', 7, '!!!' ); 59 // returns 'be!!!op' 60 ``` 61 62 </section> 63 64 <!-- /.usage --> 65 66 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 67 68 <section class="notes"> 69 70 </section> 71 72 <!-- /.notes --> 73 74 <!-- Package usage examples. --> 75 76 <section class="examples"> 77 78 ## Examples 79 80 <!-- eslint no-undef: "error" --> 81 82 ```javascript 83 var truncateMiddle = require( '@stdlib/string/truncate-middle' ); 84 85 var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; 86 var out = truncateMiddle( str, 15 ); 87 // returns 'Lorem ... elit.' 88 89 str = 'To be or not to be, that is the question'; 90 out = truncateMiddle( str, 19, '|' ); 91 // returns 'To be or | question' 92 93 str = 'The quick fox jumps over the lazy dog.'; 94 out = truncateMiddle( str, 28, '...' ); 95 // returns 'The quick fox...he lazy dog.' 96 97 str = '🐺 Wolf Brothers 🐺'; 98 out = truncateMiddle( str, 7 ); 99 // returns '🐺 ... 🐺' 100 101 str = '🐺 Wolf Pack 🐺'; 102 out = truncateMiddle( str, 7, '🐺🐺🐺' ); 103 // returns '🐺 🐺🐺🐺 🐺' 104 ``` 105 106 </section> 107 108 <!-- /.examples --> 109 110 <!-- Section for describing a command-line interface. --> 111 112 * * * 113 114 <section class="cli"> 115 116 ## CLI 117 118 <!-- CLI usage documentation. --> 119 120 <section class="usage"> 121 122 ### Usage 123 124 ```text 125 Usage: truncate-middle [options] [<string>] --len <length> 126 127 Options: 128 129 -h, --help Print this message. 130 -V, --version Print the package version. 131 --len length String length. 132 --seq str Custom replacement sequence. Default: '...'. 133 --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. 134 ``` 135 136 </section> 137 138 <!-- /.usage --> 139 140 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 141 142 <section class="notes"> 143 144 ### Notes 145 146 - If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. 147 148 ```bash 149 # Not escaped... 150 $ echo -n $'Hello, World!\nBeep Boop Baz' | truncate-middle --len 6 --split /\r?\n/ 151 152 # Escaped... 153 $ echo -n $'Hello, World!\nBeep Boop Baz' | truncate-middle --len 6 --split /\\r?\\n/ 154 ``` 155 156 - The implementation ignores trailing delimiters. 157 158 </section> 159 160 <!-- /.notes --> 161 162 <!-- CLI usage examples. --> 163 164 <section class="examples"> 165 166 ### Examples 167 168 ```bash 169 $ truncate-middle 'Hello, World!' --len 8 170 Hel...d! 171 172 $ truncate-middle 'Hello, World!' --len 6 --seq '!' 173 Hel|d! 174 ``` 175 176 To use as a [standard stream][standard-streams], 177 178 ```bash 179 $ echo -n 'Hello, World!' | truncate-middle --len 8 180 Hel...d! 181 ``` 182 183 By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. 184 185 ```bash 186 $ echo -n 'Hello, World!\tBeep Boop' | truncate-middle --split '\t' --len 6 --seq '!' 187 Hel|d! 188 Bee|op 189 ``` 190 191 </section> 192 193 <!-- /.examples --> 194 195 </section> 196 197 <!-- /.cli --> 198 199 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 200 201 <section class="references"> 202 203 </section> 204 205 <!-- /.references --> 206 207 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 208 209 <section class="related"> 210 211 * * * 212 213 ## See Also 214 215 - <span class="package-name">[`@stdlib/string/truncate`][@stdlib/string/truncate]</span><span class="delimiter">: </span><span class="description">truncate a string to a specified length.</span> 216 217 </section> 218 219 <!-- /.related --> 220 221 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 222 223 <section class="links"> 224 225 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 226 227 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 228 229 <!-- <related-links> --> 230 231 [@stdlib/string/truncate]: https://github.com/stdlib-js/string/tree/main/truncate 232 233 <!-- </related-links> --> 234 235 </section> 236 237 <!-- /.links -->