Skip to content

Commit ddf8a0a

Browse files
committed
Auto-generated commit
1 parent 217bdc3 commit ddf8a0a

File tree

8 files changed

+45
-10
lines changed

8 files changed

+45
-10
lines changed

Diff for: .github/.keepalive

-1
This file was deleted.

Diff for: README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ out = replaceAfterLast( str, 'o', 'bar', str.length );
8686
// returns 'beep boobar'
8787
```
8888

89+
To begin searching from a specific index, provide a corresponding `fromIndex` argument.
90+
91+
```javascript
92+
var out = replaceAfterLast( 'beep boop beep', ' ', 'loop', 6 );
93+
// returns 'beep loop'
94+
```
95+
96+
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`.
97+
98+
```javascript
99+
var out = replaceAfterLast( 'beep boop beep', ' ', 'loop', -1 );
100+
// returns 'beep boop loop'
101+
```
102+
89103
</section>
90104

91105
<!-- /.usage -->
@@ -98,7 +112,7 @@ out = replaceAfterLast( str, 'o', 'bar', str.length );
98112

99113
- If a search string is not present in a provided string, the function returns the provided string unchanged.
100114
- If a search string is an empty string, the function returns the provided string unchanged.
101-
- If `fromIndex` is less than `0`, the function returns the provided string unchanged.
115+
- If `fromIndex` resolves to an index which is less than `0`, the function returns the provided string unchanged.
102116

103117
</section>
104118

Diff for: dist/index.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/index.js.map

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/repl.txt

+9-1
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
-------

Diff for: docs/types/index.d.ts

+6
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

Diff for: lib/main.js

+4-1
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 ) {

Diff for: test/test.js

+6-1
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)