Skip to content

Commit 379baf8

Browse files
committed
Add split option support
1 parent 83d85f8 commit 379baf8

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

lib/node_modules/@stdlib/assert/is-uri/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,34 @@ Options:
180180
181181
-h, --help Print this message.
182182
-V, --version Print the package version.
183+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
183184
```
184185

185186
</section>
186187

187188
<!-- /.usage -->
188189

190+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="notes">
193+
194+
### Notes
195+
196+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
197+
198+
```bash
199+
# Not escaped...
200+
$ echo -n $'beEp booP\nhttp://google.com' | is-uri --split /\r?\n/
201+
# Escaped...
202+
$ echo -n $'beEp booP\nhttp://google.com' | is-uri --split /\\r?\\n/
203+
```
204+
205+
- The implementation ignores trailing delimiters.
206+
207+
</section>
208+
209+
<!-- /.notes -->
210+
189211
<section class="examples">
190212

191213
### Examples
@@ -202,6 +224,14 @@ $ echo -n 'http://google.com' | is-uri
202224
true
203225
```
204226

227+
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.
228+
229+
```bash
230+
$ echo -n 'beep\thttps://google.com' | is-uri --split '\t'
231+
false
232+
true
233+
```
234+
205235
</section>
206236

207237
<!-- /.examples -->
@@ -230,6 +260,8 @@ true
230260

231261
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
232262

263+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
264+
233265
</section>
234266

235267
<!-- /.links -->

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

3335

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

lib/node_modules/@stdlib/assert/is-uri/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/'.

lib/node_modules/@stdlib/assert/is-uri/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/node_modules/@stdlib/assert/is-uri/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 \'beep boop\thttp://google.com\'',
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 \'beep boop\thttp://google.com\'',
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)