Skip to content

Commit b6cb1d3

Browse files
committed
feat!: support negative fromIndex arguments
BREAKING CHANGE: resolve negative indices relative to last index In order to preserve prior behavior, users should insert a manual check before calling this API. Ref: #1364 Ref: 272f91b
1 parent 20f26c2 commit b6cb1d3

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ out = replaceBeforeLast( str, 'o', 'bar', str.length );
5353
// returns 'barop'
5454
```
5555

56-
The search starts at the end of the string and proceeds backwards to the beginning. To start the search at a specified index, specify an integer for the `fromIndex` argument.
56+
To begin searching from a specific index, provide a corresponding `fromIndex` argument.
5757

5858
```javascript
5959
var str = 'beep boop beep';
6060
var out = replaceBeforeLast( str, ' ', 'loop', 5 );
6161
// returns 'loop boop beep'
6262
```
6363

64+
If `fromIndex` is less than zero, the starting index is resolved relative to the last string character, with the last string character corresponding to `fromIndex = -1`.
65+
66+
```javascript
67+
var str = 'beep boop beep';
68+
var out = replaceBeforeLast( str, ' ', 'loop', -1 );
69+
// returns 'loop beep'
70+
```
71+
6472
</section>
6573

6674
<!-- /.usage -->
@@ -73,7 +81,7 @@ var out = replaceBeforeLast( str, ' ', 'loop', 5 );
7381

7482
- If a search string is not present in a provided string, the function returns the provided string unchanged.
7583
- If a search string is an empty string, the function returns the provided string unchanged.
76-
- If `fromIndex` is less than `0`, the function returns the provided string unchanged.
84+
- If `fromIndex` resolves to an index which is less than `0`, the function returns the provided string unchanged.
7785

7886
</section>
7987

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Replaces the substring before the last occurrence of a specified search
44
string.
55

6+
If unable to find a search string, the function returns the input string
7+
unchanged.
8+
9+
The function scans an input string from the starting index to the beginning
10+
of the string (i.e., backward).
11+
612
Parameters
713
----------
814
str: string
@@ -15,7 +21,9 @@
1521
Replacement string.
1622

1723
fromIndex: integer
18-
Index from which to start searching.
24+
Starting index (inclusive). If less than zero, the starting index is
25+
resolved relative to the last string character, with the last string
26+
character corresponding to `fromIndex = -1`.
1927

2028
Returns
2129
-------

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
/**
2222
* Replaces the substring before the last occurrence of a specified search string.
2323
*
24+
* ## Notes
25+
*
26+
* - The function scans a provided string from the starting index to the beginning of the string (i.e., backward).
27+
* - If unable to find search string, the function returns the input string unchanged.
28+
* - If `fromIndex` is less than zero, the starting index is resolved relative to the last string chraacter, with the last string chraacter corresponding to `fromIndex = -1`.
29+
*
2430
* @param str - input string
2531
* @param search - search string
2632
* @param replacement - replacement string

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@
6262
function replaceBeforeLast( str, search, replacement, fromIndex ) {
6363
var idx;
6464
if ( fromIndex < 0 ) {
65-
return str;
65+
fromIndex += str.length;
66+
if ( fromIndex < 0 ) {
67+
return str;
68+
}
6669
}
6770
idx = str.lastIndexOf( search, fromIndex );
6871
if ( str === '' || search === '' || replacement === '' || idx < 0 ) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ tape( 'the function replaces the substring before a provided search string (cust
9595

9696
str = 'beep boop baz';
9797
actual = replaceBeforeLast( str, 'beep', 'foo', -2 );
98+
expected = 'foobeep boop baz';
99+
t.strictEqual( actual, expected, 'returns expected value' );
100+
101+
str = 'beep boop baz';
102+
actual = replaceBeforeLast( str, 'beep', 'foo', -20 );
98103
expected = 'beep boop baz';
99104
t.strictEqual( actual, expected, 'returns expected value' );
100105

0 commit comments

Comments
 (0)