Skip to content

Commit 29d7b0d

Browse files
committed
feat!: add support for fromIndex
BREAKING CHANGE: require `fromIndex` argument To migrate, users should provide an explicit `fromIndex` of `0` in order to retain previous behavior.
1 parent 607aad5 commit 29d7b0d

File tree

9 files changed

+114
-66
lines changed

9 files changed

+114
-66
lines changed

lib/node_modules/@stdlib/string/base/replace-before/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,19 @@ limitations under the License.
4040
var replaceBefore = require( '@stdlib/string/base/replace-before' );
4141
```
4242

43-
#### replaceBefore( str, search, replacement )
43+
#### replaceBefore( str, search, replacement, fromIndex )
4444

4545
Replaces the substring before the first occurrence of a specified search string.
4646

4747
```javascript
48-
var out = replaceBefore( 'beep boop', ' ', 'loop' );
48+
var out = replaceBefore( 'beep boop', ' ', 'loop', 0 );
4949
// returns 'loop boop'
5050

51-
out = replaceBefore( 'beep boop', 'o', 'bar' );
51+
out = replaceBefore( 'beep boop', 'o', 'bar', 0 );
5252
// returns 'baroop'
53+
54+
out = replaceBefore( 'beep boop', 'p', 'bar', 5 );
55+
// returns 'barp'
5356
```
5457

5558
</section>
@@ -64,6 +67,7 @@ out = replaceBefore( 'beep boop', 'o', 'bar' );
6467

6568
- If a search string is not present in a provided string, the function returns the provided string unchanged.
6669
- If a search string is an empty string, the function returns the provided string unchanged.
70+
- If `fromIndex` is less than `0` or greater than `str.length`, the search starts at index `0` and `str.length`, respectively.
6771

6872
</section>
6973

@@ -80,16 +84,16 @@ out = replaceBefore( 'beep boop', 'o', 'bar' );
8084
```javascript
8185
var replaceBefore = require( '@stdlib/string/base/replace-before' );
8286

83-
var out = replaceBefore( 'beep boop', 'p', 'see' );
87+
var out = replaceBefore( 'beep boop', 'p', 'see', 0 );
8488
// returns 'seep boop'
8589

86-
out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
90+
out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
8791
// returns 'Hello World!'
8892

89-
out = replaceBefore( 'Hello World!', '', 'foo' );
93+
out = replaceBefore( 'Hello World!', '', 'foo', 0 );
9094
// returns 'Hello World!'
9195

92-
out = replaceBefore( '', 'xyz', 'foo' );
96+
out = replaceBefore( '', 'xyz', 'foo', 0 );
9397
// returns ''
9498
```
9599

lib/node_modules/@stdlib/string/base/replace-before/benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bench( pkg, function benchmark( b ) {
4444

4545
b.tic();
4646
for ( i = 0; i < b.iterations; i++ ) {
47-
out = replaceBefore( str, '.', values[ i%values.length ] );
47+
out = replaceBefore( str, '.', values[ i%values.length ], 0 );
4848
if ( typeof out !== 'string' ) {
4949
b.fail( 'should return a string' );
5050
}

lib/node_modules/@stdlib/string/base/replace-before/docs/repl.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
{{alias}}( str, search, replacement )
2+
{{alias}}( str, search, replacement, fromIndex )
33
Replaces the substring before the first occurrence of a specified search
44
string.
55

@@ -10,21 +10,23 @@
1010

1111
search: string
1212
Search string.
13-
13+
1414
replacement: string
1515
Replacement string.
1616

17+
fromIndex: integer
18+
Index from which to start the search.
19+
1720
Returns
1821
-------
1922
out: string
2023
Output string.
2124

2225
Examples
2326
--------
24-
> var str = 'beep boop';
25-
> var out = {{alias}}( str, ' ', 'foo' )
27+
> var out = {{alias}}( 'beep boop', ' ', 'foo', 0 )
2628
'foo boop'
27-
> out = {{alias}}( str, 'o', 'foo' )
29+
> out = {{alias}}( 'beep boop', 'o', 'foo', 0 )
2830
'foooop'
2931

3032
See Also

lib/node_modules/@stdlib/string/base/replace-before/docs/types/index.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,26 @@
2424
* @param str - input string
2525
* @param search - search string
2626
* @param replacement - replacement string
27+
* @param fromIndex - index at which to start the search
2728
* @returns output string
2829
*
2930
* @example
30-
* var out = replaceBefore( 'beep boop', ' ', 'foo' );
31+
* var out = replaceBefore( 'beep boop', ' ', 'foo', 0 );
3132
* // returns 'foo boop'
3233
*
3334
* @example
34-
* var out = replaceBefore( 'beep boop', 'p', 'foo' );
35-
* // returns 'foop boop'
35+
* var out = replaceBefore( 'beep boop', 'p', 'foo', 5 );
36+
* // returns 'foop'
3637
*
3738
* @example
38-
* var out = replaceBefore( 'Hello World!', '', 'foo' );
39+
* var out = replaceBefore( 'Hello World!', '', 'foo', 0 );
3940
* // returns 'Hello world!'
4041
*
4142
* @example
42-
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
43+
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
4344
* // returns 'Hello World!'
4445
*/
45-
declare function replaceBefore( str: string, search: string, replacement: string ): string;
46+
declare function replaceBefore( str: string, search: string, replacement: string, fromIndex: number ): string;
4647

4748

4849
// EXPORTS //

lib/node_modules/@stdlib/string/base/replace-before/docs/types/test.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,46 @@ import replaceBefore = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
replaceBefore( 'beep boop', ' ', 'foo' ); // $ExpectType string
27-
replaceBefore( 'beep boop', 'xyz', 'foo' ); // $ExpectType string
28-
replaceBefore( 'beep boop', '', 'foo' ); // $ExpectType string
26+
replaceBefore( 'beep boop', ' ', 'foo', 0 ); // $ExpectType string
27+
replaceBefore( 'beep boop', 'xyz', 'foo', 0 ); // $ExpectType string
28+
replaceBefore( 'beep boop', '', 'foo', 0 ); // $ExpectType string
2929
}
3030

3131
// The compiler throws an error if the function is provided arguments having invalid types...
3232
{
33-
replaceBefore( true, 'd', 'foo' ); // $ExpectError
34-
replaceBefore( false, 'd' , 'foo' ); // $ExpectError
35-
replaceBefore( 3, 'd' , 'foo' ); // $ExpectError
36-
replaceBefore( [], 'd' , 'foo' ); // $ExpectError
37-
replaceBefore( {}, 'd' , 'foo' ); // $ExpectError
38-
replaceBefore( ( x: number ): number => x, 'd', 'foo' ); // $ExpectError
39-
40-
replaceBefore( 'abc', true, 'foo' ); // $ExpectError
41-
replaceBefore( 'abc', false, 'foo' ); // $ExpectError
42-
replaceBefore( 'abc', 5 , 'foo' ); // $ExpectError
43-
replaceBefore( 'abc', [], 'foo' ); // $ExpectError
44-
replaceBefore( 'abc', {} , 'foo' ); // $ExpectError
45-
replaceBefore( 'abc', ( x: number ): number => x , 'foo' ); // $ExpectError
46-
47-
replaceBefore( 'abc', 'd', true ); // $ExpectError
48-
replaceBefore( 'abc', 'd', false ); // $ExpectError
49-
replaceBefore( 'abc', 'd', 5 ); // $ExpectError
50-
replaceBefore( 'abc', 'd', [] ); // $ExpectError
51-
replaceBefore( 'abc', 'd', {} ); // $ExpectError
52-
replaceBefore( 'abc', 'd', ( x: number ): number => x ); // $ExpectError
33+
replaceBefore( true, 'd', 'foo', 0 ); // $ExpectError
34+
replaceBefore( false, 'd' , 'foo', 0 ); // $ExpectError
35+
replaceBefore( 3, 'd' , 'foo', 0 ); // $ExpectError
36+
replaceBefore( [], 'd' , 'foo', 0 ); // $ExpectError
37+
replaceBefore( {}, 'd' , 'foo', 0 ); // $ExpectError
38+
replaceBefore( ( x: number ): number => x, 'd', 'foo', 0 ); // $ExpectError
39+
40+
replaceBefore( 'abc', true, 'foo', 0 ); // $ExpectError
41+
replaceBefore( 'abc', false, 'foo', 0 ); // $ExpectError
42+
replaceBefore( 'abc', 5 , 'foo', 0 ); // $ExpectError
43+
replaceBefore( 'abc', [], 'foo', 0 ); // $ExpectError
44+
replaceBefore( 'abc', {} , 'foo', 0 ); // $ExpectError
45+
replaceBefore( 'abc', ( x: number ): number => x , 'foo', 0 ); // $ExpectError
46+
47+
replaceBefore( 'abc', 'd', true, 0 ); // $ExpectError
48+
replaceBefore( 'abc', 'd', false, 0 ); // $ExpectError
49+
replaceBefore( 'abc', 'd', 5, 0 ); // $ExpectError
50+
replaceBefore( 'abc', 'd', [], 0 ); // $ExpectError
51+
replaceBefore( 'abc', 'd', {}, 0 ); // $ExpectError
52+
replaceBefore( 'abc', 'd', ( x: number ): number => x, 0 ); // $ExpectError
53+
54+
replaceBefore( 'abc', 'd', 'foo', true ); // $ExpectError
55+
replaceBefore( 'abc', 'd', 'foo', false ); // $ExpectError
56+
replaceBefore( 'abc', 'd', 'foo', '5' ); // $ExpectError
57+
replaceBefore( 'abc', 'd', 'foo', [] ); // $ExpectError
58+
replaceBefore( 'abc', 'd', 'foo', {} ); // $ExpectError
59+
replaceBefore( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError
5360
}
5461

5562
// The compiler throws an error if the function is provided insufficient arguments...
5663
{
5764
replaceBefore(); // $ExpectError
5865
replaceBefore( 'abc' ); // $ExpectError
5966
replaceBefore( 'abc', 'd' ); // $ExpectError
67+
replaceBefore( 'abc', 'd', 'foo', 1, 1 ); // $ExpectError
6068
}

lib/node_modules/@stdlib/string/base/replace-before/examples/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020

2121
var replaceBefore = require( './../lib' );
2222

23-
var out = replaceBefore( 'beep boop', 'p', 'see' );
23+
var out = replaceBefore( 'beep boop', 'p', 'see', 0 );
2424
console.log( out );
2525
// => 'seep boop'
2626

27-
out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
27+
out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
2828
console.log( out );
2929
// => 'Hello World!'
3030

31-
out = replaceBefore( 'Hello World!', '', 'foo' );
31+
out = replaceBefore( 'Hello World!', '', 'foo', 0 );
3232
console.log( out );
3333
// => 'Hello World!'
3434

35-
out = replaceBefore( '', 'xyz', 'foo' );
35+
out = replaceBefore( '', 'xyz', 'foo', 0 );
3636
console.log( out );
3737
// => ''

lib/node_modules/@stdlib/string/base/replace-before/lib/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
* @example
2727
* var replaceBefore = require( '@stdlib/string/base/replace-before' );
2828
*
29-
* var str = 'beep boop';
30-
*
31-
* var out = replaceBefore( str, ' ', 'foo' );
29+
* var out = replaceBefore( 'beep boop', ' ', 'foo', 0 );
3230
* // returns 'foo boop'
3331
*
34-
* out = replaceBefore( str, 'o', 'bar' );
32+
* out = replaceBefore( 'beep boop', 'o', 'bar', 0 );
3533
* // returns 'baroop'
3634
*/
3735

lib/node_modules/@stdlib/string/base/replace-before/lib/main.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,33 @@
2626
* @param {string} str - input string
2727
* @param {string} search - search string
2828
* @param {string} replacement - replacement string
29+
* @param {integer} fromIndex - index at which to start the search
2930
* @returns {string} string
3031
*
3132
* @example
32-
* var out = replaceBefore( 'beep boop', ' ', 'foo' );
33+
* var out = replaceBefore( 'beep boop', ' ', 'foo', 0 );
3334
* // returns 'foo boop'
3435
*
3536
* @example
36-
* var out = replaceBefore( 'beep boop', 'p', 'foo' );
37-
* // returns 'foop boop'
37+
* var out = replaceBefore( 'beep boop', 'p', 'foo', 5 );
38+
* // returns 'foop'
3839
*
3940
* @example
40-
* var out = replaceBefore( 'Hello World!', '', 'foo' );
41+
* var out = replaceBefore( 'Hello World!', '', 'foo', 0 );
4142
* // returns 'Hello World!'
4243
*
4344
* @example
44-
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
45+
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
4546
* // returns 'Hello World!'
4647
*/
47-
function replaceBefore( str, search, replacement ) {
48-
var idx = str.indexOf( search );
48+
function replaceBefore( str, search, replacement, fromIndex ) {
49+
var idx;
50+
if ( fromIndex < 0 ) {
51+
fromIndex = 0;
52+
} else if ( fromIndex > str.length ) {
53+
fromIndex = str.length;
54+
}
55+
idx = str.indexOf( search, fromIndex );
4956
if ( str === '' || search === '' || replacement === '' || idx < 0 ) {
5057
return str;
5158
}

lib/node_modules/@stdlib/string/base/replace-before/test/test.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ tape( 'the function replaces the substring before the first occurrence of a spec
3636
var expected;
3737
var actual;
3838

39-
actual = replaceBefore( 'beep boop', ' ', 'foo' );
39+
actual = replaceBefore( 'beep boop', ' ', 'foo', 0 );
4040
expected = 'foo boop';
4141
t.strictEqual( actual, expected, 'returns expected value' );
4242

43-
actual = replaceBefore( 'beep boop', 'p', 'foo' );
43+
actual = replaceBefore( 'beep boop', 'p', 'foo', 0 );
4444
expected = 'foop boop';
4545
t.strictEqual( actual, expected, 'returns expected value' );
4646

47-
actual = replaceBefore( 'Hello, World!', 'o', 'foo' );
47+
actual = replaceBefore( 'Hello, World!', 'o', 'foo', 0 );
4848
expected = 'fooo, World!';
4949
t.strictEqual( actual, expected, 'returns expected value' );
5050

@@ -55,30 +55,58 @@ tape( 'the function replaces the substring before the first occurrence of a spec
5555
var expected;
5656
var actual;
5757

58-
actual = replaceBefore( 'beep 😀 boop 😀 baz', '😀', 'foo' );
58+
actual = replaceBefore( 'beep 😀 boop 😀 baz', '😀', 'foo', 0 );
5959
expected = 'foo😀 boop 😀 baz';
6060
t.strictEqual( actual, expected, 'returns expected value' );
6161

62-
actual = replaceBefore( '🤖 Robot army 🤖!', '🤖', 'foo' );
62+
actual = replaceBefore( '🤖 Robot army 🤖!', '🤖', 'foo', 0 );
6363
expected = 'foo🤖 Robot army 🤖!';
6464
t.strictEqual( actual, expected, 'returns expected value' );
6565

66-
actual = replaceBefore( '🐺 Wolf brothers 🐺', 'o', 'foo' );
66+
actual = replaceBefore( '🐺 Wolf brothers 🐺', 'o', 'foo', 0 );
6767
expected = 'fooolf brothers 🐺';
6868
t.strictEqual( actual, expected, 'returns expected value' );
6969

7070
t.end();
7171
});
7272

73+
tape( 'the function replaces the substring before a provided search string (custom start index)', function test( t ) {
74+
var expected;
75+
var actual;
76+
var str;
77+
78+
str = 'beep boop baz';
79+
actual = replaceBefore( str, ' ', 'foo', 6 );
80+
expected = 'foo baz';
81+
t.strictEqual( actual, expected, 'returns expected value' );
82+
83+
str = 'beep boop baz';
84+
actual = replaceBefore( str, 'p', 'foo', 6 );
85+
expected = 'foop baz';
86+
t.strictEqual( actual, expected, 'returns expected value' );
87+
88+
str = 'beep boop baz';
89+
actual = replaceBefore( str, 'beep', 'foo', -2 );
90+
expected = 'foobeep boop baz';
91+
t.strictEqual( actual, expected, 'returns expected value' );
92+
93+
str = 'beep boop baz';
94+
actual = replaceBefore( str, 'beep', 'foo', 20 );
95+
expected = 'beep boop baz';
96+
t.strictEqual( actual, expected, 'returns expected value' );
97+
98+
t.end();
99+
});
100+
73101
tape( 'the function returns the entire string if the search string is not found', function test( t ) {
74102
var expected;
75103
var actual;
76104

77-
actual = replaceBefore( 'beep boop', 'z', 'foo' );
105+
actual = replaceBefore( 'beep boop', 'z', 'foo', 0 );
78106
expected = 'beep boop';
79107
t.strictEqual( actual, expected, 'returns expected value' );
80108

81-
actual = replaceBefore( 'beep boop', 'baz', 'foo' );
109+
actual = replaceBefore( 'beep boop', 'baz', 'foo', 0 );
82110
expected = 'beep boop';
83111
t.strictEqual( actual, expected, 'returns expected value' );
84112

@@ -89,7 +117,7 @@ tape( 'the function returns the entire string if the search string is the empty
89117
var expected;
90118
var actual;
91119

92-
actual = replaceBefore( 'beep boop', '', 'foo' );
120+
actual = replaceBefore( 'beep boop', '', 'foo', 0 );
93121
expected = 'beep boop';
94122
t.strictEqual( actual, expected, 'returns expected value' );
95123

0 commit comments

Comments
 (0)