Skip to content

Commit c1bc2de

Browse files
committed
Add split option support
1 parent 71ec67f commit c1bc2de

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

lib/node_modules/@stdlib/string/replace/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,35 @@ Options:
116116
-V, --version Print the package version.
117117
--search string Search string.
118118
--newval string Replacement string.
119+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
119120
```
120121

121122
</section>
122123

123124
<!-- /.usage -->
124125

126+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
127+
128+
<section class="notes">
129+
130+
### Notes
131+
132+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
133+
134+
```bash
135+
# Not escaped...
136+
$ echo -n $'foo\nbar' | replace --search='o' --newval='e' --split /\r?\n/
137+
138+
# Escaped...
139+
$ echo -n $'foo\nbar' | replace --search='o' --newval='e' --split /\\r?\\n/
140+
```
141+
142+
- The implementation ignores trailing delimiters.
143+
144+
</section>
145+
146+
<!-- /.notes -->
147+
125148
<section class="examples">
126149

127150
### Examples
@@ -134,10 +157,18 @@ baap
134157
To use as a [standard stream][standard-streams],
135158

136159
```bash
137-
$ echo -n 'boop' | replace --search='o' newval='e'
160+
$ echo -n 'boop' | replace --search='o' --newval='e'
138161
beep
139162
```
140163

164+
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.
165+
166+
```bash
167+
$ echo -n 'boop\tfoo' | replace --search='o' --newval='e' --split '\t'
168+
beep
169+
fee
170+
```
171+
141172
</section>
142173

143174
<!-- /.examples -->
@@ -162,6 +193,8 @@ beep
162193

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

196+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
197+
165198
</section>
166199

167200
<!-- /.links -->

lib/node_modules/@stdlib/string/replace/bin/cli

+15-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var replace = require( './../lib' );
4343
*/
4444
function main() {
4545
var search;
46+
var split;
4647
var flags;
4748
var args;
4849
var cli;
@@ -72,6 +73,14 @@ function main() {
7273

7374
// Check if we are receiving data from `stdin`...
7475
if ( !stdinStream.isTTY ) {
76+
if ( flags.split ) {
77+
if ( !isRegExpString( flags.split ) ) {
78+
flags.split = '/'+flags.split+'/';
79+
}
80+
split = reFromString( flags.split );
81+
} else {
82+
split = RE_EOL;
83+
}
7584
return stdin( onRead );
7685
}
7786
console.log( replace( args[ 0 ], search, flags.newval ) ); // eslint-disable-line no-console
@@ -90,7 +99,12 @@ function main() {
9099
if ( error ) {
91100
return cli.error( error );
92101
}
93-
lines = data.toString().split( RE_EOL );
102+
lines = data.toString().split( split );
103+
104+
// Remove any trailing separators (e.g., trailing newline)...
105+
if ( lines[ lines.length-1 ] === '' ) {
106+
lines.pop();
107+
}
94108
for ( i = 0; i < lines.length; i++ ) {
95109
console.log( replace( lines[ i ], search, flags.newval ) ); // eslint-disable-line no-console
96110
}

lib/node_modules/@stdlib/string/replace/docs/usage.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Options:
77
-V, --version Print the package version.
88
--search string Search string.
99
--newval string Replacement string.
10+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

lib/node_modules/@stdlib/string/replace/etc/cli_opts.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
],
66
"string": [
77
"search",
8-
"newval"
8+
"newval",
9+
"split"
910
],
1011
"alias": {
1112
"help": [

lib/node_modules/@stdlib/string/replace/test/test.cli.js

+48
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,54 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
209209
}
210210
});
211211

212+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) {
213+
var cmd = [
214+
'printf \'a11y\ti18n\ta16z\'',
215+
'|',
216+
EXEC_PATH,
217+
fpath,
218+
'--search=/[0-9]/g',
219+
'--newval=""',
220+
'--split \'\t\''
221+
];
222+
223+
exec( cmd.join( ' ' ), done );
224+
225+
function done( error, stdout, stderr ) {
226+
if ( error ) {
227+
t.fail( error.message );
228+
} else {
229+
t.strictEqual( stdout.toString(), 'ay\nin\naz\n', 'expected value' );
230+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
231+
}
232+
t.end();
233+
}
234+
});
235+
236+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) {
237+
var cmd = [
238+
'printf \'a11y\ti18n\ta16z\'',
239+
'|',
240+
EXEC_PATH,
241+
fpath,
242+
'--search=/[0-9]/g',
243+
'--newval=""',
244+
'--split=/\\\\t/'
245+
];
246+
247+
exec( cmd.join( ' ' ), done );
248+
249+
function done( error, stdout, stderr ) {
250+
if ( error ) {
251+
t.fail( error.message );
252+
} else {
253+
t.strictEqual( stdout.toString(), 'ay\nin\naz\n', 'expected value' );
254+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
255+
}
256+
t.end();
257+
}
258+
});
259+
212260
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 ) {
213261
var script;
214262
var opts;

0 commit comments

Comments
 (0)