diff --git a/base/replace-before-last/README.md b/base/replace-before-last/README.md new file mode 100644 index 00000000..888586ee --- /dev/null +++ b/base/replace-before-last/README.md @@ -0,0 +1,136 @@ + + +# replaceBeforeLast + +> Replace the substring before the last occurrence of a specified search string. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); +``` + +#### replaceBeforeLast( str, search, replacement, fromIndex ) + +Replaces the substring before the last occurrence of a specified search string. + +```javascript +var str = 'beep boop'; +var out = replaceBeforeLast( str, ' ', 'loop', str.length ); +// returns 'loop boop' + +out = replaceBeforeLast( str, 'o', 'bar', str.length ); +// returns 'barop' +``` + +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. + +```javascript +var str = 'beep boop beep'; +var out = replaceBeforeLast( str, ' ', 'loop', 5 ); +// returns 'loop boop beep' +``` + +
+ + + + + +
+ +## Notes + +- 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`, the function returns the provided string unchanged. + +
+ + + + + +
+ +## Examples + + + +```javascript +var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); + +var str = 'beep boop'; +var out = replaceBeforeLast( str, 'p', 'see', str.length ); +// returns 'seep' + +str = 'Hello World!'; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); +// returns 'Hello World!' + +str = 'Hello World!'; +out = replaceBeforeLast( str, '', 'foo', str.length ); +// returns 'Hello World!' + +str = ''; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); +// returns '' +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/replace-before-last/benchmark/benchmark.js b/base/replace-before-last/benchmark/benchmark.js new file mode 100644 index 00000000..052ee531 --- /dev/null +++ b/base/replace-before-last/benchmark/benchmark.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var replaceBeforeLast = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var str; + var i; + + str = 'To be, or not to be, that is the question.'; + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = replaceBeforeLast( str, 'T', values[ i%values.length ], str.length ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/replace-before-last/docs/repl.txt b/base/replace-before-last/docs/repl.txt new file mode 100644 index 00000000..a2dda150 --- /dev/null +++ b/base/replace-before-last/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( str, search, replacement, fromIndex ) + Replaces the substring before the last occurrence of a specified search + string. + + Parameters + ---------- + str: string + Input string. + + search: string + Search string. + + replacement: string + Replacement string. + + fromIndex: integer + Index from which to start searching. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var str = 'beep boop'; + > var out = {{alias}}( str, ' ', 'foo', str.length ) + 'foo boop' + > out = {{alias}}( str, 'o', 'foo', str.length ) + 'fooop' + > out = {{alias}}( 'Hello World!', 'o', 'foo', 5 ) + 'fooo World!' + + See Also + -------- + diff --git a/base/replace-before-last/docs/types/index.d.ts b/base/replace-before-last/docs/types/index.d.ts new file mode 100644 index 00000000..a89dbdaa --- /dev/null +++ b/base/replace-before-last/docs/types/index.d.ts @@ -0,0 +1,65 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/** +* Replaces the substring before the last occurrence of a specified search string. +* +* @param str - input string +* @param search - search string +* @param replacement - replacement string +* @param fromIndex - index from which to start searching +* @returns output string +* +* @example +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); +* // returns 'foo boop' +* +* @example +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, 'p', 'foo', str.length ); +* // returns 'foop' +* +* @example +* var str = 'Hello World!'; +* var out = replaceBeforeLast( str, '', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'Hello World!'; +* var out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceBeforeLast( str, 'p b', 'foo', str.length ); +* // returns 'foop baz' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceBeforeLast( str, 'p b', 'foo', 6 ); +* // returns 'foop boop baz' +*/ +declare function replaceBeforeLast( str: string, search: string, replacement: string, fromIndex: number ): string; + + +// EXPORTS // + +export = replaceBeforeLast; diff --git a/base/replace-before-last/docs/types/test.ts b/base/replace-before-last/docs/types/test.ts new file mode 100644 index 00000000..4e76a173 --- /dev/null +++ b/base/replace-before-last/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import replaceBeforeLast = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + replaceBeforeLast( 'beep boop', ' ', 'foo', 10 ); // $ExpectType string + replaceBeforeLast( 'beep boop', 'xyz', 'foo', 10 ); // $ExpectType string + replaceBeforeLast( 'beep boop', '', 'foo', 10 ); // $ExpectType string +} + +// The compiler throws an error if the function is provided arguments having invalid types... +{ + replaceBeforeLast( true, 'd', 'foo', 100 ); // $ExpectError + replaceBeforeLast( false, 'd' , 'foo', 100 ); // $ExpectError + replaceBeforeLast( 3, 'd' , 'foo', 100 ); // $ExpectError + replaceBeforeLast( [], 'd' , 'foo', 100 ); // $ExpectError + replaceBeforeLast( {}, 'd' , 'foo', 100 ); // $ExpectError + replaceBeforeLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError + + replaceBeforeLast( 'abc', true, 'foo', 10 ); // $ExpectError + replaceBeforeLast( 'abc', false, 'foo', 10 ); // $ExpectError + replaceBeforeLast( 'abc', 5 , 'foo', 10 ); // $ExpectError + replaceBeforeLast( 'abc', [], 'foo', 10 ); // $ExpectError + replaceBeforeLast( 'abc', {} , 'foo', 10 ); // $ExpectError + replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo', 10 ); // $ExpectError + + replaceBeforeLast( 'abc', 'd', true, 10 ); // $ExpectError + replaceBeforeLast( 'abc', 'd', false, 10 ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 5, 10 ); // $ExpectError + replaceBeforeLast( 'abc', 'd', [], 10 ); // $ExpectError + replaceBeforeLast( 'abc', 'd', {}, 10 ); // $ExpectError + replaceBeforeLast( 'abc', 'd', ( x: number ): number => x, 10 ); // $ExpectError + + replaceBeforeLast( 'abc', 'd', 'foo', true ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', false ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', '5' ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', [] ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', {} ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + replaceBeforeLast(); // $ExpectError + replaceBeforeLast( 'abc' ); // $ExpectError + replaceBeforeLast( 'abc', 'd' ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError +} diff --git a/base/replace-before-last/examples/index.js b/base/replace-before-last/examples/index.js new file mode 100644 index 00000000..7e11cb4c --- /dev/null +++ b/base/replace-before-last/examples/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var replaceBeforeLast = require( './../lib' ); + +var str = 'beep boop'; +var out = replaceBeforeLast( str, 'p', 'see', str.length ); +console.log( out ); +// => 'seep' + +str = 'Hello World!'; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); +console.log( out ); +// => 'Hello World!' + +str = 'Hello World!'; +out = replaceBeforeLast( str, '', 'foo', str.length ); +console.log( out ); +// => 'Hello World!' + +str = ''; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); +console.log( out ); +// => '' + +str = 'beep boop baz'; +out = replaceBeforeLast( str, 'p b', 'foo', str.length ); +console.log( out ); +// => 'foop baz' + +str = 'beep boop baz'; +out = replaceBeforeLast( str, 'p b', 'foo', 6 ); +console.log( out ); +// => 'foop boop baz' diff --git a/base/replace-before-last/lib/index.js b/base/replace-before-last/lib/index.js new file mode 100644 index 00000000..b29877c0 --- /dev/null +++ b/base/replace-before-last/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Replace the substring before the last occurrence of a specified search string. +* +* @module @stdlib/string/base/replace-before-last +* +* @example +* var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); +* +* var str = 'beep boop'; +* +* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); +* // returns 'foo boop' +* +* out = replaceBeforeLast( str, 'o', 'bar', str.length ); +* // returns 'barop' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/replace-before-last/lib/main.js b/base/replace-before-last/lib/main.js new file mode 100644 index 00000000..fd03f8fa --- /dev/null +++ b/base/replace-before-last/lib/main.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Replaces the substring before the last occurrence of a specified search string. +* +* @param {string} str - input string +* @param {string} search - search string +* @param {string} replacement - replacement string +* @param {integer} fromIndex - index from which to start searching +* @returns {string} string +* +* @example +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); +* // returns 'foo boop' +* +* @example +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, 'p', 'foo', str.length ); +* // returns 'foop' +* +* @example +* var str = 'Hello World!'; +* var out = replaceBeforeLast( str, '', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'Hello World!'; +* var out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceBeforeLast( str, 'p b', 'foo', str.length ); +* // returns 'foop baz' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceBeforeLast( str, 'p b', 'foo', 6 ); +* // returns 'foop boop baz' +*/ +function replaceBeforeLast( str, search, replacement, fromIndex ) { + var idx; + if ( fromIndex < 0 ) { + return str; + } + idx = str.lastIndexOf( search, fromIndex ); + if ( str === '' || search === '' || replacement === '' || idx < 0 ) { + return str; + } + return replacement + str.substring( idx ); +} + + +// EXPORTS // + +module.exports = replaceBeforeLast; diff --git a/base/replace-before-last/package.json b/base/replace-before-last/package.json new file mode 100644 index 00000000..ed5cd1dd --- /dev/null +++ b/base/replace-before-last/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/string/base/replace-before-last", + "version": "0.0.0", + "description": "Replace the substring before the last occurrence of a specified search string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "utilities", + "utility", + "utils", + "util", + "base", + "string", + "str", + "replace", + "search", + "substring", + "substr", + "before", + "last", + "match" + ] +} diff --git a/base/replace-before-last/test/test.js b/base/replace-before-last/test/test.js new file mode 100644 index 00000000..d36115b6 --- /dev/null +++ b/base/replace-before-last/test/test.js @@ -0,0 +1,138 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var replaceBeforeLast = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof replaceBeforeLast, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function replaces the substring before the last occurrence of a specified search string', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep boop'; + actual = replaceBeforeLast( str, ' ', 'foo', str.length ); + expected = 'foo boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + actual = replaceBeforeLast( str, 'p', 'foo', str.length ); + expected = 'foop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'Hello, World!'; + actual = replaceBeforeLast( str, 'o', 'foo', str.length ); + expected = 'fooorld!'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function replaces the substring before the last occurrence of a specified search string (Unicode characters)', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep 😀 boop 😀 baz'; + actual = replaceBeforeLast( str, '😀', 'foo', str.length ); + expected = 'foo😀 baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🤖 Robot army 🤖!'; + actual = replaceBeforeLast( str, '🤖', 'foo', str.length ); + expected = 'foo🤖!'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf brothers 🐺'; + actual = replaceBeforeLast( str, 'o', 'foo', str.length ); + expected = 'fooothers 🐺'; + 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 = replaceBeforeLast( str, ' ', 'foo', 9 ); + expected = 'foo baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop baz'; + actual = replaceBeforeLast( str, 'p', 'foo', 6 ); + expected = 'foop boop baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop baz'; + actual = replaceBeforeLast( str, 'beep', 'foo', -2 ); + expected = 'beep boop baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop baz'; + actual = replaceBeforeLast( str, 'beep', 'foo', 20 ); + expected = 'foobeep 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; + var str; + + str = 'beep boop'; + actual = replaceBeforeLast( str, 'z', 'foo', str.length ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + actual = replaceBeforeLast( str, 'baz', 'foo', str.length ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the entire string if the search string is the empty string', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep boop'; + actual = replaceBeforeLast( str, '', 'foo', str.length ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +});