Skip to content

Commit 04ee89f

Browse files
AuenKrkgryte
andauthored
feat: add string/base/replace-after
Closes: #812 PR-URL: #1363 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Golden <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 5daf468 commit 04ee89f

File tree

10 files changed

+719
-0
lines changed

10 files changed

+719
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# replaceAfter
22+
23+
> Replace the substring after 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 replaceAfter = require( '@stdlib/string/base/replace-after' );
41+
```
42+
43+
#### replaceAfter( str, search, replacement, fromIndex )
44+
45+
Replaces the substring after the first occurrence of a specified search string.
46+
47+
```javascript
48+
var out = replaceAfter( 'beep boop', ' ', 'loop', 0 );
49+
// returns 'beep loop'
50+
51+
out = replaceAfter( 'beep boop', 'o', 'bar', 0 );
52+
// returns 'beep bobar'
53+
54+
out = replaceAfter( 'beep boop beep baz', 'beep', 'foo', 5 );
55+
// return 'beep boop beepfoo'
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
63+
64+
<section class="notes">
65+
66+
## Notes
67+
68+
- If a search string is not present in a provided string, the function returns the provided string unchanged.
69+
- If a search string is an empty string, the function returns the provided string unchanged.
70+
- If `fromIndex` is less than `0` or greater than `str.length`, the search starts at index `0` and `str.length`, respectively.
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var replaceAfter = require( '@stdlib/string/base/replace-after' );
86+
87+
var out = replaceAfter( 'beep boop', 'p', 'see', 0 );
88+
// returns 'beepsee'
89+
90+
out = replaceAfter( 'Hello World!', 'xyz', 'foo', 0 );
91+
// returns 'Hello World!'
92+
93+
out = replaceAfter( 'Hello World!', '', 'foo', 0 );
94+
// returns 'Hello World!'
95+
96+
out = replaceAfter( '', 'xyz', 'foo', 0 );
97+
// returns ''
98+
99+
out = replaceAfter( 'beep boop beep baz', 'beep', 'foo', 5 );
100+
// return 'beep boop beepfoo'
101+
```
102+
103+
</section>
104+
105+
<!-- /.examples -->
106+
107+
<!-- 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. -->
108+
109+
<section class="references">
110+
111+
</section>
112+
113+
<!-- /.references -->
114+
115+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
116+
117+
<section class="related">
118+
119+
</section>
120+
121+
<!-- /.related -->
122+
123+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
124+
125+
<section class="links">
126+
127+
</section>
128+
129+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var replaceAfter = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var str;
35+
var i;
36+
37+
str = 'To be, or not to be, that is the question.';
38+
values = [
39+
'foo',
40+
'bar',
41+
'beep',
42+
'boop'
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
out = replaceAfter( str, '.', values[ i%values.length ], 0 );
48+
if ( typeof out !== 'string' ) {
49+
b.fail( 'should return a string' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isString( out ) ) {
54+
b.fail( 'should return a string' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( str, search, replacement, fromIndex )
3+
Replaces the substring after the first occurrence of a specified search
4+
string.
5+
6+
Parameters
7+
----------
8+
str: string
9+
Input string.
10+
11+
search: string
12+
Search string.
13+
14+
replacement: string
15+
Replacement string.
16+
17+
fromIndex: integer
18+
Index from which to start the search.
19+
20+
Returns
21+
-------
22+
out: string
23+
Output string.
24+
25+
Examples
26+
--------
27+
> var out = {{alias}}( 'beep boop', ' ', 'foo', 0 )
28+
'beep foo'
29+
> out = {{alias}}( 'beep boop', 'o', 'foo', 0 )
30+
'beep bofoo'
31+
> out = {{alias}}( 'Hello World!', 'o', 'foo', 5 )
32+
'Hello Wofoo'
33+
> out = {{alias}}( 'beep boop beep baz', 'beep', 'foo', 5 )
34+
'beep boop beepfoo'
35+
36+
See Also
37+
--------
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Replaces the substring after the first occurrence of a specified search string.
23+
*
24+
* @param str - input string
25+
* @param search - search string
26+
* @param replacement - replacement string
27+
* @param fromIndex - index at which to start the search
28+
* @returns output string
29+
*
30+
* @example
31+
* var out = replaceAfter( 'beep boop', ' ', 'foo', 0 );
32+
* // returns 'beep foo'
33+
*
34+
* @example
35+
* var out = replaceAfter( 'beep boop', 'p', 'foo', 5 );
36+
* // returns 'beep boopfoo'
37+
*
38+
* @example
39+
* var out = replaceAfter( 'Hello World!', '', 'foo', 0 );
40+
* // returns 'Hello World!'
41+
*
42+
* @example
43+
* var out = replaceAfter( 'Hello World!', 'xyz', 'foo', 0 );
44+
* // returns 'Hello World!'
45+
*
46+
* @example
47+
* var out = replaceAfter( 'beep boop', ' ', 'foo' , 5 );
48+
* // returns 'beep foo'
49+
*
50+
* @example
51+
* var out = replaceAfter( 'beep boop beep baz', 'beep', 'foo' , 5 );
52+
* // returns 'beep boop beepfoo'
53+
*/
54+
declare function replaceAfter( str: string, search: string, replacement: string, fromIndex: number ): string;
55+
56+
57+
// EXPORTS //
58+
59+
export = replaceAfter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
import replaceAfter = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
replaceAfter( 'beep boop', ' ', 'foo', 0 ); // $ExpectType string
27+
replaceAfter( 'beep boop', 'xyz', 'foo', 0 ); // $ExpectType string
28+
replaceAfter( 'beep boop', '', 'foo', 0 ); // $ExpectType string
29+
replaceAfter( 'beep boop', '', 'foo', 5 ); // $ExpectType string
30+
}
31+
32+
// The compiler throws an error if the function is provided arguments having invalid types...
33+
{
34+
replaceAfter( true, 'd', 'foo', 0 ); // $ExpectError
35+
replaceAfter( false, 'd' , 'foo', 0 ); // $ExpectError
36+
replaceAfter( 3, 'd' , 'foo', 0 ); // $ExpectError
37+
replaceAfter( [], 'd' , 'foo', 0 ); // $ExpectError
38+
replaceAfter( {}, 'd' , 'foo', 0 ); // $ExpectError
39+
replaceAfter( ( x: number ): number => x, 'd', 'foo', 0 ); // $ExpectError
40+
41+
replaceAfter( 'abc', true, 'foo', 0 ); // $ExpectError
42+
replaceAfter( 'abc', false, 'foo', 0 ); // $ExpectError
43+
replaceAfter( 'abc', 5 , 'foo', 0 ); // $ExpectError
44+
replaceAfter( 'abc', [], 'foo', 0 ); // $ExpectError
45+
replaceAfter( 'abc', {} , 'foo', 0 ); // $ExpectError
46+
replaceAfter( 'abc', ( x: number ): number => x , 'foo', 0 ); // $ExpectError
47+
48+
replaceAfter( 'abc', 'd', true, 0 ); // $ExpectError
49+
replaceAfter( 'abc', 'd', false, 0 ); // $ExpectError
50+
replaceAfter( 'abc', 'd', 5, 0 ); // $ExpectError
51+
replaceAfter( 'abc', 'd', [], 0 ); // $ExpectError
52+
replaceAfter( 'abc', 'd', {}, 0 ); // $ExpectError
53+
replaceAfter( 'abc', 'd', ( x: number ): number => x, 0 ); // $ExpectError
54+
55+
replaceAfter( 'abc', 'd', 'foo', true ); // $ExpectError
56+
replaceAfter( 'abc', 'd', 'foo', false ); // $ExpectError
57+
replaceAfter( 'abc', 'd', 'foo', '5' ); // $ExpectError
58+
replaceAfter( 'abc', 'd', 'foo', [] ); // $ExpectError
59+
replaceAfter( 'abc', 'd', 'foo', {} ); // $ExpectError
60+
replaceAfter( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError
61+
}
62+
63+
// The compiler throws an error if the function is provided insufficient arguments...
64+
{
65+
replaceAfter(); // $ExpectError
66+
replaceAfter( 'abc' ); // $ExpectError
67+
replaceAfter( 'abc', 'd' ); // $ExpectError
68+
replaceAfter( 'abc', 'd', 'd', 1, 1 ); // $ExpectError
69+
}

0 commit comments

Comments
 (0)