Skip to content

Commit 272f91b

Browse files
AuenKrstdlib-botkgrytePlaneshifter
authored
feat: add string/base/replace-before-last
PR-URL: #1364 Closes: #813 Signed-off-by: Athan Reines <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 58bdac8 commit 272f91b

File tree

10 files changed

+745
-0
lines changed

10 files changed

+745
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
# replaceBeforeLast
22+
23+
> Replace the substring before 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 replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' );
41+
```
42+
43+
#### replaceBeforeLast( str, search, replacement, fromIndex )
44+
45+
Replaces the substring before the last occurrence of a specified search string.
46+
47+
```javascript
48+
var str = 'beep boop';
49+
var out = replaceBeforeLast( str, ' ', 'loop', str.length );
50+
// returns 'loop boop'
51+
52+
out = replaceBeforeLast( str, 'o', 'bar', str.length );
53+
// returns 'barop'
54+
```
55+
56+
The search starts at the end of the string and proceeds backwards to the beginning. To start the search at a specified index, specify an integer for the `fromIndex` argument.
57+
58+
```javascript
59+
var str = 'beep boop beep';
60+
var out = replaceBeforeLast( str, ' ', 'loop', 5 );
61+
// returns 'loop boop beep'
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- If a search string is not present in a provided string, the function returns the provided string unchanged.
75+
- If a search string is an empty string, the function returns the provided string unchanged.
76+
- If `fromIndex` is less than `0`, the function returns the provided string unchanged.
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<!-- Package usage examples. -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' );
92+
93+
var str = 'beep boop';
94+
var out = replaceBeforeLast( str, 'p', 'see', str.length );
95+
// returns 'seep'
96+
97+
str = 'Hello World!';
98+
out = replaceBeforeLast( str, 'xyz', 'foo', str.length );
99+
// returns 'Hello World!'
100+
101+
str = 'Hello World!';
102+
out = replaceBeforeLast( str, '', 'foo', str.length );
103+
// returns 'Hello World!'
104+
105+
str = '';
106+
out = replaceBeforeLast( str, 'xyz', 'foo', str.length );
107+
// returns ''
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- 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. -->
115+
116+
<section class="references">
117+
118+
</section>
119+
120+
<!-- /.references -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
</section>
135+
136+
<!-- /.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 replaceBeforeLast = 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 = replaceBeforeLast( 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 before 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+
'foo boop'
30+
> out = {{alias}}( str, 'o', 'foo', str.length )
31+
'fooop'
32+
> out = {{alias}}( 'Hello World!', 'o', 'foo', 5 )
33+
'fooo World!'
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 before 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 = replaceBeforeLast( str, ' ', 'foo', str.length );
33+
* // returns 'foo boop'
34+
*
35+
* @example
36+
* var str = 'beep boop';
37+
* var out = replaceBeforeLast( str, 'p', 'foo', str.length );
38+
* // returns 'foop'
39+
*
40+
* @example
41+
* var str = 'Hello World!';
42+
* var out = replaceBeforeLast( str, '', 'foo', str.length );
43+
* // returns 'Hello World!'
44+
*
45+
* @example
46+
* var str = 'Hello World!';
47+
* var out = replaceBeforeLast( str, 'xyz', 'foo', str.length );
48+
* // returns 'Hello World!'
49+
*
50+
* @example
51+
* var str = 'beep boop baz';
52+
* var out = replaceBeforeLast( str, 'p b', 'foo', str.length );
53+
* // returns 'foop baz'
54+
*
55+
* @example
56+
* var str = 'beep boop baz';
57+
* var out = replaceBeforeLast( str, 'p b', 'foo', 6 );
58+
* // returns 'foop boop baz'
59+
*/
60+
declare function replaceBeforeLast( str: string, search: string, replacement: string, fromIndex: number ): string;
61+
62+
63+
// EXPORTS //
64+
65+
export = replaceBeforeLast;
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 replaceBeforeLast = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
replaceBeforeLast( 'beep boop', ' ', 'foo', 10 ); // $ExpectType string
27+
replaceBeforeLast( 'beep boop', 'xyz', 'foo', 10 ); // $ExpectType string
28+
replaceBeforeLast( '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+
replaceBeforeLast( true, 'd', 'foo', 100 ); // $ExpectError
34+
replaceBeforeLast( false, 'd' , 'foo', 100 ); // $ExpectError
35+
replaceBeforeLast( 3, 'd' , 'foo', 100 ); // $ExpectError
36+
replaceBeforeLast( [], 'd' , 'foo', 100 ); // $ExpectError
37+
replaceBeforeLast( {}, 'd' , 'foo', 100 ); // $ExpectError
38+
replaceBeforeLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError
39+
40+
replaceBeforeLast( 'abc', true, 'foo', 10 ); // $ExpectError
41+
replaceBeforeLast( 'abc', false, 'foo', 10 ); // $ExpectError
42+
replaceBeforeLast( 'abc', 5 , 'foo', 10 ); // $ExpectError
43+
replaceBeforeLast( 'abc', [], 'foo', 10 ); // $ExpectError
44+
replaceBeforeLast( 'abc', {} , 'foo', 10 ); // $ExpectError
45+
replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo', 10 ); // $ExpectError
46+
47+
replaceBeforeLast( 'abc', 'd', true, 10 ); // $ExpectError
48+
replaceBeforeLast( 'abc', 'd', false, 10 ); // $ExpectError
49+
replaceBeforeLast( 'abc', 'd', 5, 10 ); // $ExpectError
50+
replaceBeforeLast( 'abc', 'd', [], 10 ); // $ExpectError
51+
replaceBeforeLast( 'abc', 'd', {}, 10 ); // $ExpectError
52+
replaceBeforeLast( 'abc', 'd', ( x: number ): number => x, 10 ); // $ExpectError
53+
54+
replaceBeforeLast( 'abc', 'd', 'foo', true ); // $ExpectError
55+
replaceBeforeLast( 'abc', 'd', 'foo', false ); // $ExpectError
56+
replaceBeforeLast( 'abc', 'd', 'foo', '5' ); // $ExpectError
57+
replaceBeforeLast( 'abc', 'd', 'foo', [] ); // $ExpectError
58+
replaceBeforeLast( 'abc', 'd', 'foo', {} ); // $ExpectError
59+
replaceBeforeLast( '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+
replaceBeforeLast(); // $ExpectError
65+
replaceBeforeLast( 'abc' ); // $ExpectError
66+
replaceBeforeLast( 'abc', 'd' ); // $ExpectError
67+
replaceBeforeLast( 'abc', 'd', 'foo' ); // $ExpectError
68+
replaceBeforeLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError
69+
}

0 commit comments

Comments
 (0)