Skip to content

Commit 71ec67f

Browse files
committed
Add split option and include missing CLI docs
1 parent 16906a7 commit 71ec67f

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

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

+81
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,85 @@ out = isWhitespace( 123 );
8080

8181
<!-- /.examples -->
8282

83+
* * *
84+
85+
<section class="cli">
86+
87+
## CLI
88+
89+
<section class="usage">
90+
91+
### Usage
92+
93+
```text
94+
Usage: is-whitespace [options] [<string>]
95+
96+
Options:
97+
98+
-h, --help Print this message.
99+
-V, --version Print the package version.
100+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
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 $' \nboop' | is-whitespace --split /\r?\n/
118+
119+
# Escaped...
120+
$ echo -n $' \nboop' | is-whitespace --split /\\r?\\n/
121+
```
122+
123+
- The implementation ignores trailing delimiters.
124+
125+
</section>
126+
127+
<!-- /.notes -->
128+
129+
<section class="examples">
130+
131+
### Examples
132+
133+
```bash
134+
$ is-whitespace foo
135+
false
136+
```
137+
138+
To use as a [standard stream][standard-streams],
139+
140+
```bash
141+
$ echo -n 'foo' | is-whitespace
142+
false
143+
```
144+
145+
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.
146+
147+
```bash
148+
$ echo -n ' \tbar' | is-whitespace --split '\t'
149+
true
150+
false
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
</section>
158+
159+
<!-- /.cli -->
160+
161+
83162
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
84163

85164
<section class="related">
@@ -100,6 +179,8 @@ out = isWhitespace( 123 );
100179

101180
[whitespace]: https://en.wikipedia.org/wiki/Whitespace_character
102181

182+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
183+
103184
<!-- <related-links> -->
104185

105186
[@stdlib/regexp/whitespace]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/regexp/whitespace

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

3335

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

lib/node_modules/@stdlib/assert/is-whitespace/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-whitespace/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-whitespace/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 \' \tbar\'',
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\nfalse\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 \' \tbar\'',
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\nfalse\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)