Skip to content

Commit d2bb2df

Browse files
committed
Auto-generated commit
1 parent 931d7ee commit d2bb2df

File tree

8 files changed

+39
-10
lines changed

8 files changed

+39
-10
lines changed

.github/.keepalive

-1
This file was deleted.

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,22 @@ out = replaceBeforeLast( str, 'o', 'bar', str.length );
8686
// returns 'barop'
8787
```
8888

89-
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.
89+
To begin searching from a specific index, provide a corresponding `fromIndex` argument.
9090

9191
```javascript
9292
var str = 'beep boop beep';
9393
var out = replaceBeforeLast( str, ' ', 'loop', 5 );
9494
// returns 'loop boop beep'
9595
```
9696

97+
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`.
98+
99+
```javascript
100+
var str = 'beep boop beep';
101+
var out = replaceBeforeLast( str, ' ', 'loop', -1 );
102+
// returns 'loop beep'
103+
```
104+
97105
</section>
98106

99107
<!-- /.usage -->
@@ -106,7 +114,7 @@ var out = replaceBeforeLast( str, ' ', 'loop', 5 );
106114

107115
- If a search string is not present in a provided string, the function returns the provided string unchanged.
108116
- If a search string is an empty string, the function returns the provided string unchanged.
109-
- If `fromIndex` is less than `0`, the function returns the provided string unchanged.
117+
- If `fromIndex` resolves to an index which is less than `0`, the function returns the provided string unchanged.
110118

111119
</section>
112120

dist/index.js

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

dist/index.js.map

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

docs/repl.txt

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

docs/types/index.d.ts

+6
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/main.js

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

test/test.js

+5
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)