diff --git a/lib/node_modules/@stdlib/string/base/package.json b/lib/node_modules/@stdlib/string/base/package.json index 8e752f5039d1..79cf5ea86268 100644 --- a/lib/node_modules/@stdlib/string/base/package.json +++ b/lib/node_modules/@stdlib/string/base/package.json @@ -56,6 +56,8 @@ "utility", "utils", "util", - "base" + "base", + "replace" ] } + diff --git a/lib/node_modules/@stdlib/string/base/replace-after/README.md b/lib/node_modules/@stdlib/string/base/replace-after/README.md new file mode 100644 index 000000000000..964431471486 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after/README.md @@ -0,0 +1,249 @@ + + +# replaceAfter + +> Replace the substring after the first occurrence of a specified search string. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var replaceAfter = require( '@stdlib/string/base/replace-after' ); +``` + +#### replaceAfter( str, replacement, search\[, fromIndex] ) + +Replaces the substring after the first occurrence of a specified search string. + +```javascript +var str = 'beep boop'; +var replacement = 'foo'; +var out = replaceAfter( str, replacement, 'o' ); +// returns 'beep bofoo' + +out = replaceAfter( str, replacement, ' ' ); +// returns 'beep foo' +``` + +By default, the search starts at the beginning of the string. To start searching from a different index, provide a `fromIndex` argument: + +```javascript +var str = 'boop baz boop'; +var replacement = 'foo'; +var out = replaceAfter( str, replacement, 'o', 3 ); +// returns 'boop baz bofoo' +``` + +
+ + + + + +
+ +## Notes + +- If a substring is not present in a provided string, the function returns an empty string. +- If provided an empty substring, the function returns the input string. +- If `fromIndex` is less than `0` or greater than `str.length`, the search starts at index `0` and `str.length`, respectively. + +
+ + + + + +
+ +## Examples + + + +```javascript +var replaceAfter = require( '@stdlib/string/base/replace-after' ); + +var str = 'To be, or not to be, that is the question.'; +var replacement = 'foo'; +var out = replaceAfter( str, replacement, ', ' ); +// returns 'To be, foo' + +out = replaceAfter( str, replacement, 'to be' ); +// returns 'To be, or not to befoo' + +out = replaceAfter( str, replacement, 'question.' ); +// returns 'To be, or not to be, that is the question.foo' + +out = replaceAfter( str, replacement, 'xyz' ); +// returns '' + +out = replaceAfter( str, replacement, '' ); +// returns 'foo' +``` + +
+ + + + + +* * * + +
+ +## CLI + + + +
+ +### Usage + +```text +Usage: replace-after [options] --search= --replacement= [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --search string Search string. + --replacement string Replacement string. + --from-index int Start index. Default: 0. + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. +``` + +
+ + + + + +
+ +### Notes + +- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. + + ```bash + # Not escaped... + $ echo -n $'foo\nbar\nbaz' | replace-after --search a --replacement b --split /\r?\n/ + + # Escaped... + $ echo -n $'foo\nbar\nbaz' | replace-after --search a --replacement b --split /\\r?\\n/ + ``` + +- The implementation ignores trailing delimiters. + +
+ + + + + +
+ +### Examples + +```bash +$ replace-after abcdefg --search d --replacement pqr +abcdpqr +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n $'bar\nbaz' | replace-after --search b --replacement pqr +bpqr +bpqr +``` + +By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. + +```bash +$ echo -n 'bar\tbaz' | replace-after --search b --replacement pqr --split '\t' +bpqr +bpqr +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/replace-after/package.json b/lib/node_modules/@stdlib/string/base/replace-after/package.json new file mode 100644 index 000000000000..bb52f455d8ab --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-after/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/string/base/replace-after", + "version": "0.0.0", + "description": "Replace the substring after the first 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" + } + ], + "bin": { + "substring-after": "./bin/cli" + }, + "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", + "string", + "str", + "check", + "search", + "replacement", + "substring", + "substr", + "after", + "match" + ] + } + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/string/base/replace-before/README.md b/lib/node_modules/@stdlib/string/base/replace-before/README.md new file mode 100644 index 000000000000..4a17ba216a54 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/README.md @@ -0,0 +1,212 @@ + + +# replaceBefore + +> Replace the substring before the first occurrence of a specified search string. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var replaceBefore = require( '@stdlib/string/base/replace-before' ); +``` + +#### replaceBefore( str, search, replacement ) + +Replaces the substring before the first occurrence of a specified search string. + +```javascript +var out = replaceBefore( 'beep boop', ' ', 'loop' ); +// returns 'loop boop' + +out = replaceBefore( 'beep boop', 'o', 'bar' ); +// returns 'baroop' +``` + +
+ + + + + +
+ +## Notes + +- If a search string is not present in a provided string, the function returns the provided string unchanged. + +
+ + + + + +
+ +## Examples + + + +```javascript +var replaceBefore = require( '@stdlib/string/base/replace-before' ); + +var out = replaceBefore( 'beep boop', 'p', 'see' ); +// returns 'seep boop' + +out = replaceBefore( 'Hello World!', 'xyz', 'foo' ); +// returns 'Hello World!' + +out = replaceBefore( 'Hello World!', '', 'foo' ); +// returns 'Hello World!' + +out = replaceBefore( '', 'xyz', 'foo'); +// returns '' +``` + +
+ + + + + +* * * + +
+ +## CLI + + + +
+ +### Usage + +```text +Usage: replace-before [options] --search= --replacement= [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --search string Search string. + --replacement string Replacement string. + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. +``` + +
+ + + + + +
+ +### Notes + +- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. + + ```bash + # Not escaped... + $ echo -n $'foo\nbar\nbaz' | replace-before --search a --replacement b --split /\r?\n/ + + # Escaped... + $ echo -n $'foo\nbar\nbaz' | replace-before --search a --replacement b --split /\\r?\\n/ + ``` + +- The implementation ignores trailing delimiters. + +
+ + + + + +
+ +### Examples + +```bash +$ replace-before abcdefg --search d --replacement pqr +pqrdefg +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n $'beep\nboop' | replace-before --search p --replacement see +seep +seep +``` + +By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. + +```bash +$ echo -n 'beep\tboop' | replace-before --search p --see --split '\t' +seep +seep +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/replace-before/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/replace-before/benchmark/benchmark.js new file mode 100644 index 000000000000..8ef80e149efe --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/benchmark/benchmark.js @@ -0,0 +1,54 @@ +/** +* @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 fromCodePoint = require( '@stdlib/string/from-code-point' ); +var pkg = require( './../package.json' ).name; +var replaceBefore = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var out; + var str; + var replacement; + var i; + + str = 'To be, or not to be, that is the question.'; + replacement = 'foo'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = replaceBefore( str, fromCodePoint( i%126 ), replacement ); + 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(); +}); \ No newline at end of file diff --git a/lib/node_modules/@stdlib/string/base/replace-before/bin/cli b/lib/node_modules/@stdlib/string/base/replace-before/bin/cli new file mode 100644 index 000000000000..f877ab781457 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/bin/cli @@ -0,0 +1,115 @@ +#!/usr/bin/env node + +/** +* @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 resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/cli/ctor' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP; +var isRegExpString = require( '@stdlib/assert/is-regexp-string' ); +var reFromString = require( '@stdlib/utils/regexp-from-string' ); +var replaceBefore = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var split; + var flags; + var args; + var cli; + var str; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + if ( args.length ) { + str = args[ 0 ]; + } else { + // Treat an empty value as an empty string: + str = ''; + } + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + if ( flags.split ) { + if ( !isRegExpString( flags.split ) ) { + flags.split = '/'+flags.split+'/'; + } + split = reFromString( flags.split ); + } else { + split = RE_EOL; + } + return stdin( onRead ); + } + console.log( replaceBefore( str, flags.search, flags.replacement) ); + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( split ); + + // Remove any trailing separators (e.g., trailing newline)... + if ( lines[ lines.length-1 ] === '' ) { + lines.pop(); + } + for ( i = 0; i < lines.length; i++ ) { + console.log(replaceBefore( lines[ i ], flags.search, flags.replacement ) ); // eslint-disable-line max-len + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/string/base/replace-before/docs/repl.txt b/lib/node_modules/@stdlib/string/base/replace-before/docs/repl.txt new file mode 100644 index 000000000000..e9ce0d5ffc33 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( str, search, replacement ) + Replaces the substring before the first + 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 replacement = 'foo'; + > var out = {{alias}}( str, ' ', replacement ) + 'foo boop' + > out = {{alias}}( str, 'o', replacement ) + 'foooop' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/replace-before/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/replace-before/docs/types/index.d.ts new file mode 100644 index 000000000000..4c681b872e7c --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @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: 2.0 + +/// + +/** +* Replaces the substring before the first occurrence of a specified search string. +* +* @param str - input string +* @param search - search string +* @param replacement - replacement string +* @returns substring +* +* @example +* var out = replaceBefore( 'beep boop', ' ', 'foo' ); +* // returns 'foo boop' +* +* @example +* var out = replaceBefore( 'beep boop', 'p', 'foo' ); +* // returns 'foop boop' +* +* @example +* var out = replaceBefore( 'Hello World!', '', 'foo' ); +* // returns 'Hello world!' +* +* @example +* var out = replaceBefore( 'Hello World!', 'XYZ', 'foo' ); +* // returns 'Hello World!' +*/ +declare function replaceBefore( str: string, search: string, replacement: string ): string; // tslint:disable-line:max-line-length + + +// EXPORTS // + +export = replaceBefore; diff --git a/lib/node_modules/@stdlib/string/base/replace-before/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/replace-before/docs/types/test.ts new file mode 100644 index 000000000000..f935b62d7b0f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/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 replaceBefore = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + replaceBefore( 'beep boop', ' ', 'foo' ); // $ExpectType string + replaceBefore( 'beep boop', 'xyz', 'foo' ); // $ExpectType string + replaceBefore( 'beep boop', '', 'foo' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided arguments having invalid types... +{ + replaceBefore( true, 'd', 'foo' ); // $ExpectError + replaceBefore( false, 'd' , 'foo' ); // $ExpectError + replaceBefore( 3, 'd' , 'foo' ); // $ExpectError + replaceBefore( [], 'd' , 'foo' ); // $ExpectError + replaceBefore( {}, 'd' , 'foo' ); // $ExpectError + replaceBefore( ( x: number ): number => x, 'd', 'foo' ); // $ExpectError + + replaceBefore( 'abc', true, 'foo' ); // $ExpectError + replaceBefore( 'abc', false, 'foo' ); // $ExpectError + replaceBefore( 'abc', 5 , 'foo' ); // $ExpectError + replaceBefore( 'abc', [], 'foo' ); // $ExpectError + replaceBefore( 'abc', {} , 'foo' ); // $ExpectError + replaceBefore( 'abc', ( x: number ): number => x , 'foo' ); // $ExpectError + + replaceBefore( 'abc', 'd', true ); // $ExpectError + replaceBefore( 'abc', 'd', false ); // $ExpectError + replaceBefore( 'abc', 'd', 5 ); // $ExpectError + replaceBefore( 'abc', 'd', [] ); // $ExpectError + replaceBefore( 'abc', 'd', {} ); // $ExpectError + replaceBefore( 'abc', 'd', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + replaceBefore(); // $ExpectError + replaceBefore( 'abc' ); // $ExpectError + replaceBefore( 'abc', 'd' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/replace-before/docs/usage.txt b/lib/node_modules/@stdlib/string/base/replace-before/docs/usage.txt new file mode 100644 index 000000000000..ae56e68867a5 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/docs/usage.txt @@ -0,0 +1,10 @@ + +Usage: replace-before [options] --search= [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --search string Search string. + --replacement string Replacement string. + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. diff --git a/lib/node_modules/@stdlib/string/base/replace-before/etc/cli_opts.json b/lib/node_modules/@stdlib/string/base/replace-before/etc/cli_opts.json new file mode 100644 index 000000000000..ef139893a8e3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/etc/cli_opts.json @@ -0,0 +1,19 @@ +{ + "string": [ + "search", + "replacement", + "split" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/lib/node_modules/@stdlib/string/base/replace-before/examples/index.js b/lib/node_modules/@stdlib/string/base/replace-before/examples/index.js new file mode 100644 index 000000000000..97caf301171b --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/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 replaceBefore = require( './../lib' ); + +var out = replaceBefore( 'beep boop', 'p', 'foo' ); +console.log( out ); +// => 'foop boop' + +out = replaceBefore( 'Hello World!', 'xyz', 'foo' ); +console.log( out ); +// => 'Hello World!' + +out = replaceBefore( 'Hello World!', '', 'foo' ); +console.log( out ); +// => 'Hello World!' + +out = replaceBefore( '', 'xyz', 'foo' ); +console.log( out ); +// => '' diff --git a/lib/node_modules/@stdlib/string/base/replace-before/lib/index.js b/lib/node_modules/@stdlib/string/base/replace-before/lib/index.js new file mode 100644 index 000000000000..274f896de0d4 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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 first occurrence of a specified search string. +* +* @module @stdlib/string/base/replace-before +* +* @example +* var replaceBefore = require( '@stdlib/string/substring-before' ); +* +* var str = 'beep boop'; +* var replacement = 'foo'; +* var out = replaceBefore( str, ' ', replacement ); +* // returns 'beep boop' +* +* replacement = 'bar'; +* out = replaceBefore( str, 'o', replacement ); +* // returns 'baroop' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/replace-before/lib/main.js b/lib/node_modules/@stdlib/string/base/replace-before/lib/main.js new file mode 100644 index 000000000000..57ee7d35966e --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/lib/main.js @@ -0,0 +1,78 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Replaces the substring before the first occurrence of a specified search string. +* +* @param {string} str - input string +* @param {string} search - search string +* @param {string} replacement - replacement string +* @throws {TypeError} first argument must be a string +* @throws {TypeError} second argument must be a string +* @throws {TypeError} third argument must be a string +* @returns {string} string +* +* @example +* var out = replaceBefore( 'beep boop', ' ', 'foo' ); +* // returns 'beep boop' +* +* @example +* var out = replaceBefore( 'beep boop', 'p', 'foo' ); +* // returns 'foop boop' +* +* @example +* var out = replaceBefore( 'Hello World!', '', 'foo' ); +* // returns 'Hello World!' +* +* @example +* var out = replaceBefore( 'Hello World!', 'XYZ', 'foo' ); +* // returns 'Hello World!' +*/ +function replaceBefore( str, search, replacement ) { + var idx; + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); + } + if ( !isString( search ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', search ) ); + } + if ( !isString( replacement ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', replacement ) ); + } + + idx = str.indexOf( search ); + + if ( str === '' || search === '' || replacement === '' || idx < 0 ) { + return str; + } + return replacement + str.substring( idx ); +} + +// EXPORTS // + +module.exports = replaceBefore; diff --git a/lib/node_modules/@stdlib/string/base/replace-before/package.json b/lib/node_modules/@stdlib/string/base/replace-before/package.json new file mode 100644 index 000000000000..1c708ff06ec0 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/string/base/replace-before", + "version": "0.0.96", + "description": "Replace the substring before the first 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" + } + ], + "bin": { + "replace-before": "bin/cli" + }, + "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", + "string", + "str", + "replace", + "search", + "substring", + "substr", + "before", + "match" + ] + } diff --git a/lib/node_modules/@stdlib/string/base/replace-before/test/fixtures/stdin_error.js.txt b/lib/node_modules/@stdlib/string/base/replace-before/test/fixtures/stdin_error.js.txt new file mode 100644 index 000000000000..3fc7f8c12cea --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @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. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} \ No newline at end of file diff --git a/lib/node_modules/@stdlib/string/base/replace-before/test/test.cli.js b/lib/node_modules/@stdlib/string/base/replace-before/test/test.cli.js new file mode 100644 index 000000000000..81a4f77bf07a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/test/test.cli.js @@ -0,0 +1,271 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var EXEC_PATH = require( '@stdlib/process/exec-path' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface replaces the substring before the first occurrence of a specified search string', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--search=p\'; process.argv[ 3 ] = \'boopbeep\'; process.argv[4] = \'--replacement=foo\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + var str; + if ( error ) { + t.fail( error.message ); + } else { + str = stdout.toString(); + t.strictEqual( str === 'foopbeep\n', true, 'prints `beep` to `stdout`' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep boop\nboop beep"', + '|', + EXEC_PATH, + fpath, + '--search="e"', + '--replacement="foo"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'fooeep boop\nfooeep\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) { + var cmd = [ + 'printf \'beep boop\tboop beep\'', + '|', + EXEC_PATH, + fpath, + '--search="e"', + '--replacement="foo"', + '--split \'\t\'' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'fooeep boop\nfooeep\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) { + var cmd = [ + 'printf \'beep boop\tboop beep\'', + '|', + EXEC_PATH, + fpath, + '--search="e"', + '--replacement="foo"', + '--split=/\\\\t/' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'fooeep boop\nfooeep\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/string/base/replace-before/test/test.js b/lib/node_modules/@stdlib/string/base/replace-before/test/test.js new file mode 100644 index 000000000000..d61e8d9e6ae9 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/replace-before/test/test.js @@ -0,0 +1,202 @@ +/** +* @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 replaceBefore = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof replaceBefore, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a string as its first argument', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + replaceBefore( value, 'beep', 'foo' ); + }; + } +}); + +tape( 'the function throws an error if not provided a string as its second argument', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + replaceBefore( 'beep boop', value, 'foo' ); + }; + } +}); + +tape( 'the function throws an error if not provided a string as its third argument', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + replaceBefore( 'beep boop', 'beep', value ); + }; + } +}); + +tape( 'the function replaces the substring before the first occurrence of a specified search string', function test( t ) { + var expected; + var actual; + var str; + var replacement; + + str = 'beep boop'; + replacement = 'foo'; + actual = replaceBefore( str, ' ' , replacement ); + expected = 'foo boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + replacement = 'foo'; + actual = replaceBefore( str, 'p', replacement ); + expected = 'foop boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'Hello, World!'; + replacement = 'foo'; + actual = replaceBefore( str, 'o', replacement ); + expected = 'fooo, World!'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function replaces the substring before the first occurrence of a specified search string (Unicode characters)', function test( t ) { + var expected; + var actual; + var str; + var replacement; + + str = 'beep 😀 boop 😀 baz'; + replacement = 'foo'; + actual = replaceBefore( str, '😀', replacement ); + expected = 'foo😀 boop 😀 baz'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🤖 Robot army 🤖!'; + replacement = 'foo'; + actual = replaceBefore( str, '🤖', replacement ); + expected = 'foo🤖 Robot army 🤖!'; + + str = '🐺 Wolf brothers 🐺'; + replacement = 'foo'; + actual = replaceBefore( str, 'o', replacement ); + expected = 'fooolf brothers 🐺'; + + 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; + var replacement; + + str = 'beep boop'; + replacement = 'foo'; + actual = replaceBefore( str, 'z', replacement ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + replacement = 'foo'; + actual = replaceBefore( str, 'baz', replacement ); + 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; + var replacement; + + str = 'beep boop'; + replacement = 'foo'; + actual = replaceBefore( str, '', replacement ); + expected = 'beep boop'; + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/package.json b/package.json index 620394bb4e87..93c3d499b9b1 100644 --- a/package.json +++ b/package.json @@ -3,32 +3,26 @@ "version": "0.0.96", "description": "Standard library.", "license": "Apache-2.0 AND BSL-1.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, + "author": "The Stdlib Authors (https://github.com/stdlib-js/stdlib/graphs/contributors)", "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } + "The Stdlib Authors (https://github.com/stdlib-js/stdlib/graphs/contributors)" ], "funding": { "type": "opencollective", "url": "https://opencollective.com/stdlib" }, "bin": { - "stdlib": "./bin/cli" + "stdlib": "bin/cli" }, "main": "./lib", "browser": { "process": "process/" }, "directories": { - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" + "doc": "docs", + "example": "examples", + "lib": "lib", + "test": "test" }, "types": "./docs/types", "scripts": { @@ -123,7 +117,7 @@ "eslint": "^8.0.1", "eslint-plugin-node": "^11.1.0", "factor-bundle": "^2.5.0", - "gh-pages": "git+https://github.com/Planeshifter/gh-pages#main", + "gh-pages": "git+https://github.com/Planeshifter/gh-pages.git#main", "jsdoc": "^3.4.0", "lunr": "^2.3.9", "mathjax-node": "^2.0.1",