From 8781d06c631d8f03eb51ad51ede287b67a953deb Mon Sep 17 00:00:00 2001 From: AuenKr Date: Sat, 24 Feb 2024 11:38:51 +0530 Subject: [PATCH 01/12] feat: add @stdlib/string/base/replace-before-last --- .../string/base/replace-before-last/README.md | 122 ++++++++++++++++++ .../benchmark/benchmark.js | 58 +++++++++ .../base/replace-before-last/docs/repl.txt | 32 +++++ .../replace-before-last/docs/types/index.d.ts | 50 +++++++ .../replace-before-last/docs/types/test.ts | 60 +++++++++ .../replace-before-last/examples/index.js | 37 ++++++ .../base/replace-before-last/lib/index.js | 45 +++++++ .../base/replace-before-last/lib/main.js | 58 +++++++++ .../base/replace-before-last/package.json | 69 ++++++++++ .../base/replace-before-last/test/test.js | 97 ++++++++++++++ 10 files changed, 628 insertions(+) create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/README.md create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/package.json create mode 100644 lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md new file mode 100644 index 000000000000..c00e9cf62f55 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md @@ -0,0 +1,122 @@ + + +# 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 ) + +Replaces the substring before the last occurrence of a specified search string. + +```javascript +var out = replaceBeforeLast( 'beep boop', ' ', 'loop' ); +// returns 'loop boop' + +out = replaceBeforeLast( 'beep boop', 'o', 'bar' ); +// returns 'barop' +``` + +
+ + + + + +
+ +## 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. + +
+ + + + + +
+ +## Examples + + + +```javascript +var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); + +var out = replaceBeforeLast( 'beep boop', 'p', 'see' ); +// returns 'seep' + +out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +// returns 'Hello World!' + +out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +// returns 'Hello World!' + +out = replaceBeforeLast( '', 'xyz', 'foo'); +// returns '' +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js new file mode 100644 index 000000000000..f496c34e87e4 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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, '.', values[ i%values.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-before-last/docs/repl.txt b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt new file mode 100644 index 000000000000..b56d393c3980 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( str, search, replacement ) + 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. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var str = 'beep boop'; + > var out = {{alias}}( str, ' ', 'foo' ) + 'foo boop' + > out = {{alias}}( str, 'o', 'foo' ) + 'fooop' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts new file mode 100644 index 000000000000..966d7e21ee4c --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 +* @returns output string +* +* @example +* var out = replaceBeforeLast( 'beep boop', ' ', 'foo' ); +* // returns 'foo boop' +* +* @example +* var out = replaceBeforeLast( 'beep boop', 'p', 'foo' ); +* // returns 'foop' +* +* @example +* var out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +* // returns 'Hello world!' +* +* @example +* var out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +* // returns 'Hello World!' +*/ +declare function replaceBeforeLast( str: string, search: string, replacement: string ): string; + + +// EXPORTS // + +export = replaceBeforeLast; diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts new file mode 100644 index 000000000000..04a413c63c34 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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' ); // $ExpectType string + replaceBeforeLast( 'beep boop', 'xyz', 'foo' ); // $ExpectType string + replaceBeforeLast( 'beep boop', '', 'foo' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided arguments having invalid types... +{ + replaceBeforeLast( true, 'd', 'foo' ); // $ExpectError + replaceBeforeLast( false, 'd' , 'foo' ); // $ExpectError + replaceBeforeLast( 3, 'd' , 'foo' ); // $ExpectError + replaceBeforeLast( [], 'd' , 'foo' ); // $ExpectError + replaceBeforeLast( {}, 'd' , 'foo' ); // $ExpectError + replaceBeforeLast( ( x: number ): number => x, 'd', 'foo' ); // $ExpectError + + replaceBeforeLast( 'abc', true, 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', false, 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', 5 , 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', [], 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', {} , 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo' ); // $ExpectError + + replaceBeforeLast( 'abc', 'd', true ); // $ExpectError + replaceBeforeLast( 'abc', 'd', false ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 5 ); // $ExpectError + replaceBeforeLast( 'abc', 'd', [] ); // $ExpectError + replaceBeforeLast( 'abc', 'd', {} ); // $ExpectError + replaceBeforeLast( 'abc', 'd', ( 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 +} diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js new file mode 100644 index 000000000000..1cbe871746ca --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 out = replaceBeforeLast( 'beep boop', 'p', 'see' ); +console.log( out ); +// => 'seep' + +out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +console.log( out ); +// => 'Hello World!' + +out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +console.log( out ); +// => 'Hello World!' + +out = replaceBeforeLast( '', 'xyz', 'foo' ); +console.log( out ); +// => '' diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js new file mode 100644 index 000000000000..ec5212e358f9 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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' ); +* // returns 'foo boop' +* +* out = replaceBeforeLast( str, 'o', 'bar' ); +* // returns 'barop' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js new file mode 100644 index 000000000000..61e715894fb5 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js @@ -0,0 +1,58 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 +* @returns {string} string +* +* @example +* var out = replaceBeforeLast( 'beep boop', ' ', 'foo' ); +* // returns 'foo boop' +* +* @example +* var out = replaceBeforeLast( 'beep boop', 'p', 'foo' ); +* // returns 'foop' +* +* @example +* var out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +* // returns 'Hello World!' +* +* @example +* var out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +* // returns 'Hello World!' +*/ +function replaceBeforeLast( str, search, replacement ) { + var idx = str.lastIndexOf( search ); + if ( str === '' || search === '' || replacement === '' || idx < 0 ) { + return str; + } + return replacement + str.substring( idx ); +} + + +// EXPORTS // + +module.exports = replaceBeforeLast; diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/package.json b/lib/node_modules/@stdlib/string/base/replace-before-last/package.json new file mode 100644 index 000000000000..ed5cd1ddfb0b --- /dev/null +++ b/lib/node_modules/@stdlib/string/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/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js new file mode 100644 index 000000000000..8c19256290dd --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js @@ -0,0 +1,97 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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; + + actual = replaceBeforeLast( 'beep boop', ' ', 'foo' ); + expected = 'foo boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + actual = replaceBeforeLast( 'beep boop', 'p', 'foo' ); + expected = 'foop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + actual = replaceBeforeLast( 'Hello, World!', 'o', 'foo' ); + 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; + + actual = replaceBeforeLast( 'beep 😀 boop 😀 baz', '😀', 'foo' ); + expected = 'foo😀 baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + actual = replaceBeforeLast( '🤖 Robot army 🤖!', '🤖', 'foo' ); + expected = 'foo🤖!'; + t.strictEqual( actual, expected, 'returns expected value' ); + + actual = replaceBeforeLast( '🐺 Wolf brothers 🐺', 'o', 'foo' ); + expected = 'fooothers 🐺'; + 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 = replaceBeforeLast( 'beep boop', 'z', 'foo' ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + actual = replaceBeforeLast( 'beep boop', 'baz', 'foo' ); + 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; + + actual = replaceBeforeLast( 'beep boop', '', 'foo' ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); From cb40731fd69fd2d23c0cc2332acdbd0d49c7d966 Mon Sep 17 00:00:00 2001 From: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Date: Sat, 24 Feb 2024 22:52:42 +0000 Subject: [PATCH 02/12] chore: update copyright years --- .../@stdlib/string/base/replace-before-last/README.md | 2 +- .../string/base/replace-before-last/benchmark/benchmark.js | 2 +- .../string/base/replace-before-last/docs/types/index.d.ts | 2 +- .../@stdlib/string/base/replace-before-last/docs/types/test.ts | 2 +- .../@stdlib/string/base/replace-before-last/examples/index.js | 2 +- .../@stdlib/string/base/replace-before-last/lib/index.js | 2 +- .../@stdlib/string/base/replace-before-last/lib/main.js | 2 +- .../@stdlib/string/base/replace-before-last/test/test.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md index c00e9cf62f55..5092d187bc43 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2023 The Stdlib Authors. +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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js index f496c34e87e4..f3c88add4511 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts index 966d7e21ee4c..4a25acbd5e07 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts index 04a413c63c34..406f82cacd09 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js index 1cbe871746ca..3a770469fc75 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js index ec5212e358f9..f45446c00fcc 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js index 61e715894fb5..1649645e5e21 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js index 8c19256290dd..628098622a50 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2023 The Stdlib Authors. +* 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. From 1b4eed856cf9a5473446fdf75e381fac29baa4fb Mon Sep 17 00:00:00 2001 From: AuenKr Date: Sun, 25 Feb 2024 18:39:49 +0530 Subject: [PATCH 03/12] fixup! feat: add fromIndex argument --- .../string/base/replace-before-last/README.md | 23 ++++--- .../benchmark/benchmark.js | 2 +- .../base/replace-before-last/docs/repl.txt | 11 +++- .../replace-before-last/docs/types/index.d.ts | 27 ++++++-- .../replace-before-last/docs/types/test.ts | 55 +++++++++------- .../replace-before-last/examples/index.js | 22 +++++-- .../base/replace-before-last/lib/index.js | 6 +- .../base/replace-before-last/lib/main.js | 31 +++++++-- .../base/replace-before-last/test/test.js | 64 +++++++++++++++---- 9 files changed, 175 insertions(+), 66 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md index 5092d187bc43..fbd7ad17aa44 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md @@ -40,15 +40,17 @@ limitations under the License. var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); ``` -#### replaceBeforeLast( str, search, replacement ) +#### replaceBeforeLast( str, search, replacement, fromIndex ) -Replaces the substring before the last occurrence of a specified search string. +- Replaces the substring after the last occurrence of a specified search string. +- fromIndex is index of last character to be considered beginning of a match. ```javascript -var out = replaceBeforeLast( 'beep boop', ' ', 'loop' ); +var str = 'beep boop'; +var out = replaceBeforeLast( str, ' ', 'loop', str.length ); // returns 'loop boop' -out = replaceBeforeLast( 'beep boop', 'o', 'bar' ); +out = replaceBeforeLast( str, 'o', 'bar', str.length ); // returns 'barop' ``` @@ -64,6 +66,7 @@ out = replaceBeforeLast( '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 search string length, the function returns the provided string unchanged. @@ -80,16 +83,20 @@ out = replaceBeforeLast( 'beep boop', 'o', 'bar' ); ```javascript var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); -var out = replaceBeforeLast( 'beep boop', 'p', 'see' ); +var str = 'beep boop'; +var out = replaceBeforeLast( str, 'p', 'see', str.length ); // returns 'seep' -out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +str = 'Hello World!'; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); // returns 'Hello World!' -out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +str = 'Hello World!'; +out = replaceBeforeLast( str, '', 'foo', str.length ); // returns 'Hello World!' -out = replaceBeforeLast( '', 'xyz', 'foo'); +str = ''; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); // returns '' ``` diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js index f3c88add4511..8d693e14fe53 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js @@ -44,7 +44,7 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = replaceBeforeLast( str, '.', values[ i%values.length ] ); + out = replaceBeforeLast( str, '.', values[ i%values.length ], str.length ); if ( typeof out !== 'string' ) { b.fail( 'should return a string' ); } diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt index b56d393c3980..df89f62c44bf 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( str, search, replacement ) +{{alias}}( str, search, replacement, fromIndex ) Replaces the substring before the last occurrence of a specified search string. @@ -14,6 +14,9 @@ replacement: string Replacement string. + fromIndex: integer + Index of last character to be considered beginning of a match. + Returns ------- out: string @@ -22,10 +25,12 @@ Examples -------- > var str = 'beep boop'; - > var out = {{alias}}( str, ' ', 'foo' ) + > var out = {{alias}}( str, ' ', 'foo', str.length ) 'foo boop' - > out = {{alias}}( str, 'o', 'foo' ) + > out = {{alias}}( str, 'o', 'foo', str.length ) 'fooop' + > out = {{alias}}( 'Hello World!', 'o', 'foo', 5 ) + 'fooo World!' See Also -------- diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts index 4a25acbd5e07..6cdc738cb7eb 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts @@ -24,25 +24,40 @@ * @param str - input string * @param search - search string * @param replacement - replacement string +* @param fromIndex - index of last character to be considered beginning of a match * @returns output string * * @example -* var out = replaceBeforeLast( 'beep boop', ' ', 'foo' ); +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); * // returns 'foo boop' * * @example -* var out = replaceBeforeLast( 'beep boop', 'p', 'foo' ); +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, 'p', 'foo', str.length ); * // returns 'foop' * * @example -* var out = replaceBeforeLast( 'Hello World!', '', 'foo' ); -* // returns 'Hello world!' +* var str = 'Hello World!'; +* var out = replaceBeforeLast( str, '', 'foo', str.length); +* // returns 'Hello World!' * * @example -* var out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +* 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 ): string; +declare function replaceBeforeLast( str: string, search: string, replacement: string, fromIndex: number ): string; // EXPORTS // diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts index 406f82cacd09..b404cf41da0e 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts @@ -23,33 +23,40 @@ import replaceBeforeLast = require( './index' ); // The function returns a string... { - replaceBeforeLast( 'beep boop', ' ', 'foo' ); // $ExpectType string - replaceBeforeLast( 'beep boop', 'xyz', 'foo' ); // $ExpectType string - replaceBeforeLast( 'beep boop', '', 'foo' ); // $ExpectType string + replaceBeforeLast( 'beep boop', ' ', 'foo', 'beep boop'.length ); // $ExpectType string + replaceBeforeLast( 'beep boop', 'xyz', 'foo', 'beep boop'.length ); // $ExpectType string + replaceBeforeLast( 'beep boop', '', 'foo', 'beep boop'.length ); // $ExpectType string } // The compiler throws an error if the function is provided arguments having invalid types... { - replaceBeforeLast( true, 'd', 'foo' ); // $ExpectError - replaceBeforeLast( false, 'd' , 'foo' ); // $ExpectError - replaceBeforeLast( 3, 'd' , 'foo' ); // $ExpectError - replaceBeforeLast( [], 'd' , 'foo' ); // $ExpectError - replaceBeforeLast( {}, 'd' , 'foo' ); // $ExpectError - replaceBeforeLast( ( x: number ): number => x, 'd', 'foo' ); // $ExpectError - - replaceBeforeLast( 'abc', true, 'foo' ); // $ExpectError - replaceBeforeLast( 'abc', false, 'foo' ); // $ExpectError - replaceBeforeLast( 'abc', 5 , 'foo' ); // $ExpectError - replaceBeforeLast( 'abc', [], 'foo' ); // $ExpectError - replaceBeforeLast( 'abc', {} , 'foo' ); // $ExpectError - replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo' ); // $ExpectError - - replaceBeforeLast( 'abc', 'd', true ); // $ExpectError - replaceBeforeLast( 'abc', 'd', false ); // $ExpectError - replaceBeforeLast( 'abc', 'd', 5 ); // $ExpectError - replaceBeforeLast( 'abc', 'd', [] ); // $ExpectError - replaceBeforeLast( 'abc', 'd', {} ); // $ExpectError - replaceBeforeLast( 'abc', 'd', ( x: number ): number => x ); // $ExpectError + 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', 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', false, 'foo', 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', 5 , 'foo', 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', [], 'foo', 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', {} , 'foo', 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo', 'abc'.length ); // $ExpectError + + replaceBeforeLast( 'abc', 'd', true, 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', 'd', false, 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 5, 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', 'd', [], 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', 'd', {}, 'abc'.length ); // $ExpectError + replaceBeforeLast( 'abc', 'd', ( x: number ): number => x, 'abc'.length ); // $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... @@ -57,4 +64,6 @@ import replaceBeforeLast = require( './index' ); replaceBeforeLast(); // $ExpectError replaceBeforeLast( 'abc' ); // $ExpectError replaceBeforeLast( 'abc', 'd' ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo' ); // $ExpectError + replaceBeforeLast( 'abc', 'd', 'foo', 4, 4 ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js index 3a770469fc75..7e11cb4c9403 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/examples/index.js @@ -20,18 +20,32 @@ var replaceBeforeLast = require( './../lib' ); -var out = replaceBeforeLast( 'beep boop', 'p', 'see' ); +var str = 'beep boop'; +var out = replaceBeforeLast( str, 'p', 'see', str.length ); console.log( out ); // => 'seep' -out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +str = 'Hello World!'; +out = replaceBeforeLast( str, 'xyz', 'foo', str.length ); console.log( out ); // => 'Hello World!' -out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +str = 'Hello World!'; +out = replaceBeforeLast( str, '', 'foo', str.length ); console.log( out ); // => 'Hello World!' -out = replaceBeforeLast( '', 'xyz', 'foo' ); +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/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js index f45446c00fcc..6f9b58742404 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js @@ -28,11 +28,11 @@ * * var str = 'beep boop'; * -* var out = replaceBeforeLast( str, ' ', 'foo' ); +* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); * // returns 'foo boop' * -* out = replaceBeforeLast( str, 'o', 'bar' ); -* // returns 'barop' +* out = replaceBeforeLast( str, 'o', 'bar', str.length ); +* // returns 'fooop' */ // MODULES // diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js index 1649645e5e21..8fcfa63b2bde 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js @@ -26,26 +26,45 @@ * @param {string} str - input string * @param {string} search - search string * @param {string} replacement - replacement string +* @param {integer} fromIndex - index of last character to be considered beginning of a match * @returns {string} string * * @example -* var out = replaceBeforeLast( 'beep boop', ' ', 'foo' ); +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, ' ', 'foo', str.length ); * // returns 'foo boop' * * @example -* var out = replaceBeforeLast( 'beep boop', 'p', 'foo' ); +* var str = 'beep boop'; +* var out = replaceBeforeLast( str, 'p', 'foo', str.length ); * // returns 'foop' * * @example -* var out = replaceBeforeLast( 'Hello World!', '', 'foo' ); +* var str = 'Hello World!'; +* var out = replaceBeforeLast( str, '', 'foo', str.length); * // returns 'Hello World!' * * @example -* var out = replaceBeforeLast( 'Hello World!', 'xyz', 'foo' ); +* 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 ) { - var idx = str.lastIndexOf( search ); +function replaceBeforeLast( str, search, replacement, fromIndex ) { + var idx; + if ( fromIndex < 0 || fromIndex > str.length ) { + return str; + } + idx = str.lastIndexOf( search, fromIndex ); if ( str === '' || search === '' || replacement === '' || idx < 0 ) { return str; } diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js index 628098622a50..fe7341681d2f 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js @@ -32,53 +32,91 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function replaces the substring before the last occurrence of a specified search string', function test( t ) { +tape( 'the function replaces the substring after the last occurrence of a specified search string', function test( t ) { var expected; var actual; - - actual = replaceBeforeLast( 'beep boop', ' ', 'foo' ); + var str; + str = 'beep boop'; + actual = replaceBeforeLast( str, ' ', 'foo', str.length ); expected = 'foo boop'; t.strictEqual( actual, expected, 'returns expected value' ); - actual = replaceBeforeLast( 'beep boop', 'p', 'foo' ); + str = 'beep boop'; + actual = replaceBeforeLast( str, 'p', 'foo', str.length ); expected = 'foop'; t.strictEqual( actual, expected, 'returns expected value' ); - actual = replaceBeforeLast( 'Hello, World!', 'o', 'foo' ); + 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 ) { +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; - actual = replaceBeforeLast( 'beep 😀 boop 😀 baz', '😀', 'foo' ); + str = 'beep 😀 boop 😀 baz'; + actual = replaceBeforeLast( str, '😀', 'foo', str.length ); expected = 'foo😀 baz'; t.strictEqual( actual, expected, 'returns expected value' ); - actual = replaceBeforeLast( '🤖 Robot army 🤖!', '🤖', 'foo' ); + str = '🤖 Robot army 🤖!'; + actual = replaceBeforeLast( str, '🤖', 'foo', str.length ); expected = 'foo🤖!'; t.strictEqual( actual, expected, 'returns expected value' ); - actual = replaceBeforeLast( '🐺 Wolf brothers 🐺', 'o', 'foo' ); + 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 after a provided search string (custom start index)', function test( t ) { + var expected; + var actual; + var str; + + str = 'beep boop baz'; + actual = replaceBeforeLast( str, ' ', 'foo', 6 ); + expected = 'foo boop 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 = '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; + var str; - actual = replaceBeforeLast( 'beep boop', 'z', 'foo' ); + str = 'beep boop'; + actual = replaceBeforeLast( str, 'z', 'foo', str.length ); expected = 'beep boop'; t.strictEqual( actual, expected, 'returns expected value' ); - actual = replaceBeforeLast( 'beep boop', 'baz', 'foo' ); + str = 'beep boop'; + actual = replaceBeforeLast( str, 'baz', 'foo', str.length ); expected = 'beep boop'; t.strictEqual( actual, expected, 'returns expected value' ); @@ -88,8 +126,10 @@ tape( 'the function returns the entire string if the search string is not found' tape( 'the function returns the entire string if the search string is the empty string', function test( t ) { var expected; var actual; + var str; - actual = replaceBeforeLast( 'beep boop', '', 'foo' ); + str = 'beep boop'; + actual = replaceBeforeLast( str, '', 'foo', str.length ); expected = 'beep boop'; t.strictEqual( actual, expected, 'returns expected value' ); From b29554e58c71e24fd67202ab6a8fa8aa343e39e7 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 25 Feb 2024 15:25:26 -0800 Subject: [PATCH 04/12] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/string/base/replace-before-last/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md index fbd7ad17aa44..6679f46255f4 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md @@ -42,8 +42,7 @@ var replaceBeforeLast = require( '@stdlib/string/base/replace-before-last' ); #### replaceBeforeLast( str, search, replacement, fromIndex ) -- Replaces the substring after the last occurrence of a specified search string. -- fromIndex is index of last character to be considered beginning of a match. +Replaces the substring before the last occurrence of a specified search string. ```javascript var str = 'beep boop'; @@ -66,7 +65,7 @@ out = replaceBeforeLast( str, 'o', 'bar', str.length ); - 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 search string length, the function returns the provided string unchanged. +- If `fromIndex` is less than `0` or greater than `search` string length, the function returns the provided string unchanged. From 8dd078f96c24dfe31910627b05881e1d2a3d0b4f Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 25 Feb 2024 15:25:53 -0800 Subject: [PATCH 05/12] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/string/base/replace-before-last/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md index 6679f46255f4..06cfc02e612a 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md @@ -65,7 +65,7 @@ out = replaceBeforeLast( str, 'o', 'bar', str.length ); - 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 `search` string length, the function returns the provided string unchanged. +- If `fromIndex` is less than `0` or greater than the `search` string length, the function returns the provided string unchanged. From bb2322acc8f3cf21f7c651a39c079883a91b75ab Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 25 Feb 2024 15:28:46 -0800 Subject: [PATCH 06/12] Apply suggestions from code review Signed-off-by: Athan --- .../string/base/replace-before-last/benchmark/benchmark.js | 2 +- .../@stdlib/string/base/replace-before-last/docs/repl.txt | 2 +- .../string/base/replace-before-last/docs/types/index.d.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js index 8d693e14fe53..052ee531423e 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/benchmark/benchmark.js @@ -44,7 +44,7 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = replaceBeforeLast( str, '.', values[ i%values.length ], str.length ); + out = replaceBeforeLast( str, 'T', values[ i%values.length ], str.length ); if ( typeof out !== 'string' ) { b.fail( 'should return a string' ); } diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt index df89f62c44bf..a2dda15022fe 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/repl.txt @@ -15,7 +15,7 @@ Replacement string. fromIndex: integer - Index of last character to be considered beginning of a match. + Index from which to start searching. Returns ------- diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts index 6cdc738cb7eb..e24f91d6b9e1 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts @@ -24,7 +24,7 @@ * @param str - input string * @param search - search string * @param replacement - replacement string -* @param fromIndex - index of last character to be considered beginning of a match +* @param fromIndex - index from which to start searching * @returns output string * * @example From 3bff6ef314661c6e956d527aff6cecb29f628b5a Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 25 Feb 2024 15:31:39 -0800 Subject: [PATCH 07/12] Apply suggestions from code review Signed-off-by: Athan --- .../replace-before-last/docs/types/test.ts | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts index b404cf41da0e..4e76a173a622 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/test.ts @@ -23,9 +23,9 @@ import replaceBeforeLast = require( './index' ); // The function returns a string... { - replaceBeforeLast( 'beep boop', ' ', 'foo', 'beep boop'.length ); // $ExpectType string - replaceBeforeLast( 'beep boop', 'xyz', 'foo', 'beep boop'.length ); // $ExpectType string - replaceBeforeLast( 'beep boop', '', 'foo', 'beep boop'.length ); // $ExpectType 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... @@ -37,19 +37,19 @@ import replaceBeforeLast = require( './index' ); replaceBeforeLast( {}, 'd' , 'foo', 100 ); // $ExpectError replaceBeforeLast( ( x: number ): number => x, 'd', 'foo', 100 ); // $ExpectError - replaceBeforeLast( 'abc', true, 'foo', 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', false, 'foo', 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', 5 , 'foo', 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', [], 'foo', 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', {} , 'foo', 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', ( x: number ): number => x , 'foo', 'abc'.length ); // $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, 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', 'd', false, 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', 'd', 5, 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', 'd', [], 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', 'd', {}, 'abc'.length ); // $ExpectError - replaceBeforeLast( 'abc', 'd', ( x: number ): number => x, 'abc'.length ); // $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 From 17283f53a87c3334feeecdc0b870d21420407e44 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 25 Feb 2024 15:33:15 -0800 Subject: [PATCH 08/12] Apply suggestions from code review Signed-off-by: Athan --- .../string/base/replace-before-last/docs/types/index.d.ts | 2 +- .../@stdlib/string/base/replace-before-last/lib/main.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts index e24f91d6b9e1..7dd66c1bca82 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts @@ -39,7 +39,7 @@ * * @example * var str = 'Hello World!'; -* var out = replaceBeforeLast( str, '', 'foo', str.length); +* var out = replaceBeforeLast( str, '', 'foo', str.length ); * // returns 'Hello World!' * * @example diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js index 8fcfa63b2bde..dc6ea9b9eee3 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js @@ -26,7 +26,7 @@ * @param {string} str - input string * @param {string} search - search string * @param {string} replacement - replacement string -* @param {integer} fromIndex - index of last character to be considered beginning of a match +* @param {integer} fromIndex - index from which to start searching * @returns {string} string * * @example @@ -41,7 +41,7 @@ * * @example * var str = 'Hello World!'; -* var out = replaceBeforeLast( str, '', 'foo', str.length); +* var out = replaceBeforeLast( str, '', 'foo', str.length ); * // returns 'Hello World!' * * @example From 57df8703f2eeb65b4f595eb3c340c79e2fa05397 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 25 Feb 2024 15:40:22 -0800 Subject: [PATCH 09/12] Apply suggestions from code review Signed-off-by: Athan --- .../@stdlib/string/base/replace-before-last/test/test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js index fe7341681d2f..5801729a3297 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js @@ -32,10 +32,11 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function replaces the substring after the last occurrence of a specified search string', function test( t ) { +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'; @@ -54,7 +55,7 @@ tape( 'the function replaces the substring after the last occurrence of a specif t.end(); }); -tape( 'the function replaces the substring after the last occurrence of a specified search string (Unicode characters)', function test( t ) { +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; @@ -77,7 +78,7 @@ tape( 'the function replaces the substring after the last occurrence of a specif t.end(); }); -tape( 'the function replaces the substring after a provided search string (custom start index)', function test( t ) { +tape( 'the function replaces the substring before a provided search string (custom start index)', function test( t ) { var expected; var actual; var str; From 723fa6f3b5bb1193c64e0d35926e5ae9579229f5 Mon Sep 17 00:00:00 2001 From: AuenKr Date: Mon, 26 Feb 2024 22:17:50 +0530 Subject: [PATCH 10/12] fixup! feat: applied changes for fromIndex --- .../@stdlib/string/base/replace-before-last/README.md | 10 +++++++++- .../string/base/replace-before-last/lib/main.js | 2 +- .../string/base/replace-before-last/test/test.js | 6 +++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md index 06cfc02e612a..888586eefe1a 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/README.md +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/README.md @@ -53,6 +53,14 @@ 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' +``` + @@ -65,7 +73,7 @@ out = replaceBeforeLast( str, 'o', 'bar', str.length ); - 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 the `search` string length, the function returns the provided string unchanged. +- If `fromIndex` is less than `0`, the function returns the provided string unchanged. diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js index dc6ea9b9eee3..fd03f8fae536 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/main.js @@ -61,7 +61,7 @@ */ function replaceBeforeLast( str, search, replacement, fromIndex ) { var idx; - if ( fromIndex < 0 || fromIndex > str.length ) { + if ( fromIndex < 0 ) { return str; } idx = str.lastIndexOf( search, fromIndex ); diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js index 5801729a3297..d36115b6dec2 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/test/test.js @@ -84,8 +84,8 @@ tape( 'the function replaces the substring before a provided search string (cust var str; str = 'beep boop baz'; - actual = replaceBeforeLast( str, ' ', 'foo', 6 ); - expected = 'foo boop baz'; + actual = replaceBeforeLast( str, ' ', 'foo', 9 ); + expected = 'foo baz'; t.strictEqual( actual, expected, 'returns expected value' ); str = 'beep boop baz'; @@ -100,7 +100,7 @@ tape( 'the function replaces the substring before a provided search string (cust str = 'beep boop baz'; actual = replaceBeforeLast( str, 'beep', 'foo', 20 ); - expected = 'beep boop baz'; + expected = 'foobeep boop baz'; t.strictEqual( actual, expected, 'returns expected value' ); t.end(); From 3f4953b949b91064173a2d8fb7aa5adfb2a73998 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Tue, 27 Feb 2024 09:41:28 -0500 Subject: [PATCH 11/12] Update lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js Signed-off-by: Philipp Burckhardt --- .../@stdlib/string/base/replace-before-last/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js index 6f9b58742404..b29877c0c2d6 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/lib/index.js @@ -32,7 +32,7 @@ * // returns 'foo boop' * * out = replaceBeforeLast( str, 'o', 'bar', str.length ); -* // returns 'fooop' +* // returns 'barop' */ // MODULES // From 68827d95f467a480d22f9b73a5a9d91a42432a2f Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Tue, 27 Feb 2024 09:41:33 -0500 Subject: [PATCH 12/12] Update lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts Signed-off-by: Philipp Burckhardt --- .../string/base/replace-before-last/docs/types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts index 7dd66c1bca82..a89dbdaada44 100644 --- a/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/replace-before-last/docs/types/index.d.ts @@ -57,7 +57,7 @@ * var out = replaceBeforeLast( str, 'p b', 'foo', 6 ); * // returns 'foop boop baz' */ -declare function replaceBeforeLast( str: string, search: string, replacement: string, fromIndex: number ): string; +declare function replaceBeforeLast( str: string, search: string, replacement: string, fromIndex: number ): string; // EXPORTS //