Skip to content

Commit 12f3952

Browse files
committed
Auto-generated commit
1 parent 7dba8c5 commit 12f3952

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,22 @@ var out = replaceBefore( 'beep boop', ' ', 'loop', 0 );
8383

8484
out = replaceBefore( 'beep boop', 'o', 'bar', 0 );
8585
// returns 'baroop'
86+
```
87+
88+
To begin searching from a specific index, provide a corresponding `fromIndex` argument.
8689

87-
out = replaceBefore( 'beep boop', 'p', 'bar', 5 );
90+
```javascript
91+
var out = replaceBefore( 'beep boop', 'p', 'bar', 5 );
8892
// returns 'barp'
8993
```
9094

95+
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`.
96+
97+
```javascript
98+
var out = replaceBefore( 'beep boop beep', ' ', 'loop', -6 );
99+
// returns 'loop beep'
100+
```
101+
91102
</section>
92103

93104
<!-- /.usage -->
@@ -100,7 +111,7 @@ out = replaceBefore( 'beep boop', 'p', 'bar', 5 );
100111

101112
- If a search string is not present in a provided string, the function returns the provided string unchanged.
102113
- If a search string is an empty string, the function returns the provided string unchanged.
103-
- If `fromIndex` is less than `0` or greater than `str.length`, the search starts at index `0` and `str.length`, respectively.
114+
- If `fromIndex` resolves to an index which is greater than or equal to `str.length`, the function returns the provided string unchanged.
104115

105116
</section>
106117

dist/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/repl.txt

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

22
{{alias}}( str, search, replacement, fromIndex )
33
Replaces the substring before the first occurrence of a specified search
4-
string.
4+
string.
5+
6+
If unable to find a search string, the function returns the input string
7+
unchanged.
58

69
Parameters
710
----------
@@ -15,7 +18,9 @@
1518
Replacement string.
1619

1720
fromIndex: integer
18-
Index from which to start the search.
21+
Starting index (inclusive). If less than zero, the starting index is
22+
resolved relative to the last string character, with the last string
23+
character corresponding to `fromIndex = -1`.
1924

2025
Returns
2126
-------

docs/types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
/**
2222
* Replaces the substring before the first occurrence of a specified search string.
2323
*
24+
* ## Notes
25+
*
26+
* - If unable to find search string, the function returns the input string unchanged.
27+
* - 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`.
28+
*
2429
* @param str - input string
2530
* @param search - search string
2631
* @param replacement - replacement string

lib/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
function replaceBefore( str, search, replacement, fromIndex ) {
4949
var idx;
5050
if ( fromIndex < 0 ) {
51-
fromIndex = 0;
52-
} else if ( fromIndex > str.length ) {
53-
fromIndex = str.length;
51+
fromIndex += str.length;
52+
} else if ( fromIndex >= str.length ) {
53+
return str;
5454
}
5555
idx = str.indexOf( search, fromIndex );
5656
if ( str === '' || search === '' || replacement === '' || idx < 0 ) {

test/test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,24 @@ tape( 'the function replaces the substring before a provided search string (cust
8787

8888
str = 'beep boop baz';
8989
actual = replaceBefore( str, 'beep', 'foo', -2 );
90-
expected = 'foobeep boop baz';
90+
expected = 'beep boop baz';
91+
t.strictEqual( actual, expected, 'returns expected value' );
92+
93+
str = 'beep boop baz';
94+
actual = replaceBefore( str, 'baz', 'foo', -5 );
95+
expected = 'foobaz';
9196
t.strictEqual( actual, expected, 'returns expected value' );
9297

9398
str = 'beep boop baz';
9499
actual = replaceBefore( str, 'beep', 'foo', 20 );
95100
expected = 'beep boop baz';
96101
t.strictEqual( actual, expected, 'returns expected value' );
97102

103+
str = 'beep boop baz';
104+
actual = replaceBefore( str, 'beep', 'foo', -200 );
105+
expected = 'foobeep boop baz';
106+
t.strictEqual( actual, expected, 'returns expected value' );
107+
98108
t.end();
99109
});
100110

0 commit comments

Comments
 (0)