-
-
Notifications
You must be signed in to change notification settings - Fork 805
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
base: develop
Are you sure you want to change the base?
Changes from all commits
2f8f997
b1c3450
61e942f
1e26eb0
d96a912
ec3587e
778b012
6473012
73a3653
03ce774
1f4d64a
5510512
3f16eb0
8774e45
09f1928
1ef2db6
e7619ff
ca8a67b
e8a3b9c
7b7ef7f
18a91aa
2e0c9d1
2f7eed4
149949d
18f5e8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] ) | ||
|
||
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, ' ' ); | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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: | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```javascript | ||
var str = 'boop baz boop'; | ||
var replacement = 'foo'; | ||
var out = replaceAfter( str, replacement, 'o', 3 ); | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These notes are not correct. See |
||
- 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than use |
||
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,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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
isand
@stdlib/string/replace-before
isMy preference is to keep consistency where
fromIndex
is the last argument andsearch
comes beforereplacement
.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.