Skip to content

feat: add support for replacing the substring before the first occurrence of a search string #843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a510f8e
readme.md for replace-before
HarshitaKalani Feb 4, 2023
3725623
readme.md added for replace-before
HarshitaKalani Feb 4, 2023
357a601
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into re…
HarshitaKalani Feb 4, 2023
42ef006
Added main.js for replace-before
HarshitaKalani Feb 4, 2023
864227e
updated a comment in main.js
HarshitaKalani Feb 4, 2023
7a75514
index.js added in replace-before
HarshitaKalani Feb 4, 2023
6049a76
benchmark.js added in replace-before
HarshitaKalani Feb 4, 2023
32e46a6
updated main.js in replace-before
HarshitaKalani Feb 4, 2023
ed76908
examples added in replace-before
HarshitaKalani Feb 4, 2023
4de1ada
test added in replace_before
HarshitaKalani Feb 4, 2023
aeca05e
package.json added to replace_before
HarshitaKalani Feb 4, 2023
2611bde
etc added to replace-before
HarshitaKalani Feb 4, 2023
f2bad45
docs added in replace_before
HarshitaKalani Feb 4, 2023
fbc12e2
added bin in replace_before
HarshitaKalani Feb 4, 2023
d9391fa
updated 2021 to 2023 in all files
HarshitaKalani Feb 5, 2023
58b88e9
indeantation added in readme.md
HarshitaKalani Feb 5, 2023
1b8fad0
made the required changes after review
HarshitaKalani Feb 12, 2023
fee7998
Update lib/node_modules/@stdlib/string/base/replace-before/README.md
HarshitaKalani Feb 12, 2023
6b45090
Update lib/node_modules/@stdlib/string/base/replace-before/README.md
HarshitaKalani Feb 12, 2023
1f1fe55
Update lib/node_modules/@stdlib/string/base/replace-before/README.md
HarshitaKalani Feb 12, 2023
a837811
Update lib/node_modules/@stdlib/string/base/replace-before/README.md
HarshitaKalani Feb 12, 2023
7485791
Update lib/node_modules/@stdlib/string/base/replace-before/README.md
HarshitaKalani Feb 12, 2023
e5a2d71
Update lib/node_modules/@stdlib/string/base/replace-before/README.md
HarshitaKalani Feb 12, 2023
9875414
Update lib/node_modules/@stdlib/string/base/replace-before/benchmark/…
HarshitaKalani Feb 12, 2023
e74289c
Update lib/node_modules/@stdlib/string/base/replace-before/docs/repl.txt
HarshitaKalani Feb 12, 2023
3d2838b
Update lib/node_modules/@stdlib/string/base/replace-before/docs/repl.txt
HarshitaKalani Feb 12, 2023
da64037
Update lib/node_modules/@stdlib/string/base/replace-before/docs/types…
HarshitaKalani Feb 12, 2023
927bb0e
Update lib/node_modules/@stdlib/string/base/replace-before/docs/types…
HarshitaKalani Feb 12, 2023
4de2f23
Update lib/node_modules/@stdlib/string/base/replace-before/docs/types…
HarshitaKalani Feb 12, 2023
5357956
updated the changes
HarshitaKalani Feb 13, 2023
3de2a67
resolve conflict
HarshitaKalani Feb 13, 2023
b9a5ed3
lint errors and cli test cases resolved
HarshitaKalani Feb 22, 2023
381c32c
updation in docs
HarshitaKalani Feb 24, 2023
0b06210
readme.md updated
HarshitaKalani Feb 24, 2023
85b24ea
package.json updates
HarshitaKalani Feb 24, 2023
321ef7b
repl.txt updates
HarshitaKalani Feb 24, 2023
2b47faa
readme.md updated
HarshitaKalani Feb 24, 2023
d85b38c
Revert changes
kgryte Feb 24, 2023
9dd3b9c
Apply suggestions from code review
kgryte Feb 24, 2023
e0b8c88
Apply suggestions from code review
kgryte Feb 24, 2023
138a52c
Fix relative path
kgryte Feb 24, 2023
df8a059
Fix require path
kgryte Feb 24, 2023
8fad29c
Apply suggestions from code review
kgryte Feb 24, 2023
9d5804e
Apply suggestions from code review
kgryte Feb 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 212 additions & 0 deletions lib/node_modules/@stdlib/string/base/replace-before/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<!--

@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.

-->

# replaceBefore

> Replace the substring before the first occurrence of a specified search string.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## 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'
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If a search string is not present in a provided string, the function returns the provided string unchanged.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 ''
```

</section>

<!-- /.examples -->

<!-- Section for describing a command-line interface. -->

* * *

<section class="cli">

## CLI

<!-- CLI usage documentation. -->

<section class="usage">

### Usage

```text
Usage: replace-before [options] --search=<string> --replacement=<string> [<string>]

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/'.
```

</section>

<!-- /.usage -->

<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

### 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.

</section>

<!-- /.notes -->

<!-- CLI usage examples. -->

<section class="examples">

### 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 --replacement see --split '\t'
seep
seep
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams

[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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 replacement;
var out;
var str;
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();
});
115 changes: 115 additions & 0 deletions lib/node_modules/@stdlib/string/base/replace-before/bin/cli
Original file line number Diff line number Diff line change
@@ -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();
Loading