Skip to content

Commit 95ba099

Browse files
committed
Add split option support
1 parent a54c302 commit 95ba099

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

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

+35
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Options:
121121
-h, --help Print this message.
122122
-V, --version Print the package version.
123123
--search string Search string.
124+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
124125
```
125126

126127
</section>
@@ -131,6 +132,20 @@ Options:
131132

132133
<section class="notes">
133134

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

136151
<!-- /.notes -->
@@ -146,6 +161,22 @@ $ substring-before-last abcdefg --search d
146161
abc
147162
```
148163

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

151182
<!-- /.examples -->
@@ -182,6 +213,10 @@ abc
182213

183214
<section class="links">
184215

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

187222
[@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-before-last/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 substringBeforeLast = require( './../lib' );
3234

3335

@@ -40,6 +42,7 @@ var substringBeforeLast = 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( substringBeforeLast( 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( substringBeforeLast( lines[ i ], flags.search ) ); // eslint-disable-line no-console
95111
}

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

+1
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-last/etc/cli_opts.json

+2-1
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-last/test/test.cli.js

+46
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(), 'be\nboop be\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(), 'be\nboop be\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)