Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add string/base/replace-after-last #1365

Merged
merged 14 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions lib/node_modules/@stdlib/string/base/replace-after-last/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<!--

@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.

-->

# replaceAfterLast

> Replace the substring after 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 replaceAfterLast = require( '@stdlib/string/base/replace-after-last' );
```

#### replaceAfterLast( str, search, replacement, fromIndex )

- Replaces the substring after the last occurrence of a specified search string.
- fromIndex is index of last character to be considered beginning of a match.
AuenKr marked this conversation as resolved.
Show resolved Hide resolved

```javascript
var str = 'beep boop';
var out = replaceAfterLast( str, ' ', 'loop', str.length );
// returns 'beep loop'

out = replaceAfterLast( str, 'o', 'bar', str.length );
// returns 'beep boobar'
```

</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 or greater than search string length, the function returns the provided string unchanged.
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var replaceAfterLast = require( '@stdlib/string/base/replace-after-last' );

var str = 'beep boop';
var out = replaceAfterLast( str, 'p', 'see', str.length );
// returns 'beep boopsee'

str = 'Hello World!';
out = replaceAfterLast( str, 'xyz', 'foo', str.length );
// returns 'Hello World!'

str = 'Hello World!';
out = replaceAfterLast( str, '', 'foo', str.length );
// returns 'Hello World!'

str = '';
out = replaceAfterLast( 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 -->
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 replaceAfterLast = 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 = replaceAfterLast( str, '.', values[ i%values.length ], str.length );
AuenKr marked this conversation as resolved.
Show resolved Hide resolved
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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

{{alias}}( str, search, replacement, fromIndex )
Replaces the substring after the last occurrence of a specified search
string.

Parameters
----------
str: string
Input string.

search: string
Search string.

replacement: string
Replacement string.

fromIndex: integer
Index of last character to be considered beginning of a match.
AuenKr marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
out: string
Output string.

Examples
--------
> var str = 'beep boop';
> var out = {{alias}}( str, ' ', 'foo', str.length )
'beep foo'
> out = {{alias}}( str, 'o', 'foo', str.length )
'beep boofoo'
> out = {{alias}}( 'Hello World!', 'o', 'foo', 5 )
'Hellofoo'

See Also
--------

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 after the last occurrence of a specified search string.
*
* @param str - input string
* @param search - search string
* @param replacement - replacement string
* @param fromIndex - index of last character to be considered beginning of a match
AuenKr marked this conversation as resolved.
Show resolved Hide resolved
* @returns output string
*
* @example
* var str = 'beep boop';
* var out = replaceAfterLast( str, ' ', 'foo', str.length );
* // returns 'beep foo'
*
* @example
* var str = 'beep boop';
* var out = replaceAfterLast( str, 'p', 'foo', str.length );
* // returns 'beep boopfoo'
*
* @example
* var str = 'Hello World!';
* var out = replaceAfterLast( str, '', 'foo', str.length);
AuenKr marked this conversation as resolved.
Show resolved Hide resolved
* // returns 'Hello World!'
*
* @example
* var str = 'Hello World!';
* var out = replaceAfterLast( str, 'xyz', 'foo', str.length );
* // returns 'Hello World!'
*
* @example
* var str = 'beep boop baz';
* var out = replaceAfterLast( str, 'p b', 'foo', str.length );
* // returns 'beep boop bfoo'
*
* @example
* var str = 'beep boop baz';
* var out = replaceAfterLast( str, 'p b', 'foo', 6 );
* // returns 'beep bfoo'
*/
declare function replaceAfterLast( str: string, search: string, replacement: string, fromIndex: number ): string;
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved


// EXPORTS //

export = replaceAfterLast;
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 replaceAfterLast = require( './index' );


// TESTS //

// The function returns a string...
{
replaceAfterLast( 'beep boop', ' ', 'foo', 'beep boop'.length ); // $ExpectType string
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
replaceAfterLast( 'beep boop', 'xyz', 'foo', 'beep boop'.length ); // $ExpectType string
replaceAfterLast( 'beep boop', '', 'foo', 'beep boop'.length ); // $ExpectType string
}

// The compiler throws an error if the function is provided arguments having invalid types...
{
replaceAfterLast( true, 'd', 'foo', 100 ); // $ExpectError
replaceAfterLast( false, 'd' , 'foo', 100 ); // $ExpectError
replaceAfterLast( 3, 'd' , 'foo', 100 ); // $ExpectError
replaceAfterLast( [], 'd' , 'foo', 100 ); // $ExpectError
replaceAfterLast( {}, 'd' , 'foo', 100 ); // $ExpectError
replaceAfterLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError

replaceAfterLast( 'abc', true, 'foo', 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', false, 'foo', 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', 5 , 'foo', 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', [], 'foo', 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', {} , 'foo', 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', ( x: number ): number => x , 'foo', 'abc'.length ); // $ExpectError

replaceAfterLast( 'abc', 'd', true, 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', 'd', false, 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', 'd', 5, 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', 'd', [], 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', 'd', {}, 'abc'.length ); // $ExpectError
replaceAfterLast( 'abc', 'd', ( x: number ): number => x, 'abc'.length ); // $ExpectError

replaceAfterLast( 'abc', 'd', 'foo', true ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo', false ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo', '5' ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo', [] ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo', {} ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
replaceAfterLast(); // $ExpectError
replaceAfterLast( 'abc' ); // $ExpectError
replaceAfterLast( 'abc', 'd' ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo' ); // $ExpectError
replaceAfterLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError
}
Loading
Loading