README.md (4536B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # Trim 22 23 > Trim whitespace characters from the beginning and end of a string. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var trim = require( '@stdlib/string/trim' ); 31 ``` 32 33 #### trim( str ) 34 35 Trims whitespace characters from the beginning and end of a `string`. 36 37 ```javascript 38 var out = trim( ' \t\t\n Beep \r\n\t ' ); 39 // returns 'Beep' 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ## Notes 49 50 - Following [Unicode 6.3.0][unicode] and later, "whitespace" is defined as the following characters: `[ \\f\\n\\r\\t\\v\\u0020\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]`. 51 52 </section> 53 54 <!-- /.notes --> 55 56 <section class="examples"> 57 58 ## Examples 59 60 <!-- eslint no-undef: "error" --> 61 62 ```javascript 63 var trim = require( '@stdlib/string/trim' ); 64 65 var out = trim( ' Whitespace ' ); 66 // returns 'Whitespace' 67 68 out = trim( '\t\t\tTabs\t\t\t' ); 69 // returns 'Tabs' 70 71 out = trim( '\n\n\nNew Lines\n\n\n' ); 72 // returns 'New Lines' 73 ``` 74 75 </section> 76 77 <!-- /.examples --> 78 79 * * * 80 81 <section class="cli"> 82 83 ## CLI 84 85 <section class="usage"> 86 87 ### Usage 88 89 ```text 90 Usage: trim [options] [<string>] 91 92 Options: 93 94 -h, --help Print this message. 95 -V, --version Print the package version. 96 --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. 97 ``` 98 99 </section> 100 101 <!-- /.usage --> 102 103 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 104 105 <section class="notes"> 106 107 ### Notes 108 109 - If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. 110 111 ```bash 112 # Not escaped... 113 $ echo -n $' foo \n bar ' | trim --split /\r?\n/ 114 115 # Escaped... 116 $ echo -n $' foo \n bar ' | trim --split /\\r?\\n/ 117 ``` 118 119 - The implementation ignores trailing delimiters. 120 121 </section> 122 123 <!-- /.notes --> 124 125 <section class="examples"> 126 127 ### Examples 128 129 ```bash 130 $ trim ' beep boop ' 131 beep boop 132 ``` 133 134 To use as a [standard stream][standard-streams], 135 136 ```bash 137 $ echo -n ' beep boop ' | trim 138 beep boop 139 ``` 140 141 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. 142 143 ```bash 144 $ echo -n ' foo \t bar \t baz ' | trim --split '\t' 145 foo 146 bar 147 baz 148 ``` 149 150 </section> 151 152 <!-- /.examples --> 153 154 </section> 155 156 <!-- /.cli --> 157 158 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 159 160 <section class="related"> 161 162 * * * 163 164 ## See Also 165 166 - <span class="package-name">[`@stdlib/string/left-trim`][@stdlib/string/left-trim]</span><span class="delimiter">: </span><span class="description">trim whitespace characters from the beginning of a string.</span> 167 - <span class="package-name">[`@stdlib/string/pad`][@stdlib/string/pad]</span><span class="delimiter">: </span><span class="description">pad a string.</span> 168 - <span class="package-name">[`@stdlib/string/right-trim`][@stdlib/string/right-trim]</span><span class="delimiter">: </span><span class="description">trim whitespace characters from the end of a string.</span> 169 170 </section> 171 172 <!-- /.related --> 173 174 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 175 176 <section class="links"> 177 178 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 179 180 [unicode]: https://en.wikipedia.org/wiki/Unicode 181 182 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 183 184 <!-- <related-links> --> 185 186 [@stdlib/string/left-trim]: https://github.com/stdlib-js/string/tree/main/left-trim 187 188 [@stdlib/string/pad]: https://github.com/stdlib-js/string/tree/main/pad 189 190 [@stdlib/string/right-trim]: https://github.com/stdlib-js/string/tree/main/right-trim 191 192 <!-- </related-links> --> 193 194 </section> 195 196 <!-- /.links -->