Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Apr 5, 2024
1 parent c466c43 commit 96fd74f
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 68 deletions.
1 change: 0 additions & 1 deletion .github/.keepalive

This file was deleted.

9 changes: 9 additions & 0 deletions base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ setReadOnly( ns, 'repeat', require( './../../base/repeat' ) );
*/
setReadOnly( ns, 'replace', require( './../../base/replace' ) );

/**
* @name replaceAfter
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/string/base/replace-after}
*/
setReadOnly( ns, 'replaceAfter', require( './../../base/replace-after' ) );

/**
* @name replaceBefore
* @memberof ns
Expand Down
18 changes: 11 additions & 7 deletions base/replace-before/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ limitations under the License.
var replaceBefore = require( '@stdlib/string/base/replace-before' );
```

#### replaceBefore( str, search, replacement )
#### replaceBefore( str, search, replacement, fromIndex )

Replaces the substring before the first occurrence of a specified search string.

```javascript
var out = replaceBefore( 'beep boop', ' ', 'loop' );
var out = replaceBefore( 'beep boop', ' ', 'loop', 0 );
// returns 'loop boop'

out = replaceBefore( 'beep boop', 'o', 'bar' );
out = replaceBefore( 'beep boop', 'o', 'bar', 0 );
// returns 'baroop'

out = replaceBefore( 'beep boop', 'p', 'bar', 5 );
// returns 'barp'
```

</section>
Expand All @@ -64,6 +67,7 @@ out = replaceBefore( 'beep boop', 'o', 'bar' );

- If a search string is not present in a provided string, the function returns the provided string unchanged.
- If a search string is an empty string, the function returns the provided string unchanged.
- If `fromIndex` is less than `0` or greater than `str.length`, the search starts at index `0` and `str.length`, respectively.

</section>

Expand All @@ -80,16 +84,16 @@ out = replaceBefore( 'beep boop', 'o', 'bar' );
```javascript
var replaceBefore = require( '@stdlib/string/base/replace-before' );

var out = replaceBefore( 'beep boop', 'p', 'see' );
var out = replaceBefore( 'beep boop', 'p', 'see', 0 );
// returns 'seep boop'

out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
// returns 'Hello World!'

out = replaceBefore( 'Hello World!', '', 'foo' );
out = replaceBefore( 'Hello World!', '', 'foo', 0 );
// returns 'Hello World!'

out = replaceBefore( '', 'xyz', 'foo' );
out = replaceBefore( '', 'xyz', 'foo', 0 );
// returns ''
```

Expand Down
2 changes: 1 addition & 1 deletion base/replace-before/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = replaceBefore( str, '.', values[ i%values.length ] );
out = replaceBefore( str, '.', values[ i%values.length ], 0 );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
Expand Down
12 changes: 7 additions & 5 deletions base/replace-before/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{{alias}}( str, search, replacement )
{{alias}}( str, search, replacement, fromIndex )
Replaces the substring before the first occurrence of a specified search
string.

Expand All @@ -10,21 +10,23 @@

search: string
Search string.

replacement: string
Replacement string.

fromIndex: integer
Index from which to start the search.

Returns
-------
out: string
Output string.

Examples
--------
> var str = 'beep boop';
> var out = {{alias}}( str, ' ', 'foo' )
> var out = {{alias}}( 'beep boop', ' ', 'foo', 0 )
'foo boop'
> out = {{alias}}( str, 'o', 'foo' )
> out = {{alias}}( 'beep boop', 'o', 'foo', 0 )
'foooop'

See Also
Expand Down
13 changes: 7 additions & 6 deletions base/replace-before/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,26 @@
* @param str - input string
* @param search - search string
* @param replacement - replacement string
* @param fromIndex - index at which to start the search
* @returns output string
*
* @example
* var out = replaceBefore( 'beep boop', ' ', 'foo' );
* var out = replaceBefore( 'beep boop', ' ', 'foo', 0 );
* // returns 'foo boop'
*
* @example
* var out = replaceBefore( 'beep boop', 'p', 'foo' );
* // returns 'foop boop'
* var out = replaceBefore( 'beep boop', 'p', 'foo', 5 );
* // returns 'foop'
*
* @example
* var out = replaceBefore( 'Hello World!', '', 'foo' );
* var out = replaceBefore( 'Hello World!', '', 'foo', 0 );
* // returns 'Hello world!'
*
* @example
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
* // returns 'Hello World!'
*/
declare function replaceBefore( str: string, search: string, replacement: string ): string;
declare function replaceBefore( str: string, search: string, replacement: string, fromIndex: number ): string;


// EXPORTS //
Expand Down
54 changes: 31 additions & 23 deletions base/replace-before/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,46 @@ import replaceBefore = require( './index' );

// The function returns a string...
{
replaceBefore( 'beep boop', ' ', 'foo' ); // $ExpectType string
replaceBefore( 'beep boop', 'xyz', 'foo' ); // $ExpectType string
replaceBefore( 'beep boop', '', 'foo' ); // $ExpectType string
replaceBefore( 'beep boop', ' ', 'foo', 0 ); // $ExpectType string
replaceBefore( 'beep boop', 'xyz', 'foo', 0 ); // $ExpectType string
replaceBefore( 'beep boop', '', 'foo', 0 ); // $ExpectType string
}

// The compiler throws an error if the function is provided arguments having invalid types...
{
replaceBefore( true, 'd', 'foo' ); // $ExpectError
replaceBefore( false, 'd' , 'foo' ); // $ExpectError
replaceBefore( 3, 'd' , 'foo' ); // $ExpectError
replaceBefore( [], 'd' , 'foo' ); // $ExpectError
replaceBefore( {}, 'd' , 'foo' ); // $ExpectError
replaceBefore( ( x: number ): number => x, 'd', 'foo' ); // $ExpectError

replaceBefore( 'abc', true, 'foo' ); // $ExpectError
replaceBefore( 'abc', false, 'foo' ); // $ExpectError
replaceBefore( 'abc', 5 , 'foo' ); // $ExpectError
replaceBefore( 'abc', [], 'foo' ); // $ExpectError
replaceBefore( 'abc', {} , 'foo' ); // $ExpectError
replaceBefore( 'abc', ( x: number ): number => x , 'foo' ); // $ExpectError

replaceBefore( 'abc', 'd', true ); // $ExpectError
replaceBefore( 'abc', 'd', false ); // $ExpectError
replaceBefore( 'abc', 'd', 5 ); // $ExpectError
replaceBefore( 'abc', 'd', [] ); // $ExpectError
replaceBefore( 'abc', 'd', {} ); // $ExpectError
replaceBefore( 'abc', 'd', ( x: number ): number => x ); // $ExpectError
replaceBefore( true, 'd', 'foo', 0 ); // $ExpectError
replaceBefore( false, 'd' , 'foo', 0 ); // $ExpectError
replaceBefore( 3, 'd' , 'foo', 0 ); // $ExpectError
replaceBefore( [], 'd' , 'foo', 0 ); // $ExpectError
replaceBefore( {}, 'd' , 'foo', 0 ); // $ExpectError
replaceBefore( ( x: number ): number => x, 'd', 'foo', 0 ); // $ExpectError

replaceBefore( 'abc', true, 'foo', 0 ); // $ExpectError
replaceBefore( 'abc', false, 'foo', 0 ); // $ExpectError
replaceBefore( 'abc', 5 , 'foo', 0 ); // $ExpectError
replaceBefore( 'abc', [], 'foo', 0 ); // $ExpectError
replaceBefore( 'abc', {} , 'foo', 0 ); // $ExpectError
replaceBefore( 'abc', ( x: number ): number => x , 'foo', 0 ); // $ExpectError

replaceBefore( 'abc', 'd', true, 0 ); // $ExpectError
replaceBefore( 'abc', 'd', false, 0 ); // $ExpectError
replaceBefore( 'abc', 'd', 5, 0 ); // $ExpectError
replaceBefore( 'abc', 'd', [], 0 ); // $ExpectError
replaceBefore( 'abc', 'd', {}, 0 ); // $ExpectError
replaceBefore( 'abc', 'd', ( x: number ): number => x, 0 ); // $ExpectError

replaceBefore( 'abc', 'd', 'foo', true ); // $ExpectError
replaceBefore( 'abc', 'd', 'foo', false ); // $ExpectError
replaceBefore( 'abc', 'd', 'foo', '5' ); // $ExpectError
replaceBefore( 'abc', 'd', 'foo', [] ); // $ExpectError
replaceBefore( 'abc', 'd', 'foo', {} ); // $ExpectError
replaceBefore( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
replaceBefore(); // $ExpectError
replaceBefore( 'abc' ); // $ExpectError
replaceBefore( 'abc', 'd' ); // $ExpectError
replaceBefore( 'abc', 'd', 'foo', 1, 1 ); // $ExpectError
}
8 changes: 4 additions & 4 deletions base/replace-before/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

var replaceBefore = require( './../lib' );

var out = replaceBefore( 'beep boop', 'p', 'see' );
var out = replaceBefore( 'beep boop', 'p', 'see', 0 );
console.log( out );
// => 'seep boop'

out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
console.log( out );
// => 'Hello World!'

out = replaceBefore( 'Hello World!', '', 'foo' );
out = replaceBefore( 'Hello World!', '', 'foo', 0 );
console.log( out );
// => 'Hello World!'

out = replaceBefore( '', 'xyz', 'foo' );
out = replaceBefore( '', 'xyz', 'foo', 0 );
console.log( out );
// => ''
6 changes: 2 additions & 4 deletions base/replace-before/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
* @example
* var replaceBefore = require( '@stdlib/string/base/replace-before' );
*
* var str = 'beep boop';
*
* var out = replaceBefore( str, ' ', 'foo' );
* var out = replaceBefore( 'beep boop', ' ', 'foo', 0 );
* // returns 'foo boop'
*
* out = replaceBefore( str, 'o', 'bar' );
* out = replaceBefore( 'beep boop', 'o', 'bar', 0 );
* // returns 'baroop'
*/

Expand Down
21 changes: 14 additions & 7 deletions base/replace-before/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,33 @@
* @param {string} str - input string
* @param {string} search - search string
* @param {string} replacement - replacement string
* @param {integer} fromIndex - index at which to start the search
* @returns {string} string
*
* @example
* var out = replaceBefore( 'beep boop', ' ', 'foo' );
* var out = replaceBefore( 'beep boop', ' ', 'foo', 0 );
* // returns 'foo boop'
*
* @example
* var out = replaceBefore( 'beep boop', 'p', 'foo' );
* // returns 'foop boop'
* var out = replaceBefore( 'beep boop', 'p', 'foo', 5 );
* // returns 'foop'
*
* @example
* var out = replaceBefore( 'Hello World!', '', 'foo' );
* var out = replaceBefore( 'Hello World!', '', 'foo', 0 );
* // returns 'Hello World!'
*
* @example
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo' );
* var out = replaceBefore( 'Hello World!', 'xyz', 'foo', 0 );
* // returns 'Hello World!'
*/
function replaceBefore( str, search, replacement ) {
var idx = str.indexOf( search );
function replaceBefore( str, search, replacement, fromIndex ) {
var idx;
if ( fromIndex < 0 ) {
fromIndex = 0;
} else if ( fromIndex > str.length ) {
fromIndex = str.length;
}
idx = str.indexOf( search, fromIndex );
if ( str === '' || search === '' || replacement === '' || idx < 0 ) {
return str;
}
Expand Down
46 changes: 37 additions & 9 deletions base/replace-before/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ tape( 'the function replaces the substring before the first occurrence of a spec
var expected;
var actual;

actual = replaceBefore( 'beep boop', ' ', 'foo' );
actual = replaceBefore( 'beep boop', ' ', 'foo', 0 );
expected = 'foo boop';
t.strictEqual( actual, expected, 'returns expected value' );

actual = replaceBefore( 'beep boop', 'p', 'foo' );
actual = replaceBefore( 'beep boop', 'p', 'foo', 0 );
expected = 'foop boop';
t.strictEqual( actual, expected, 'returns expected value' );

actual = replaceBefore( 'Hello, World!', 'o', 'foo' );
actual = replaceBefore( 'Hello, World!', 'o', 'foo', 0 );
expected = 'fooo, World!';
t.strictEqual( actual, expected, 'returns expected value' );

Expand All @@ -55,30 +55,58 @@ tape( 'the function replaces the substring before the first occurrence of a spec
var expected;
var actual;

actual = replaceBefore( 'beep 😀 boop 😀 baz', '😀', 'foo' );
actual = replaceBefore( 'beep 😀 boop 😀 baz', '😀', 'foo', 0 );
expected = 'foo😀 boop 😀 baz';
t.strictEqual( actual, expected, 'returns expected value' );

actual = replaceBefore( '🤖 Robot army 🤖!', '🤖', 'foo' );
actual = replaceBefore( '🤖 Robot army 🤖!', '🤖', 'foo', 0 );
expected = 'foo🤖 Robot army 🤖!';
t.strictEqual( actual, expected, 'returns expected value' );

actual = replaceBefore( '🐺 Wolf brothers 🐺', 'o', 'foo' );
actual = replaceBefore( '🐺 Wolf brothers 🐺', 'o', 'foo', 0 );
expected = 'fooolf brothers 🐺';
t.strictEqual( actual, expected, 'returns expected value' );

t.end();
});

tape( 'the function replaces the substring before a provided search string (custom start index)', function test( t ) {
var expected;
var actual;
var str;

str = 'beep boop baz';
actual = replaceBefore( str, ' ', 'foo', 6 );
expected = 'foo baz';
t.strictEqual( actual, expected, 'returns expected value' );

str = 'beep boop baz';
actual = replaceBefore( str, 'p', 'foo', 6 );
expected = 'foop baz';
t.strictEqual( actual, expected, 'returns expected value' );

str = 'beep boop baz';
actual = replaceBefore( str, 'beep', 'foo', -2 );
expected = 'foobeep boop baz';
t.strictEqual( actual, expected, 'returns expected value' );

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

t.end();
});

tape( 'the function returns the entire string if the search string is not found', function test( t ) {
var expected;
var actual;

actual = replaceBefore( 'beep boop', 'z', 'foo' );
actual = replaceBefore( 'beep boop', 'z', 'foo', 0 );
expected = 'beep boop';
t.strictEqual( actual, expected, 'returns expected value' );

actual = replaceBefore( 'beep boop', 'baz', 'foo' );
actual = replaceBefore( 'beep boop', 'baz', 'foo', 0 );
expected = 'beep boop';
t.strictEqual( actual, expected, 'returns expected value' );

Expand All @@ -89,7 +117,7 @@ tape( 'the function returns the entire string if the search string is the empty
var expected;
var actual;

actual = replaceBefore( 'beep boop', '', 'foo' );
actual = replaceBefore( 'beep boop', '', 'foo', 0 );
expected = 'beep boop';
t.strictEqual( actual, expected, 'returns expected value' );

Expand Down
2 changes: 1 addition & 1 deletion replace-before/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function replaceBefore( str, search, replacement ) {
if ( !isString( replacement ) ) {
throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', replacement ) );
}
return base( str, search, replacement );
return base( str, search, replacement, 0 );
}


Expand Down

0 comments on commit 96fd74f

Please sign in to comment.