Skip to content

Commit 58bdac8

Browse files
AuenKrkgrytePlaneshifter
authored
feat: add string/base/replace-after-last
PR-URL: #1365 Closes: #814 Signed-off-by: Golden <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 589377c commit 58bdac8

File tree

10 files changed

+741
-0
lines changed

10 files changed

+741
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
# replaceAfterLast
22+
23+
> Replace the substring after the last 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 replaceAfterLast = require( '@stdlib/string/base/replace-after-last' );
41+
```
42+
43+
#### replaceAfterLast( str, search, replacement, fromIndex )
44+
45+
Replaces the substring after the last occurrence of a specified search string.
46+
47+
```javascript
48+
var str = 'beep boop';
49+
var out = replaceAfterLast( str, ' ', 'loop', str.length );
50+
// returns 'beep loop'
51+
52+
out = replaceAfterLast( str, 'o', 'bar', str.length );
53+
// returns 'beep boobar'
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- If a search string is not present in a provided string, the function returns the provided string unchanged.
67+
- If a search string is an empty string, the function returns the provided string unchanged.
68+
- If `fromIndex` is less than `0`, the function returns the provided string unchanged.
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 replaceAfterLast = require( '@stdlib/string/base/replace-after-last' );
84+
85+
var str = 'beep boop';
86+
var out = replaceAfterLast( str, 'p', 'see', str.length );
87+
// returns 'beep boopsee'
88+
89+
str = 'Hello World!';
90+
out = replaceAfterLast( str, 'xyz', 'foo', str.length );
91+
// returns 'Hello World!'
92+
93+
str = 'Hello World!';
94+
out = replaceAfterLast( str, '', 'foo', str.length );
95+
// returns 'Hello World!'
96+
97+
str = '';
98+
out = replaceAfterLast( str, 'xyz', 'foo', str.length );
99+
// returns ''
100+
101+
str = 'beep boop beep baz';
102+
out = replaceAfterLast( str, 'beep', 'foo', 5 );
103+
// return 'beepfoo'
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- 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. -->
111+
112+
<section class="references">
113+
114+
</section>
115+
116+
<!-- /.references -->
117+
118+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
119+
120+
<section class="related">
121+
122+
</section>
123+
124+
<!-- /.related -->
125+
126+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="links">
129+
130+
</section>
131+
132+
<!-- /.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 replaceAfterLast = 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 = replaceAfterLast( str, 'T', values[ i%values.length ], str.length );
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,37 @@
1+
2+
{{alias}}( str, search, replacement, fromIndex )
3+
Replaces the substring after the last 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 searching.
19+
20+
Returns
21+
-------
22+
out: string
23+
Output string.
24+
25+
Examples
26+
--------
27+
> var str = 'beep boop';
28+
> var out = {{alias}}( str, ' ', 'foo', str.length )
29+
'beep foo'
30+
> out = {{alias}}( str, 'o', 'foo', str.length )
31+
'beep boofoo'
32+
> out = {{alias}}( 'Hello World!', 'o', 'foo', 5 )
33+
'Hellofoo'
34+
35+
See Also
36+
--------
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 last 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 from which to start searching
28+
* @returns output string
29+
*
30+
* @example
31+
* var str = 'beep boop';
32+
* var out = replaceAfterLast( str, ' ', 'foo', str.length );
33+
* // returns 'beep foo'
34+
*
35+
* @example
36+
* var str = 'beep boop';
37+
* var out = replaceAfterLast( str, 'p', 'foo', str.length );
38+
* // returns 'beep boopfoo'
39+
*
40+
* @example
41+
* var str = 'Hello World!';
42+
* var out = replaceAfterLast( str, '', 'foo', str.length );
43+
* // returns 'Hello World!'
44+
*
45+
* @example
46+
* var str = 'Hello World!';
47+
* var out = replaceAfterLast( str, 'xyz', 'foo', str.length );
48+
* // returns 'Hello World!'
49+
*
50+
* @example
51+
* var str = 'beep boop baz';
52+
* var out = replaceAfterLast( str, 'p b', 'foo', str.length );
53+
* // returns 'beep boop bfoo'
54+
*
55+
* @example
56+
* var str = 'beep boop baz';
57+
* var out = replaceAfterLast( str, 'p b', 'foo', 6 );
58+
* // returns 'beep bfoo'
59+
*/
60+
declare function replaceAfterLast( str: string, search: string, replacement: string, fromIndex: number ): string;
61+
62+
63+
// EXPORTS //
64+
65+
export = replaceAfterLast;
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 replaceAfterLast = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
replaceAfterLast( 'beep boop', ' ', 'foo', 10 ); // $ExpectType string
27+
replaceAfterLast( 'beep boop', 'xyz', 'foo', 10 ); // $ExpectType string
28+
replaceAfterLast( 'beep boop', '', 'foo', 10 ); // $ExpectType string
29+
}
30+
31+
// The compiler throws an error if the function is provided arguments having invalid types...
32+
{
33+
replaceAfterLast( true, 'd', 'foo', 100 ); // $ExpectError
34+
replaceAfterLast( false, 'd' , 'foo', 100 ); // $ExpectError
35+
replaceAfterLast( 3, 'd' , 'foo', 100 ); // $ExpectError
36+
replaceAfterLast( [], 'd' , 'foo', 100 ); // $ExpectError
37+
replaceAfterLast( {}, 'd' , 'foo', 100 ); // $ExpectError
38+
replaceAfterLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError
39+
40+
replaceAfterLast( 'abc', true, 'foo', 10 ); // $ExpectError
41+
replaceAfterLast( 'abc', false, 'foo', 10 ); // $ExpectError
42+
replaceAfterLast( 'abc', 5 , 'foo', 10 ); // $ExpectError
43+
replaceAfterLast( 'abc', [], 'foo', 10 ); // $ExpectError
44+
replaceAfterLast( 'abc', {} , 'foo', 10 ); // $ExpectError
45+
replaceAfterLast( 'abc', ( x: number ): number => x , 'foo', 10 ); // $ExpectError
46+
47+
replaceAfterLast( 'abc', 'd', true, 10 ); // $ExpectError
48+
replaceAfterLast( 'abc', 'd', false, 10 ); // $ExpectError
49+
replaceAfterLast( 'abc', 'd', 5, 10 ); // $ExpectError
50+
replaceAfterLast( 'abc', 'd', [], 10 ); // $ExpectError
51+
replaceAfterLast( 'abc', 'd', {}, 10 ); // $ExpectError
52+
replaceAfterLast( 'abc', 'd', ( x: number ): number => x, 10 ); // $ExpectError
53+
54+
replaceAfterLast( 'abc', 'd', 'foo', true ); // $ExpectError
55+
replaceAfterLast( 'abc', 'd', 'foo', false ); // $ExpectError
56+
replaceAfterLast( 'abc', 'd', 'foo', '5' ); // $ExpectError
57+
replaceAfterLast( 'abc', 'd', 'foo', [] ); // $ExpectError
58+
replaceAfterLast( 'abc', 'd', 'foo', {} ); // $ExpectError
59+
replaceAfterLast( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError
60+
}
61+
62+
// The compiler throws an error if the function is provided insufficient arguments...
63+
{
64+
replaceAfterLast(); // $ExpectError
65+
replaceAfterLast( 'abc' ); // $ExpectError
66+
replaceAfterLast( 'abc', 'd' ); // $ExpectError
67+
replaceAfterLast( 'abc', 'd', 'foo' ); // $ExpectError
68+
replaceAfterLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError
69+
}

0 commit comments

Comments
 (0)