Skip to content

Commit ac09c0e

Browse files
committed
Auto-generated commit
1 parent 8f87c97 commit ac09c0e

File tree

7 files changed

+111
-5
lines changed

7 files changed

+111
-5
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Justin Dennison <[email protected]>
1616
1717
Matt Cochrane <[email protected]>
1818
Milan Raj <[email protected]>
19+
Momtchil Momtchev <[email protected]>
1920
Ognjen Jevremović <[email protected]>
2021
Philipp Burckhardt <[email protected]>
2122
Ricky Reusser <[email protected]>

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# substringBeforeLast
2222

23-
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] [![dependencies][dependencies-image]][dependencies-url]
23+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
2424

2525
> Return the part of a string before the last occurrence of a specified substring.
2626
@@ -143,6 +143,7 @@ Options:
143143
-h, --help Print this message.
144144
-V, --version Print the package version.
145145
--search string Search string.
146+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
146147
```
147148

148149
</section>
@@ -153,6 +154,20 @@ Options:
153154

154155
<section class="notes">
155156

157+
### Notes
158+
159+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
160+
161+
```bash
162+
# Not escaped...
163+
$ echo -n $'foo\nbar\nbaz' | substring-before-last --search a --split /\r?\n/
164+
165+
# Escaped...
166+
$ echo -n $'foo\nbar\nbaz' | substring-before-last --search a --split /\\r?\\n/
167+
```
168+
169+
- The implementation ignores trailing delimiters.
170+
156171
</section>
157172

158173
<!-- /.notes -->
@@ -168,6 +183,22 @@ $ substring-before-last abcdefg --search d
168183
abc
169184
```
170185

186+
To use as a [standard stream][standard-streams],
187+
188+
```bash
189+
$ echo -n $'beep\nboop' | substring-before-last --search p
190+
bee
191+
boo
192+
```
193+
194+
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.
195+
196+
```bash
197+
$ echo -n 'beep\tboop' | substring-before-last --search p --split '\t'
198+
bee
199+
boo
200+
```
201+
171202
</section>
172203

173204
<!-- /.examples -->
@@ -245,9 +276,13 @@ Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
245276
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/string-substring-before-last/main.svg
246277
[coverage-url]: https://codecov.io/github/stdlib-js/string-substring-before-last?branch=main
247278

279+
<!--
280+
248281
[dependencies-image]: https://img.shields.io/david/stdlib-js/string-substring-before-last.svg
249282
[dependencies-url]: https://david-dm.org/stdlib-js/string-substring-before-last/main
250283

284+
-->
285+
251286
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
252287
[chat-url]: https://gitter.im/stdlib-js/stdlib/
253288

@@ -257,6 +292,10 @@ Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
257292

258293
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/string-substring-before-last/main/LICENSE
259294

295+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
296+
297+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
298+
260299
<!-- <related-links> -->
261300

262301
[@stdlib/string/substring-before]: https://github.com/stdlib-js/string-substring-before

bin/cli

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ var readFileSync = require( '@stdlib/fs-read-file' ).sync;
2727
var CLI = require( '@stdlib/cli-ctor' );
2828
var stdin = require( '@stdlib/process-read-stdin' );
2929
var stdinStream = require( '@stdlib/streams-node-stdin' );
30-
var reEOL = require( '@stdlib/regexp-eol' );
30+
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( reEOL.REGEXP );
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
}

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

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",

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
"url": "https://github.com/stdlib-js/stdlib/issues"
4141
},
4242
"dependencies": {
43+
"@stdlib/assert-is-regexp-string": "^0.0.x",
4344
"@stdlib/assert-is-string": "^0.0.x",
4445
"@stdlib/cli-ctor": "^0.0.x",
4546
"@stdlib/fs-read-file": "^0.0.x",
4647
"@stdlib/process-read-stdin": "^0.0.x",
4748
"@stdlib/regexp-eol": "^0.0.x",
4849
"@stdlib/streams-node-stdin": "^0.0.x",
49-
"@stdlib/types": "^0.0.x"
50+
"@stdlib/types": "^0.0.x",
51+
"@stdlib/utils-regexp-from-string": "^0.0.x"
5052
},
5153
"devDependencies": {
5254
"@stdlib/assert-is-browser": "^0.0.x",

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(), '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)