Skip to content

Commit bef09ec

Browse files
committed
Auto-generated commit
1 parent ef9f8e4 commit bef09ec

File tree

8 files changed

+102
-6
lines changed

8 files changed

+102
-6
lines changed

.github/.keepalive

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,34 @@ Options:
132132
133133
-h, --help Print this message.
134134
-V, --version Print the package version.
135+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
135136
```
136137

137138
</section>
138139

139140
<!-- /.usage -->
140141

142+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
143+
144+
<section class="notes">
145+
146+
### Notes
147+
148+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
149+
150+
```bash
151+
# Not escaped...
152+
$ echo -n $'beEp booP\n100001110' | is-binary-string --split /\r?\n/
153+
# Escaped...
154+
$ echo -n $'beEp booP\n100001110' | is-binary-string --split /\\r?\\n/
155+
```
156+
157+
- The implementation ignores trailing delimiters.
158+
159+
</section>
160+
161+
<!-- /.notes -->
162+
141163
<section class="examples">
142164

143165
### Examples
@@ -154,6 +176,14 @@ $ echo -n '0110' | is-binary-string
154176
true
155177
```
156178

179+
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.
180+
181+
```bash
182+
$ echo -n '0110\t1234' | is-binary-string --split '\t'
183+
true
184+
false
185+
```
186+
157187
</section>
158188

159189
<!-- /.examples -->
@@ -247,6 +277,8 @@ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
247277

248278
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
249279

280+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
281+
250282
<!-- <related-links> -->
251283

252284
[@stdlib/assert/is-string]: https://github.com/stdlib-js/assert-is-string

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 isBinaryString = require( './../lib' );
3234

3335

@@ -40,6 +42,7 @@ var isBinaryString = require( './../lib' );
4042
* @returns {void}
4143
*/
4244
function main() {
45+
var split;
4346
var flags;
4447
var args;
4548
var cli;
@@ -64,6 +67,14 @@ function main() {
6467

6568
// Check if we are receiving data from `stdin`...
6669
if ( !stdinStream.isTTY ) {
70+
if ( flags.split ) {
71+
if ( !isRegExpString( flags.split ) ) {
72+
flags.split = '/'+flags.split+'/';
73+
}
74+
split = reFromString( flags.split );
75+
} else {
76+
split = RE_EOL;
77+
}
6778
return stdin( onRead );
6879
}
6980
console.log( isBinaryString( String( args[ 0 ] ) ) ); // eslint-disable-line no-console
@@ -82,7 +93,12 @@ function main() {
8293
if ( error ) {
8394
return cli.error( error );
8495
}
85-
lines = data.toString().split( RE_EOL );
96+
lines = data.toString().split( split );
97+
98+
// Remove any trailing separators (e.g., trailing newline)...
99+
if ( lines[ lines.length-1 ] === '' ) {
100+
lines.pop();
101+
}
86102
for ( i = 0; i < lines.length; i++ ) {
87103
console.log( isBinaryString( lines[ i ] ) ); // eslint-disable-line no-console
88104
}

docs/usage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Options:
55

66
-h, --help Print this message.
77
-V, --version Print the package version.
8-
8+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

etc/cli_opts.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"string": [
3+
"split"
4+
],
25
"boolean": [
36
"help",
47
"version"

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838

3939
// MODULES //
4040

41-
var isBinaryString = require( './main.js' );
41+
var main = require( './main.js' );
4242

4343

4444
// EXPORTS //
4545

46-
module.exports = isBinaryString;
46+
module.exports = main;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
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",
48-
"@stdlib/streams-node-stdin": "^0.0.x"
49+
"@stdlib/streams-node-stdin": "^0.0.x",
50+
"@stdlib/utils-regexp-from-string": "^0.0.x"
4951
},
5052
"devDependencies": {
5153
"@stdlib/assert-is-boolean": "^0.0.x",

test/test.cli.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,50 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
183183
}
184184
});
185185

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

0 commit comments

Comments
 (0)