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

Conversation

HarshitaKalani
Copy link
Contributor

Resolves #812 .

With this PR, I'm adding the implementation for replace-after issue.

@stdlib-js/reviewers

@kgryte kgryte marked this pull request as draft February 24, 2023 22:10
@kgryte kgryte added the Feature Issue or pull request for adding a new feature. label Feb 24, 2023
@kgryte kgryte changed the title Added implementation for @stdlib/string/base/replace-after feat: add support for replacing a substring after the first occurrence of a search string Feb 24, 2023
@kgryte
Copy link
Member

kgryte commented Feb 25, 2023

@HarshitaKalani For this implementation, you can leave out the CLI and input validation. I've updated @stdlib/string/base/replace-before accordingly, which you can use as a reference when building out this package. LMK if you have any questions!

@HarshitaKalani
Copy link
Contributor Author

Hey @kgryte , please have a review and lmk if anything needs to be updated.

@@ -0,0 +1,20 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

This file can be removed, as the package does not include a CLI.

*/
function replaceAfter( str, replacement, search, fromIndex ) {
var idx;
if ( !isString( str ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

You can remove input argument validation as this is a "base" package where we assume we've been provided valid inputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove the validation of the 4th argument "fromIndex" too?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, you can remove the isInteger check.

*
* @example
* var out = replaceAfter( 'beep boop', 'foo', 'xyz' );
* // returns ''
Copy link
Member

Choose a reason for hiding this comment

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

This is unexpected. I'd expect that, if a search string is not present in an input string, the function would return the input string unchanged.

*
* @example
* var out = replaceAfter( 'beep boop', 'foo', 'beep', 5 );
* // returns ''
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above.

idx = str.indexOf( search );
}
if ( idx === -1 ) {
return '';
Copy link
Member

Choose a reason for hiding this comment

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

IMO, we should return str here.

@HarshitaKalani
Copy link
Contributor Author

@kgryte please have a review.

@kgryte
Copy link
Member

kgryte commented Mar 2, 2023

@HarshitaKalani Am I correct in thinking that this PR is complete and ready for review?

@HarshitaKalani HarshitaKalani marked this pull request as ready for review March 2, 2023 07:47
@HarshitaKalani
Copy link
Contributor Author

Hey @kgryte , please review it and suggest me a good issue to pick next.

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.

Comment on lines +76 to +77
- 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.
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.


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.

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

*/
function replaceAfter( str, replacement, search, fromIndex ) {
var idx;
if ( arguments.length > 3 ) {
Copy link
Member

Choose a reason for hiding this comment

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

We should include similar checks as in replace-before. Namely, if str or search is empty, we can return an input string.

* var out = replaceAfter( 'beep boop beep baz', 'foo', 'beep', 5 );
* // returns 'beep boop beepfoo'
*/
function replaceAfter( str, replacement, search, fromIndex ) {
Copy link
Member

Choose a reason for hiding this comment

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

As discussed for the README, IMO the argument order should be updated to match replace-before.

@HarshitaKalani
Copy link
Contributor Author

image

I'm getting this linting error whenever I try to commit. Please suggest how to get over with the error.

@kgryte
Copy link
Member

kgryte commented Apr 2, 2023

Try upgrading to the latest version of Node.js (eg, v18).

@kgryte kgryte added Stale Issue or pull request which has not been updated in an extended period of time. Needs Changes Pull request which needs changes before being merged. labels Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. Stale Issue or pull request which has not been updated in an extended period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC]: Add @stdlib/string/base/replace-after
3 participants