Skip to content

Commit 0467cd2

Browse files
committed
Add split option support
1 parent c1bc2de commit 0467cd2

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

lib/node_modules/@stdlib/assert/is-email-address/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,35 @@ Options:
103103
104104
-h, --help Print this message.
105105
-V, --version Print the package version.
106+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
106107
```
107108

108109
</section>
109110

110111
<!-- /.usage -->
111112

113+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="notes">
116+
117+
### Notes
118+
119+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
120+
121+
```bash
122+
# Not escaped...
123+
$ echo -n $'foo@bar.com\nboop' | is-email --split /\r?\n/
124+
125+
# Escaped...
126+
$ echo -n $'foo@bar.com\nboop' | is-email --split /\\r?\\n/
127+
```
128+
129+
- The implementation ignores trailing delimiters.
130+
131+
</section>
132+
133+
<!-- /.notes -->
134+
112135
<section class="examples">
113136

114137
### Examples
@@ -125,6 +148,14 @@ $ echo -n 'beep@boop.com' | is-email
125148
true
126149
```
127150

151+
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.
152+
153+
```bash
154+
$ echo -n 'foo@bar.com\tbeep' | is-email --split '\t'
155+
true
156+
false
157+
```
158+
128159
</section>
129160

130161
<!-- /.examples -->
@@ -149,6 +180,8 @@ true
149180

150181
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
151182

183+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
184+
152185
</section>
153186

154187
<!-- /.links -->

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

3335

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

lib/node_modules/@stdlib/assert/is-email-address/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-email-address/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-email-address/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 \'foo@bar.com\tbeepBoop\'',
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 \'foo@bar.com\tbeepBoop\'',
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)