diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/README.md b/lib/node_modules/@stdlib/string/base/replace-after-last/README.md new file mode 100644 index 00000000000..25770239061 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-last/README.md @@ -0,0 +1,132 @@ + + +# replaceAfterLast + +> Replace the substring after the last occurrence of a specified search string. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var replaceAfterLast = require( '@stdlib/string/base/replace-after-last' ); +``` + +#### replaceAfterLast( str, search, replacement, fromIndex ) + +Replaces the substring after the last occurrence of a specified search string. + +```javascript +var str = 'beep boop'; +var out = replaceAfterLast( str, ' ', 'loop', str.length ); +// returns 'beep loop' + +out = replaceAfterLast( str, 'o', 'bar', str.length ); +// returns 'beep boobar' +``` + +
+ + + + + +
+ +## 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 replaceAfterLast = require( '@stdlib/string/base/replace-after-last' ); + +var str = 'beep boop'; +var out = replaceAfterLast( str, 'p', 'see', str.length ); +// returns 'beep boopsee' + +str = 'Hello World!'; +out = replaceAfterLast( str, 'xyz', 'foo', str.length ); +// returns 'Hello World!' + +str = 'Hello World!'; +out = replaceAfterLast( str, '', 'foo', str.length ); +// returns 'Hello World!' + +str = ''; +out = replaceAfterLast( str, 'xyz', 'foo', str.length ); +// returns '' + +str = 'beep boop beep baz'; +out = replaceAfterLast( str, 'beep', 'foo', 5 ); +// return 'beepfoo' +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/replace-after-last/benchmark/benchmark.js new file mode 100644 index 00000000000..5c83fa02335 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 replaceAfterLast = 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 = replaceAfterLast( 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/lib/node_modules/@stdlib/string/base/replace-after-last/docs/repl.txt b/lib/node_modules/@stdlib/string/base/replace-after-last/docs/repl.txt new file mode 100644 index 00000000000..dceadc4889f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-last/docs/repl.txt @@ -0,0 +1,37 @@ + +{{alias}}( str, search, replacement, fromIndex ) + Replaces the substring after 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 ) + 'beep foo' + > out = {{alias}}( str, 'o', 'foo', str.length ) + 'beep boofoo' + > out = {{alias}}( 'Hello World!', 'o', 'foo', 5 ) + 'Hellofoo' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-after-last/docs/types/index.d.ts new file mode 100644 index 00000000000..812e5112b62 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 after 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 = replaceAfterLast( str, ' ', 'foo', str.length ); +* // returns 'beep foo' +* +* @example +* var str = 'beep boop'; +* var out = replaceAfterLast( str, 'p', 'foo', str.length ); +* // returns 'beep boopfoo' +* +* @example +* var str = 'Hello World!'; +* var out = replaceAfterLast( str, '', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'Hello World!'; +* var out = replaceAfterLast( str, 'xyz', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceAfterLast( str, 'p b', 'foo', str.length ); +* // returns 'beep boop bfoo' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceAfterLast( str, 'p b', 'foo', 6 ); +* // returns 'beep bfoo' +*/ +declare function replaceAfterLast( str: string, search: string, replacement: string, fromIndex: number ): string; + + +// EXPORTS // + +export = replaceAfterLast; diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/replace-after-last/docs/types/test.ts new file mode 100644 index 00000000000..5e008fd2d32 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 replaceAfterLast = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + replaceAfterLast( 'beep boop', ' ', 'foo', 10 ); // $ExpectType string + replaceAfterLast( 'beep boop', 'xyz', 'foo', 10 ); // $ExpectType string + replaceAfterLast( 'beep boop', '', 'foo', 10 ); // $ExpectType string +} + +// The compiler throws an error if the function is provided arguments having invalid types... +{ + replaceAfterLast( true, 'd', 'foo', 100 ); // $ExpectError + replaceAfterLast( false, 'd' , 'foo', 100 ); // $ExpectError + replaceAfterLast( 3, 'd' , 'foo', 100 ); // $ExpectError + replaceAfterLast( [], 'd' , 'foo', 100 ); // $ExpectError + replaceAfterLast( {}, 'd' , 'foo', 100 ); // $ExpectError + replaceAfterLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError + + replaceAfterLast( 'abc', true, 'foo', 10 ); // $ExpectError + replaceAfterLast( 'abc', false, 'foo', 10 ); // $ExpectError + replaceAfterLast( 'abc', 5 , 'foo', 10 ); // $ExpectError + replaceAfterLast( 'abc', [], 'foo', 10 ); // $ExpectError + replaceAfterLast( 'abc', {} , 'foo', 10 ); // $ExpectError + replaceAfterLast( 'abc', ( x: number ): number => x , 'foo', 10 ); // $ExpectError + + replaceAfterLast( 'abc', 'd', true, 10 ); // $ExpectError + replaceAfterLast( 'abc', 'd', false, 10 ); // $ExpectError + replaceAfterLast( 'abc', 'd', 5, 10 ); // $ExpectError + replaceAfterLast( 'abc', 'd', [], 10 ); // $ExpectError + replaceAfterLast( 'abc', 'd', {}, 10 ); // $ExpectError + replaceAfterLast( 'abc', 'd', ( x: number ): number => x, 10 ); // $ExpectError + + replaceAfterLast( 'abc', 'd', 'foo', true ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo', false ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo', '5' ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo', [] ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo', {} ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + replaceAfterLast(); // $ExpectError + replaceAfterLast( 'abc' ); // $ExpectError + replaceAfterLast( 'abc', 'd' ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo' ); // $ExpectError + replaceAfterLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/examples/index.js b/lib/node_modules/@stdlib/string/base/replace-after-last/examples/index.js new file mode 100644 index 00000000000..48acaabbb17 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 replaceAfterLast = require( './../lib' ); + +var str = 'beep boop'; +var out = replaceAfterLast( str, 'p', 'see', str.length ); +console.log( out ); +// => 'beep boopsee' + +str = 'Hello World!'; +out = replaceAfterLast( str, 'xyz', 'foo', str.length ); +console.log( out ); +// => 'Hello World!' + +str = 'Hello World!'; +out = replaceAfterLast( str, '', 'foo', str.length ); +console.log( out ); +// => 'Hello World!' + +str = ''; +out = replaceAfterLast( str, 'xyz', 'foo', str.length ); +console.log( out ); +// => '' + +str = 'beep boop baz'; +out = replaceAfterLast( str, 'p b', 'foo', str.length ); +console.log( out ); +// => 'beep boop bfoo' + +str = 'beep boop baz'; +out = replaceAfterLast( str, 'p b', 'foo', 6 ); +console.log( out ); +// => 'beep bfoo' diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/lib/index.js b/lib/node_modules/@stdlib/string/base/replace-after-last/lib/index.js new file mode 100644 index 00000000000..533d2b95825 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 after the last occurrence of a specified search string. +* +* @module @stdlib/string/base/replace-after-last +* +* @example +* var replaceAfterLast = require( '@stdlib/string/base/replace-after-last' ); +* +* var str = 'beep boop'; +* +* var out = replaceAfterLast( str, ' ', 'foo', str.length ); +* // returns 'beep foo' +* +* out = replaceAfterLast( str, 'o', 'bar', str.length ); +* // returns 'beep boobar' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-after-last/lib/main.js new file mode 100644 index 00000000000..cfc8d37ece8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 after 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 = replaceAfterLast( str, ' ', 'foo', str.length ); +* // returns 'beep foo' +* +* @example +* var str = 'beep boop'; +* var out = replaceAfterLast( str, 'p', 'foo', str.length ); +* // returns 'beep boopfoo' +* +* @example +* var str = 'Hello World!'; +* var out = replaceAfterLast( str, '', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'Hello World!'; +* var out = replaceAfterLast( str, 'xyz', 'foo', str.length ); +* // returns 'Hello World!' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceAfterLast( str, 'p b', 'foo', str.length ); +* // returns 'beep boop bfoo' +* +* @example +* var str = 'beep boop baz'; +* var out = replaceAfterLast( str, 'p b', 'foo', 6 ); +* // returns 'beep bfoo' +*/ +function replaceAfterLast( 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 str.substring( 0, idx + search.length ) + replacement; +} + + +// EXPORTS // + +module.exports = replaceAfterLast; diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/package.json b/lib/node_modules/@stdlib/string/base/replace-after-last/package.json new file mode 100644 index 00000000000..f6269b2751f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-last/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/string/base/replace-after-last", + "version": "0.0.0", + "description": "Replace the substring after 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", + "after", + "last", + "match" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/replace-after-last/test/test.js b/lib/node_modules/@stdlib/string/base/replace-after-last/test/test.js new file mode 100644 index 00000000000..1036682dd25 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after-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 replaceAfterLast = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof replaceAfterLast, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function replaces the substring after the last occurrence of a specified search string', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep boop'; + actual = replaceAfterLast( str, ' ', 'foo', str.length ); + expected = 'beep foo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + actual = replaceAfterLast( str, 'p', 'foo', str.length ); + expected = 'beep boopfoo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'Hello, World!'; + actual = replaceAfterLast( str, 'o', 'foo', str.length ); + expected = 'Hello, Wofoo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function replaces the substring after the last occurrence of a specified search string (Unicode characters)', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep 😀 boop 😀 baz'; + actual = replaceAfterLast( str, '😀', 'foo', str.length ); + expected = 'beep 😀 boop 😀foo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🤖 Robot army 🤖!'; + actual = replaceAfterLast( str, '🤖', 'foo', str.length ); + expected = '🤖 Robot army 🤖foo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf brothers 🐺'; + actual = replaceAfterLast( str, 'o', 'foo', str.length ); + expected = '🐺 Wolf brofoo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function replaces the substring after a provided search string (custom start index)', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep boop baz'; + actual = replaceAfterLast( str, ' ', 'foo', 6 ); + expected = 'beep foo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop baz'; + actual = replaceAfterLast( str, 'p', 'foo', 6 ); + expected = 'beepfoo'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop baz'; + actual = replaceAfterLast( str, 'beep', 'foo', -2 ); + expected = 'beep boop baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop baz'; + actual = replaceAfterLast( str, 'beep', 'foo', 20 ); + expected = 'beepfoo'; + 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 = replaceAfterLast( str, 'z', 'foo', str.length ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + actual = replaceAfterLast( 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 = replaceAfterLast( str, '', 'foo', str.length ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +});