README.md (3756B)
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 # Repeat 22 23 > Repeat a string a specified number of times and return the concatenated result. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var repeat = require( '@stdlib/string/repeat' ); 31 ``` 32 33 #### repeat( str, n ) 34 35 Repeats a string `n` times and returns the concatenated result. 36 37 ```javascript 38 var str = repeat( 'a', 5 ); 39 // returns 'aaaaa' 40 41 str = repeat( '', 100 ); 42 // returns '' 43 44 str = repeat( 'beep', 0 ); 45 // returns '' 46 ``` 47 48 </section> 49 50 <!-- /.usage --> 51 52 <section class="examples"> 53 54 ## Examples 55 56 <!-- eslint no-undef: "error" --> 57 58 ```javascript 59 var round = require( '@stdlib/math/base/special/round' ); 60 var randu = require( '@stdlib/random/base/randu' ); 61 var repeat = require( '@stdlib/string/repeat' ); 62 63 var str = 'beep'; 64 var n; 65 var i; 66 67 for ( i = 0; i < 100; i++ ) { 68 n = round( randu()*3.0 ); 69 console.log( repeat( str, n ) ); 70 } 71 ``` 72 73 </section> 74 75 <!-- /.examples --> 76 77 * * * 78 79 <section class="cli"> 80 81 ## CLI 82 83 <section class="usage"> 84 85 ### Usage 86 87 ```text 88 Usage: repstr [options] [<string>] --n <repeats> 89 90 Options: 91 92 -h, --help Print this message. 93 -V, --version Print the package version. 94 --n repeats Number of repetitions. 95 ``` 96 97 </section> 98 99 <!-- /.usage --> 100 101 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 102 103 <section class="notes"> 104 105 ### Notes 106 107 - If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. 108 109 ```bash 110 # Not escaped... 111 $ echo -n $'beep\nboop' | repstr --n 3 --split /\r?\n/ 112 113 # Escaped... 114 $ echo -n $'beep\nboop' | repstr --n 3 --split /\\r?\\n/ 115 ``` 116 117 - The implementation ignores trailing delimiters. 118 119 </section> 120 121 <!-- /.notes --> 122 123 <section class="examples"> 124 125 ### Examples 126 127 ```bash 128 $ repstr beep --n 5 129 beepbeepbeepbeepbeep 130 ``` 131 132 To use as a [standard stream][standard-streams], 133 134 ```bash 135 $ echo -n $'ab' | repstr --n 3 136 ababab 137 ``` 138 139 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. 140 141 ```bash 142 $ echo -n $'beep\tboop' | repstr --n 3 --split '\t' 143 beepbeepbeep 144 boopboopboop 145 ``` 146 147 </section> 148 149 <!-- /.examples --> 150 151 </section> 152 153 <!-- /.cli --> 154 155 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 156 157 <section class="related"> 158 159 * * * 160 161 ## See Also 162 163 - <span class="package-name">[`@stdlib/string/pad`][@stdlib/string/pad]</span><span class="delimiter">: </span><span class="description">pad a string.</span> 164 165 </section> 166 167 <!-- /.related --> 168 169 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 170 171 <section class="links"> 172 173 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 174 175 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 176 177 <!-- <related-links> --> 178 179 [@stdlib/string/pad]: https://github.com/stdlib-js/string/tree/main/pad 180 181 <!-- </related-links> --> 182 183 </section> 184 185 <!-- /.links -->