Skip to content

feat: add support for replacing a substring after the first occurrence of a search string #922

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

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2f8f997
readme.md and package.json added to replace-after
HarshitaKalani Feb 24, 2023
b1c3450
index.js and main.js added in replace-after
HarshitaKalani Feb 24, 2023
61e942f
updated main.js and added examples
HarshitaKalani Feb 24, 2023
1e26eb0
etc added
HarshitaKalani Feb 24, 2023
d96a912
benchmark added
HarshitaKalani Feb 25, 2023
ec3587e
benchmark.js
HarshitaKalani Feb 25, 2023
778b012
benchmark.js
HarshitaKalani Feb 25, 2023
6473012
types added
HarshitaKalani Feb 25, 2023
73a3653
test.js added
HarshitaKalani Feb 25, 2023
03ce774
test.js added and main.js updated
HarshitaKalani Feb 25, 2023
1f4d64a
test.js added and main.js updated
HarshitaKalani Feb 25, 2023
5510512
main.js updated
HarshitaKalani Feb 25, 2023
3f16eb0
main.js updated
HarshitaKalani Feb 25, 2023
8774e45
cli removed from readme.md
HarshitaKalani Feb 25, 2023
09f1928
cli removed from readme.md
HarshitaKalani Feb 25, 2023
1ef2db6
package.json updated
HarshitaKalani Feb 25, 2023
e7619ff
main.js logic updated
HarshitaKalani Feb 27, 2023
ca8a67b
examples corrected wrt main.js
HarshitaKalani Feb 27, 2023
e8a3b9c
isValid consitions removed
HarshitaKalani Feb 28, 2023
7b7ef7f
Update lib/node_modules/@stdlib/string/base/replace-after/README.md
Pranavchiku Mar 2, 2023
18a91aa
Update lib/node_modules/@stdlib/string/base/replace-after/benchmark/b…
Pranavchiku Mar 2, 2023
2e0c9d1
indentation rectified in package.json
HarshitaKalani Mar 2, 2023
2f7eed4
repl.txt corrected
HarshitaKalani Mar 2, 2023
149949d
all tests passed
HarshitaKalani Mar 2, 2023
18f5e8f
feat: main.js updated
HarshitaKalani Apr 1, 2023
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
139 changes: 139 additions & 0 deletions lib/node_modules/@stdlib/string/base/replace-after/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!--

@license Apache-2.0

Copyright (c) 2023 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.

-->

# replaceAfter

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

#### replaceAfter( str, replacement, search\[, fromIndex] )
Copy link
Member

@kgryte kgryte Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HarshitaKalani I just noticed this, but the argument order is reversed from @stdlib/string/base/replace-before. Is there a reason for this? Personally, I would expect the argument order to be the same for the sake of API consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept it that way since there's an extra argument 'fromIndex', so thought it's better to keep search at last in case fromIndex is also given as the argument.
Should I keep it as replaceAfter(str, search, replacement[, fromIndex] ) or replaceAfter(str, search[, fromIndex], replacement) would be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that @stdlib/string/replace is

replace( str, search, newval )

and @stdlib/string/replace-before is

replaceBefore( str, search, replacement )

My preference is to keep consistency where fromIndex is the last argument and search comes before replacement.

replaceAfter( str, search, replacement[, fromIndex] )

The fromIndex argument comes last in other APIs (e.g., @stdlib/string/starts-with and others), so IMO best to keep consistent here and have it be the last argument, which is also better from a polymorphic interface standpoint. An optional argument in the middle of mandatory arguments is generally harder to optimize.


Replaces the substring after the first occurrence of a specified search string.

```javascript
var str = 'beep boop';
var replacement = 'foo';
var out = replaceAfter( str, replacement, 'o' );
// returns 'beep bofoo'

out = replaceAfter( str, replacement, ' ' );
// returns 'beep foo'
```

By default, the search starts at the beginning of the string. To start searching from a different index, provide a `fromIndex` argument:

```javascript
var str = 'boop baz boop';
var replacement = 'foo';
var out = replaceAfter( str, replacement, 'o', 3 );
// returns 'boop baz bofoo'
```

</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 substring is not present in a provided string, the function returns an empty string.
- If provided an empty substring, the function returns the input string.
Comment on lines +76 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These notes are not correct. See replace-before.

- If `fromIndex` is less than `0` or greater than `str.length`, the search starts at index `0` and `str.length`, respectively.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

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

var str = 'To be, or not to be, that is the question.';
var replacement = 'foo';
var out = replaceAfter( str, replacement, ', ' );
// returns 'To be, foo'

out = replaceAfter( str, replacement, 'to be' );
// returns 'To be, or not to befoo'

out = replaceAfter( str, replacement, 'question.' );
// returns 'To be, or not to be, that is the question.foo'

out = replaceAfter( str, replacement, 'xyz' );
// returns 'To be, or not to be, that is the question.'

out = replaceAfter( str, replacement, '' );
// returns 'foo'
```

</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,59 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 fromCodePoint = require( '@stdlib/string/from-code-point' );
var pkg = require( './../package.json' ).name;
var replaceAfter = 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 = replaceAfter( str, values[ i % values.length ], fromCodePoint( i%126 ) ); // eslint-disable-line max-len
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than use fromCodePoint, use ., as done in replace-before.

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();
});
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/string/base/replace-after/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

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

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

replacement: string
Replacement string.

search: string
Search string.

fromIndex: integer (optional)
Index from which to start the search. Default: `0`.

Returns
-------
out: string
Substring.

Examples
--------
> var out = {{alias}}( 'Hello World!', 'foo', 'World' )
'Hello Worldfoo'
> out = {{alias}}( 'Hello World!', 'foo', 'Hello ' )
'Hello foo'
> out = {{alias}}( 'Hello World!', 'foo', 'l', 5 )
'Hello Worlfoo'

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 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: 2.0

/// <reference types="@stdlib/types"/>

/**
* Returns the part of a string after a specified substring.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description is incorrect.

*
* @param str - input string
* @param replacement - replacement string
* @param search - search string
* @param fromIndex - index at which to start the search (default: 0)
* @returns substring
*
* @example
* var out = replaceAfter( 'Hello, world!', 'foo', ', ' );
* // returns 'Hello, foo'
*
* @example
* var out = replaceAfter( 'beep boop', 'foo', 'beep' );
* // returns 'beepfoo'
*
* @example
* var out = replaceAfter( 'beep boop', 'foo', 'boop' );
* // returns 'beep boopfoo'
*
* @example
* var out = replaceAfter( 'beep boop', 'foo', 'xyz' );
* // returns 'beep boop'
*
* @example
* var out = replaceAfter( 'beep boop', 'foo', 'beep', 5 );
* // returns 'beep boop'
*
* @example
* var out = replaceAfter( 'beep boop beep baz', 'foo', 'beep', 5 );
* // returns ' beep boop beepfoo'
*/
declare function replaceAfter( str: string, replacement: string, search: string, fromIndex?: number ): string; // tslint:disable-line:max-line-length


// EXPORTS //

export = replaceAfter;
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 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 replaceAfter = require( './index' );


// TESTS //

// The function returns a string...
{
replaceAfter( 'beep boop', 'foo', ' ' ); // $ExpectType string
replaceAfter( 'beep boop', 'foo', 'xyz' ); // $ExpectType string
replaceAfter( 'beep boop', 'foo', '' ); // $ExpectType string
replaceAfter( 'beep boop', 'foo', 'b', 5 ); // $ExpectType string
}

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

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

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

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

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
replaceAfter(); // $ExpectError
replaceAfter( 'abc' ); // $ExpectError
replaceAfter( 'abc', 'd', 1, 1 ); // $ExpectError
}
Loading