Skip to content

Commit f646db7

Browse files
committed
Add split option support
1 parent 58e5d58 commit f646db7

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

lib/node_modules/@stdlib/assert/is-hex-string/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,34 @@ Options:
9797
9898
-h, --help Print this message.
9999
-V, --version Print the package version.
100+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
100101
```
101102

102103
</section>
103104

104105
<!-- /.usage -->
105106

107+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="notes">
110+
111+
### Notes
112+
113+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
114+
115+
```bash
116+
# Not escaped...
117+
$ echo -n $'0xffffff\n0123456789abcdefABCDEF | is-hex-string --split /\r?\n/
118+
# Escaped...
119+
$ echo -n $'0xffffff\n0123456789abcdefABCDEF | is-hex-string --split /\\r?\\n/
120+
```
121+
122+
- The implementation ignores trailing delimiters.
123+
124+
</section>
125+
126+
<!-- /.notes -->
127+
106128
<section class="examples">
107129

108130
### Examples
@@ -119,6 +141,14 @@ $ echo -n '0123456789abcdefABCDEF' | is-hex-string
119141
true
120142
```
121143

144+
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.
145+
146+
```bash
147+
$ echo -n '0123456789abcdefABCDEF\t0xffffff' | is-hex-string --split '\t'
148+
true
149+
false
150+
```
151+
122152
</section>
123153

124154
<!-- /.examples -->
@@ -147,6 +177,8 @@ true
147177

148178
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
149179

180+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
181+
150182
<!-- <related-links> -->
151183

152184
[@stdlib/assert/is-string]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-string

lib/node_modules/@stdlib/assert/is-hex-string/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 isHexString = require( './../lib' );
3234

3335

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

lib/node_modules/@stdlib/assert/is-hex-string/docs/usage.txt

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

lib/node_modules/@stdlib/assert/is-hex-string/etc/cli_opts.json

+3
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/node_modules/@stdlib/assert/is-hex-string/test/test.cli.js

+44
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 \'0123456789abcdefABCDEF\t0xffffff\'',
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(), 'true\nfalseE\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 \'0123456789abcdefABCDEF\t0xffffff\'',
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(), 'true\nfalseE\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)