-
-
Notifications
You must be signed in to change notification settings - Fork 838
feat: add string/base/replace-after
#1363
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb4f1f0
feat: add @stdlib/string/base/replace-after
AuenKr 97e35fa
Update lib/node_modules/@stdlib/string/base/replace-after/README.md
AuenKr 238a8a2
Apply suggestions from code review
kgryte b1cc473
Merge branch 'stdlib-js:develop' into feature/replace-after
AuenKr 609b2ce
fixup! feat: add fromIndex argument
AuenKr ad83e85
fixup! feat: make fromIndex mandatory argument
AuenKr 7d56aff
Merge branch 'stdlib-js:develop' into feature/replace-after
AuenKr 1765012
Apply suggestions from code review
kgryte dfdf671
Apply suggestions from code review
kgryte 93584bd
Apply suggestions from code review
kgryte 346c51e
Apply suggestions from code review
kgryte e40d258
fixup! feat: test issue fixed
AuenKr c757331
Update repl.txt
AuenKr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
lib/node_modules/@stdlib/string/base/replace-after/README.md
This file contains hidden or 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,122 @@ | ||
<!-- | ||
|
||
@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, search, replacement ) | ||
|
||
Replaces the substring after the first occurrence of a specified search string. | ||
|
||
```javascript | ||
var out = replaceAfter( 'beep boop', ' ', 'loop' ); | ||
// returns 'beep loop' | ||
|
||
out = replaceAfter( 'beep boop', 'o', 'bar' ); | ||
// returns 'beep bobar' | ||
``` | ||
|
||
</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. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var replaceAfter = require( '@stdlib/string/base/replace-after' ); | ||
|
||
var out = replaceAfter( 'beep boop', 'p', 'see' ); | ||
// returns 'beepsee' | ||
|
||
out = replaceAfter( 'Hello World!', 'xyz', 'foo' ); | ||
// returns 'Hello World!' | ||
|
||
out = replaceAfter( 'Hello World!', '', 'foo' ); | ||
// returns 'Hello World!' | ||
|
||
out = replaceAfter( '', 'xyz', 'foo'); | ||
// 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 --> |
58 changes: 58 additions & 0 deletions
58
lib/node_modules/@stdlib/string/base/replace-after/benchmark/benchmark.js
This file contains hidden or 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) 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 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 ] ); | ||
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(); | ||
}); |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/string/base/replace-after/docs/repl.txt
This file contains hidden or 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,32 @@ | ||
|
||
{{alias}}( str, search, replacement ) | ||
Replaces the substring after the first occurrence of a specified search | ||
string. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
search: string | ||
Search string. | ||
|
||
replacement: string | ||
Replacement string. | ||
|
||
Returns | ||
------- | ||
out: string | ||
Output string. | ||
|
||
Examples | ||
-------- | ||
> var str = 'beep boop'; | ||
> var out = {{alias}}( str, ' ', 'foo' ) | ||
'beep foo' | ||
> out = {{alias}}( str, 'o', 'foo' ) | ||
'beep bofoo' | ||
|
||
See Also | ||
-------- | ||
|
50 changes: 50 additions & 0 deletions
50
lib/node_modules/@stdlib/string/base/replace-after/docs/types/index.d.ts
This file contains hidden or 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,50 @@ | ||
/* | ||
* @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: 4.1 | ||
|
||
/** | ||
* Replaces the substring after the first occurrence of a specified search string. | ||
* | ||
* @param str - input string | ||
* @param search - search string | ||
* @param replacement - replacement string | ||
* @returns output string | ||
* | ||
* @example | ||
* var out = replaceAfter( 'beep boop', ' ', 'foo' ); | ||
* // returns 'beep foo' | ||
* | ||
* @example | ||
* var out = replaceAfter( 'beep boop', 'p', 'foo' ); | ||
* // returns 'beepfoo' | ||
* | ||
* @example | ||
* var out = replaceAfter( 'Hello World!', '', 'foo' ); | ||
* // returns 'Hello World!' | ||
* | ||
* @example | ||
* var out = replaceAfter( 'Hello World!', 'xyz', 'foo' ); | ||
* // returns 'Hello World!' | ||
*/ | ||
declare function replaceAfter( str: string, search: string, replacement: string ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = replaceAfter; |
60 changes: 60 additions & 0 deletions
60
lib/node_modules/@stdlib/string/base/replace-after/docs/types/test.ts
This file contains hidden or 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,60 @@ | ||
/* | ||
* @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', 'xyz', 'foo' ); // $ExpectType string | ||
replaceAfter( 'beep boop', '', 'foo' ); // $ExpectType string | ||
} | ||
|
||
// The compiler throws an error if the function is provided arguments having invalid types... | ||
{ | ||
replaceAfter( true, 'd', 'foo' ); // $ExpectError | ||
replaceAfter( false, 'd' , 'foo' ); // $ExpectError | ||
replaceAfter( 3, 'd' , 'foo' ); // $ExpectError | ||
replaceAfter( [], 'd' , 'foo' ); // $ExpectError | ||
replaceAfter( {}, 'd' , 'foo' ); // $ExpectError | ||
replaceAfter( ( x: number ): number => x, 'd', 'foo' ); // $ExpectError | ||
|
||
replaceAfter( 'abc', true, 'foo' ); // $ExpectError | ||
replaceAfter( 'abc', false, 'foo' ); // $ExpectError | ||
replaceAfter( 'abc', 5 , 'foo' ); // $ExpectError | ||
replaceAfter( 'abc', [], 'foo' ); // $ExpectError | ||
replaceAfter( 'abc', {} , 'foo' ); // $ExpectError | ||
replaceAfter( 'abc', ( x: number ): number => x , 'foo' ); // $ExpectError | ||
|
||
replaceAfter( 'abc', 'd', true ); // $ExpectError | ||
replaceAfter( 'abc', 'd', false ); // $ExpectError | ||
replaceAfter( 'abc', 'd', 5 ); // $ExpectError | ||
replaceAfter( 'abc', 'd', [] ); // $ExpectError | ||
replaceAfter( 'abc', 'd', {} ); // $ExpectError | ||
replaceAfter( 'abc', 'd', ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided insufficient arguments... | ||
{ | ||
replaceAfter(); // $ExpectError | ||
replaceAfter( 'abc' ); // $ExpectError | ||
replaceAfter( 'abc', 'd' ); // $ExpectError | ||
} |
37 changes: 37 additions & 0 deletions
37
lib/node_modules/@stdlib/string/base/replace-after/examples/index.js
This file contains hidden or 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 @@ | ||
/** | ||
* @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'; | ||
|
||
var replaceAfter = require( './../lib' ); | ||
|
||
var out = replaceAfter( 'beep boop', 'p', 'see' ); | ||
console.log( out ); | ||
// => 'beepsee' | ||
|
||
out = replaceAfter( 'Hello World!', 'xyz', 'foo' ); | ||
console.log( out ); | ||
// => 'Hello World!' | ||
|
||
out = replaceAfter( 'Hello World!', '', 'foo' ); | ||
console.log( out ); | ||
// => 'Hello World!' | ||
|
||
out = replaceAfter( '', 'xyz', 'foo' ); | ||
console.log( out ); | ||
// => '' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.