Skip to content

Commit ec1c506

Browse files
committed
feat!: resolve negative index arguments relative to last index
BREAKING CHANGE: resolve negative index arguments relative to last index Previously, a negative `fromIndex` argument would resolve to `0`. The current behavior resolves relative to the last character index. To preserve the previous behavior, users should clamp index arguments to index bounds before calling `replaceAfterLast`. Ref: #1365 Ref: 58bdac8
1 parent e863555 commit ec1c506

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ out = replaceAfterLast( str, 'o', 'bar', str.length );
5353
// returns 'beep boobar'
5454
```
5555

56+
To begin searching from a specific index, provide a corresponding `fromIndex` argument.
57+
58+
```javascript
59+
var out = replaceAfterLast( 'beep boop beep', ' ', 'loop', 6 );
60+
// returns 'beep loop'
61+
```
62+
63+
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`.
64+
65+
```javascript
66+
var out = replaceAfterLast( 'beep boop beep', ' ', 'loop', -1 );
67+
// returns 'beep boop loop'
68+
```
69+
5670
</section>
5771

5872
<!-- /.usage -->
@@ -65,7 +79,7 @@ out = replaceAfterLast( str, 'o', 'bar', str.length );
6579

6680
- If a search string is not present in a provided string, the function returns the provided string unchanged.
6781
- If a search string is an empty string, the function returns the provided string unchanged.
68-
- If `fromIndex` is less than `0`, the function returns the provided string unchanged.
82+
- If `fromIndex` resolves to an index which is less than `0`, the function returns the provided string unchanged.
6983

7084
</section>
7185

lib/node_modules/@stdlib/string/base/replace-after-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 after 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-after-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 after 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 character, with the last string character 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-after-last/lib/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@
6262
function replaceAfterLast( 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-after-last/test/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ tape( 'the function replaces the substring after a provided search string (custo
9494
t.strictEqual( actual, expected, 'returns expected value' );
9595

9696
str = 'beep boop baz';
97-
actual = replaceAfterLast( str, 'beep', 'foo', -2 );
97+
actual = replaceAfterLast( str, 'beep', 'foo', -3 );
98+
expected = 'beepfoo';
99+
t.strictEqual( actual, expected, 'returns expected value' );
100+
101+
str = 'beep boop baz';
102+
actual = replaceAfterLast( str, 'beep', 'foo', -100 );
98103
expected = 'beep boop baz';
99104
t.strictEqual( actual, expected, 'returns expected value' );
100105

0 commit comments

Comments
 (0)