Skip to content

Commit 50b3560

Browse files
committed
Add split option support
1 parent dccc8fc commit 50b3560

File tree

5 files changed

+103
-2
lines changed

5 files changed

+103
-2
lines changed

lib/node_modules/@stdlib/string/substring-after/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Options:
134134
-V, --version Print the package version.
135135
--search string Search string.
136136
--from-index int Start index. Default: 0.
137+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
137138
```
138139

139140
</section>
@@ -144,6 +145,20 @@ Options:
144145

145146
<section class="notes">
146147

148+
### Notes
149+
150+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
151+
152+
```bash
153+
# Not escaped...
154+
$ echo -n $'foo\nbar\nbaz' | substring-after --search a --split /\r?\n/
155+
156+
# Escaped...
157+
$ echo -n $'foo\nbar\nbaz' | substring-after --search a --split /\\r?\\n/
158+
```
159+
160+
- The implementation ignores trailing delimiters.
161+
147162
</section>
148163

149164
<!-- /.notes -->
@@ -159,6 +174,22 @@ $ substring-after abcdefg --search d
159174
efg
160175
```
161176

177+
To use as a [standard stream][standard-streams],
178+
179+
```bash
180+
$ echo -n $'bar\nbaz' | substring-after --search b
181+
ar
182+
az
183+
```
184+
185+
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.
186+
187+
```bash
188+
$ echo -n 'bar\tbaz' | substring-after --search b --split '\t'
189+
ar
190+
az
191+
```
192+
162193
</section>
163194

164195
<!-- /.examples -->
@@ -195,6 +226,10 @@ efg
195226

196227
<section class="links">
197228

229+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
230+
231+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
232+
198233
<!-- <related-links> -->
199234

200235
[@stdlib/string/substring-before]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/substring-before

lib/node_modules/@stdlib/string/substring-after/bin/cli

+17-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var CLI = require( '@stdlib/cli/ctor' );
2828
var stdin = require( '@stdlib/process/read-stdin' );
2929
var stdinStream = require( '@stdlib/streams/node/stdin' );
3030
var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
31+
var isRegExpString = require( '@stdlib/assert/is-regexp-string' );
32+
var reFromString = require( '@stdlib/utils/regexp-from-string' );
3133
var substringAfter = require( './../lib' );
3234

3335

@@ -40,6 +42,7 @@ var substringAfter = require( './../lib' );
4042
* @returns {void}
4143
*/
4244
function main() {
45+
var split;
4346
var flags;
4447
var args;
4548
var cli;
@@ -71,6 +74,14 @@ function main() {
7174
}
7275
// Check if we are receiving data from `stdin`...
7376
if ( !stdinStream.isTTY ) {
77+
if ( flags.split ) {
78+
if ( !isRegExpString( flags.split ) ) {
79+
flags.split = '/'+flags.split+'/';
80+
}
81+
split = reFromString( flags.split );
82+
} else {
83+
split = RE_EOL;
84+
}
7485
return stdin( onRead );
7586
}
7687
if ( flags[ 'from-index' ] ) {
@@ -94,7 +105,12 @@ function main() {
94105
if ( error ) {
95106
return cli.error( error );
96107
}
97-
lines = data.toString().split( RE_EOL );
108+
lines = data.toString().split( split );
109+
110+
// Remove any trailing separators (e.g., trailing newline)...
111+
if ( lines[ lines.length-1 ] === '' ) {
112+
lines.pop();
113+
}
98114
if ( flags[ 'from-index' ] ) {
99115
fromIndex = parseInt( flags[ 'from-index' ], 10 );
100116
for ( i = 0; i < lines.length; i++ ) {

lib/node_modules/@stdlib/string/substring-after/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
--from-index int Start index. Default: 0.
10+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

lib/node_modules/@stdlib/string/substring-after/etc/cli_opts.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"string": [
33
"search",
4-
"from-index"
4+
"from-index",
5+
"split"
56
],
67
"boolean": [
78
"help",

lib/node_modules/@stdlib/string/substring-after/test/test.cli.js

+48
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,54 @@ tape( 'the command-line interface supports use as a standard stream (custom star
231231
}
232232
});
233233

234+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) {
235+
var cmd = [
236+
'printf \'beep boop foo\tboop beep bar\'',
237+
'|',
238+
EXEC_PATH,
239+
fpath,
240+
'--search=" "',
241+
'--from-index=6',
242+
'--split \'\t\''
243+
];
244+
245+
exec( cmd.join( ' ' ), done );
246+
247+
function done( error, stdout, stderr ) {
248+
if ( error ) {
249+
t.fail( error.message );
250+
} else {
251+
t.strictEqual( stdout.toString(), 'foo\nbar\n', 'expected value' );
252+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
253+
}
254+
t.end();
255+
}
256+
});
257+
258+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) {
259+
var cmd = [
260+
'printf \'beep boop foo\tboop beep bar\'',
261+
'|',
262+
EXEC_PATH,
263+
fpath,
264+
'--search=" "',
265+
'--from-index=6',
266+
'--split=/\\\\t/'
267+
];
268+
269+
exec( cmd.join( ' ' ), done );
270+
271+
function done( error, stdout, stderr ) {
272+
if ( error ) {
273+
t.fail( error.message );
274+
} else {
275+
t.strictEqual( stdout.toString(), 'foo\nbar\n', 'expected value' );
276+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
277+
}
278+
t.end();
279+
}
280+
});
281+
234282
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 ) {
235283
var script;
236284
var opts;

0 commit comments

Comments
 (0)