-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ff88e2
commit a371b6b
Showing
10 changed files
with
745 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<!-- | ||
@license Apache-2.0 | ||
Copyright (c) 2024 The Stdlib Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
# replaceBeforeLast | ||
|
||
> Replace the substring before the last occurrence of a specified search string. | ||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); | ||
``` | ||
|
||
#### replaceBeforeLast( str, search, replacement, fromIndex ) | ||
|
||
Replaces the substring before the last occurrence of a specified search string. | ||
|
||
```javascript | ||
var str = 'beep boop'; | ||
var out = replaceBeforeLast( str, ' ', 'loop', str.length ); | ||
// returns 'loop boop' | ||
|
||
out = replaceBeforeLast( str, 'o', 'bar', str.length ); | ||
// returns 'barop' | ||
``` | ||
|
||
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. | ||
|
||
```javascript | ||
var str = 'beep boop beep'; | ||
var out = replaceBeforeLast( str, ' ', 'loop', 5 ); | ||
// returns 'loop boop beep' | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- If a search string is not present in a provided string, the function returns the provided string unchanged. | ||
- If a search string is an empty string, the function returns the provided string unchanged. | ||
- If `fromIndex` is less than `0`, the function returns the provided string unchanged. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); | ||
|
||
var str = 'beep boop'; | ||
var out = replaceBeforeLast( str, 'p', 'see', str.length ); | ||
// returns 'seep' | ||
|
||
str = 'Hello World!'; | ||
out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); | ||
// returns 'Hello World!' | ||
|
||
str = 'Hello World!'; | ||
out = replaceBeforeLast( str, '', 'foo', str.length ); | ||
// returns 'Hello World!' | ||
|
||
str = ''; | ||
out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); | ||
// returns '' | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- 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. --> | ||
|
||
<section class="references"> | ||
|
||
</section> | ||
|
||
<!-- /.references --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var replaceBeforeLast = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var values; | ||
var out; | ||
var str; | ||
var i; | ||
|
||
str = 'To be, or not to be, that is the question.'; | ||
values = [ | ||
'foo', | ||
'bar', | ||
'beep', | ||
'boop' | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = replaceBeforeLast( str, 'T', values[ i%values.length ], str.length ); | ||
if ( typeof out !== 'string' ) { | ||
b.fail( 'should return a string' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isString( out ) ) { | ||
b.fail( 'should return a string' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
{{alias}}( str, search, replacement, fromIndex ) | ||
Replaces the substring before the last occurrence of a specified search | ||
string. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
search: string | ||
Search string. | ||
|
||
replacement: string | ||
Replacement string. | ||
|
||
fromIndex: integer | ||
Index from which to start searching. | ||
|
||
Returns | ||
------- | ||
out: string | ||
Output string. | ||
|
||
Examples | ||
-------- | ||
> var str = 'beep boop'; | ||
> var out = {{alias}}( str, ' ', 'foo', str.length ) | ||
'foo boop' | ||
> out = {{alias}}( str, 'o', 'foo', str.length ) | ||
'fooop' | ||
> out = {{alias}}( 'Hello World!', 'o', 'foo', 5 ) | ||
'fooo World!' | ||
|
||
See Also | ||
-------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Replaces the substring before the last occurrence of a specified search string. | ||
* | ||
* @param str - input string | ||
* @param search - search string | ||
* @param replacement - replacement string | ||
* @param fromIndex - index from which to start searching | ||
* @returns output string | ||
* | ||
* @example | ||
* var str = 'beep boop'; | ||
* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); | ||
* // returns 'foo boop' | ||
* | ||
* @example | ||
* var str = 'beep boop'; | ||
* var out = replaceBeforeLast( str, 'p', 'foo', str.length ); | ||
* // returns 'foop' | ||
* | ||
* @example | ||
* var str = 'Hello World!'; | ||
* var out = replaceBeforeLast( str, '', 'foo', str.length ); | ||
* // returns 'Hello World!' | ||
* | ||
* @example | ||
* var str = 'Hello World!'; | ||
* var out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); | ||
* // returns 'Hello World!' | ||
* | ||
* @example | ||
* var str = 'beep boop baz'; | ||
* var out = replaceBeforeLast( str, 'p b', 'foo', str.length ); | ||
* // returns 'foop baz' | ||
* | ||
* @example | ||
* var str = 'beep boop baz'; | ||
* var out = replaceBeforeLast( str, 'p b', 'foo', 6 ); | ||
* // returns 'foop boop baz' | ||
*/ | ||
declare function replaceBeforeLast( str: string, search: string, replacement: string, fromIndex: number ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = replaceBeforeLast; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import replaceBeforeLast = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a string... | ||
{ | ||
replaceBeforeLast( 'beep boop', ' ', 'foo', 10 ); // $ExpectType string | ||
replaceBeforeLast( 'beep boop', 'xyz', 'foo', 10 ); // $ExpectType string | ||
replaceBeforeLast( 'beep boop', '', 'foo', 10 ); // $ExpectType string | ||
} | ||
|
||
// The compiler throws an error if the function is provided arguments having invalid types... | ||
{ | ||
replaceBeforeLast( true, 'd', 'foo', 100 ); // $ExpectError | ||
replaceBeforeLast( false, 'd' , 'foo', 100 ); // $ExpectError | ||
replaceBeforeLast( 3, 'd' , 'foo', 100 ); // $ExpectError | ||
replaceBeforeLast( [], 'd' , 'foo', 100 ); // $ExpectError | ||
replaceBeforeLast( {}, 'd' , 'foo', 100 ); // $ExpectError | ||
replaceBeforeLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError | ||
|
||
replaceBeforeLast( 'abc', true, 'foo', 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', false, 'foo', 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', 5 , 'foo', 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', [], 'foo', 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', {} , 'foo', 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo', 10 ); // $ExpectError | ||
|
||
replaceBeforeLast( 'abc', 'd', true, 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', false, 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 5, 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', [], 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', {}, 10 ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', ( x: number ): number => x, 10 ); // $ExpectError | ||
|
||
replaceBeforeLast( 'abc', 'd', 'foo', true ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo', false ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo', '5' ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo', [] ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo', {} ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided insufficient arguments... | ||
{ | ||
replaceBeforeLast(); // $ExpectError | ||
replaceBeforeLast( 'abc' ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd' ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo' ); // $ExpectError | ||
replaceBeforeLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError | ||
} |
Oops, something went wrong.