|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 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 | +# replaceBefore |
| 22 | + |
| 23 | +> Replace the substring before the first occurrence of a specified search string. |
| 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 replaceBefore = require( '@stdlib/string/replace-before' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### replaceBefore( str, search, replacement ) |
| 44 | + |
| 45 | +Replaces the substring before the first occurrence of a specified search string. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var out = replaceBefore( 'beep boop', ' ', 'loop' ); |
| 49 | +// returns 'loop boop' |
| 50 | + |
| 51 | +out = replaceBefore( 'beep boop', 'o', 'bar' ); |
| 52 | +// returns 'baroop' |
| 53 | +``` |
| 54 | + |
| 55 | +</section> |
| 56 | + |
| 57 | +<!-- /.usage --> |
| 58 | + |
| 59 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 60 | + |
| 61 | +<section class="notes"> |
| 62 | + |
| 63 | +## Notes |
| 64 | + |
| 65 | +- If a search string is not present in a provided string, the function returns the provided string unchanged. |
| 66 | +- If a search string is an empty string, the function returns the provided string unchanged. |
| 67 | + |
| 68 | +</section> |
| 69 | + |
| 70 | +<!-- /.notes --> |
| 71 | + |
| 72 | +<!-- Package usage examples. --> |
| 73 | + |
| 74 | +<section class="examples"> |
| 75 | + |
| 76 | +## Examples |
| 77 | + |
| 78 | +<!-- eslint no-undef: "error" --> |
| 79 | + |
| 80 | +```javascript |
| 81 | +var replaceBefore = require( '@stdlib/string/replace-before' ); |
| 82 | + |
| 83 | +var out = replaceBefore( 'beep boop', 'p', 'see' ); |
| 84 | +// returns 'seep boop' |
| 85 | + |
| 86 | +out = replaceBefore( 'Hello World!', 'xyz', 'foo' ); |
| 87 | +// returns 'Hello World!' |
| 88 | + |
| 89 | +out = replaceBefore( 'Hello World!', '', 'foo' ); |
| 90 | +// returns 'Hello World!' |
| 91 | + |
| 92 | +out = replaceBefore( '', 'xyz', 'foo'); |
| 93 | +// returns '' |
| 94 | +``` |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.examples --> |
| 99 | + |
| 100 | +<!-- Section for describing a command-line interface. --> |
| 101 | + |
| 102 | +* * * |
| 103 | + |
| 104 | +<section class="cli"> |
| 105 | + |
| 106 | +## CLI |
| 107 | + |
| 108 | +<!-- CLI usage documentation. --> |
| 109 | + |
| 110 | +<section class="usage"> |
| 111 | + |
| 112 | +### Usage |
| 113 | + |
| 114 | +```text |
| 115 | +Usage: replace-before [options] --search=<string> --replacement=<string> [<string>] |
| 116 | +
|
| 117 | +Options: |
| 118 | +
|
| 119 | + -h, --help Print this message. |
| 120 | + -V, --version Print the package version. |
| 121 | + --search string Search string. |
| 122 | + --replacement string Replacement string. |
| 123 | + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. |
| 124 | +``` |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.usage --> |
| 129 | + |
| 130 | +<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 131 | + |
| 132 | +<section class="notes"> |
| 133 | + |
| 134 | +### Notes |
| 135 | + |
| 136 | +- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. |
| 137 | + |
| 138 | + ```bash |
| 139 | + # Not escaped... |
| 140 | + $ echo -n $'foo\nbar\nbaz' | replace-before --search a --replacement b --split /\r?\n/ |
| 141 | + |
| 142 | + # Escaped... |
| 143 | + $ echo -n $'foo\nbar\nbaz' | replace-before --search a --replacement b --split /\\r?\\n/ |
| 144 | + ``` |
| 145 | + |
| 146 | +- The implementation ignores trailing delimiters. |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.notes --> |
| 151 | + |
| 152 | +<!-- CLI usage examples. --> |
| 153 | + |
| 154 | +<section class="examples"> |
| 155 | + |
| 156 | +### Examples |
| 157 | + |
| 158 | +```bash |
| 159 | +$ replace-before abcdefg --search d --replacement pqr |
| 160 | +pqrdefg |
| 161 | +``` |
| 162 | + |
| 163 | +To use as a [standard stream][standard-streams], |
| 164 | + |
| 165 | +```bash |
| 166 | +$ echo -n $'beep\nboop' | replace-before --search p --replacement see |
| 167 | +seep |
| 168 | +seep |
| 169 | +``` |
| 170 | + |
| 171 | +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. |
| 172 | + |
| 173 | +```bash |
| 174 | +$ echo -n 'beep\tboop' | replace-before --search p --replacement see --split '\t' |
| 175 | +seep |
| 176 | +seep |
| 177 | +``` |
| 178 | + |
| 179 | +</section> |
| 180 | + |
| 181 | +<!-- /.examples --> |
| 182 | + |
| 183 | +</section> |
| 184 | + |
| 185 | +<!-- /.cli --> |
| 186 | + |
| 187 | +<!-- 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. --> |
| 188 | + |
| 189 | +<section class="references"> |
| 190 | + |
| 191 | +</section> |
| 192 | + |
| 193 | +<!-- /.references --> |
| 194 | + |
| 195 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 196 | + |
| 197 | +<section class="related"> |
| 198 | + |
| 199 | +</section> |
| 200 | + |
| 201 | +<!-- /.related --> |
| 202 | + |
| 203 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 204 | + |
| 205 | +<section class="links"> |
| 206 | + |
| 207 | +[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams |
| 208 | + |
| 209 | +[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions |
| 210 | + |
| 211 | +</section> |
| 212 | + |
| 213 | +<!-- /.links --> |
0 commit comments