Skip to content

Commit a54c302

Browse files
committed
Add split option support
1 parent 357c00e commit a54c302

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Options:
120120
-h, --help Print this message.
121121
-V, --version Print the package version.
122122
--search string Search string.
123+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
123124
```
124125

125126
</section>
@@ -130,6 +131,20 @@ Options:
130131

131132
<section class="notes">
132133

134+
### Notes
135+
136+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
137+
138+
```bash
139+
# Not escaped...
140+
$ echo -n $'foo\nbar\nbaz' | substring-before --search a --split /\r?\n/
141+
142+
# Escaped...
143+
$ echo -n $'foo\nbar\nbaz' | substring-before --search a --split /\\r?\\n/
144+
```
145+
146+
- The implementation ignores trailing delimiters.
147+
133148
</section>
134149

135150
<!-- /.notes -->
@@ -145,6 +160,22 @@ $ substring-before abcdefg --search d
145160
abc
146161
```
147162

163+
To use as a [standard stream][standard-streams],
164+
165+
```bash
166+
$ echo -n $'beep\nboop' | substring-before --search p
167+
bee
168+
boo
169+
```
170+
171+
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.
172+
173+
```bash
174+
$ echo -n 'beep\tboop' | substring-before --search p --split '\t'
175+
bee
176+
boo
177+
```
178+
148179
</section>
149180

150181
<!-- /.examples -->
@@ -181,6 +212,10 @@ abc
181212

182213
<section class="links">
183214

215+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
216+
217+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
218+
184219
<!-- <related-links> -->
185220

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

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

Lines changed: 17 additions & 1 deletion
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 substringBefore = require( './../lib' );
3234

3335

@@ -40,6 +42,7 @@ var substringBefore = 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
console.log( substringBefore( str, flags.search ) ); // eslint-disable-line no-console
@@ -89,7 +100,12 @@ function main() {
89100
if ( error ) {
90101
return cli.error( error );
91102
}
92-
lines = data.toString().split( RE_EOL );
103+
lines = data.toString().split( split );
104+
105+
// Remove any trailing separators (e.g., trailing newline)...
106+
if ( lines[ lines.length-1 ] === '' ) {
107+
lines.pop();
108+
}
93109
for ( i = 0; i < lines.length; i++ ) {
94110
console.log( substringBefore( lines[ i ], flags.search ) ); // eslint-disable-line no-console
95111
}

lib/node_modules/@stdlib/string/substring-before/docs/usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Options:
66
-h, --help Print this message.
77
-V, --version Print the package version.
88
--search string Search string.
9+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"string": [
3-
"search"
3+
"search",
4+
"split"
45
],
56
"boolean": [
67
"help",

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,52 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
186186
}
187187
});
188188

189+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) {
190+
var cmd = [
191+
'printf \'beep boop\tboop beep\'',
192+
'|',
193+
EXEC_PATH,
194+
fpath,
195+
'--search="e"',
196+
'--split \'\t\''
197+
];
198+
199+
exec( cmd.join( ' ' ), done );
200+
201+
function done( error, stdout, stderr ) {
202+
if ( error ) {
203+
t.fail( error.message );
204+
} else {
205+
t.strictEqual( stdout.toString(), 'b\nboop b\n', 'expected value' );
206+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
207+
}
208+
t.end();
209+
}
210+
});
211+
212+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) {
213+
var cmd = [
214+
'printf \'beep boop\tboop beep\'',
215+
'|',
216+
EXEC_PATH,
217+
fpath,
218+
'--search="e"',
219+
'--split=/\\\\t/'
220+
];
221+
222+
exec( cmd.join( ' ' ), done );
223+
224+
function done( error, stdout, stderr ) {
225+
if ( error ) {
226+
t.fail( error.message );
227+
} else {
228+
t.strictEqual( stdout.toString(), 'b\nboop b\n', 'expected value' );
229+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
230+
}
231+
t.end();
232+
}
233+
});
234+
189235
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 ) {
190236
var script;
191237
var opts;

0 commit comments

Comments
 (0)