Skip to content

Commit b09d25d

Browse files
committed
Add split option support
1 parent 95ba099 commit b09d25d

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

lib/node_modules/@stdlib/string/repeat/README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,52 @@ Options:
9898

9999
<!-- /.usage -->
100100

101+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="notes">
104+
105+
### Notes
106+
107+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
108+
109+
```bash
110+
# Not escaped...
111+
$ echo -n $'beep\nboop' | repstr --n 3 --split /\r?\n/
112+
113+
# Escaped...
114+
$ echo -n $'beep\nboop' | repstr --n 3 --split /\\r?\\n/
115+
```
116+
117+
- The implementation ignores trailing delimiters.
118+
119+
</section>
120+
121+
<!-- /.notes -->
122+
101123
<section class="examples">
102124

103125
### Examples
104126

105127
```bash
106-
$ repstr beep -n 5
128+
$ repstr beep --n 5
107129
beepbeepbeepbeepbeep
108130
```
109131

110132
To use as a [standard stream][standard-streams],
111133

112134
```bash
113-
$ echo -n 'ab' | repstr -n 3
135+
$ echo -n $'ab' | repstr --n 3
114136
ababab
115137
```
116138

139+
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.
140+
141+
```bash
142+
$ echo -n $'beep\tboop' | repstr --n 3 --split '\t'
143+
beepbeepbeep
144+
boopboopboop
145+
```
146+
117147
</section>
118148

119149
<!-- /.examples -->
@@ -142,6 +172,8 @@ ababab
142172

143173
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
144174

175+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
176+
145177
<!-- <related-links> -->
146178

147179
[@stdlib/string/pad]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/pad

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

3335

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

6972
// Check if we are receiving data from `stdin`...
7073
if ( !stdinStream.isTTY ) {
74+
if ( flags.split ) {
75+
if ( !isRegExpString( flags.split ) ) {
76+
flags.split = '/'+flags.split+'/';
77+
}
78+
split = reFromString( flags.split );
79+
} else {
80+
split = RE_EOL;
81+
}
7182
return stdin( onRead );
7283
}
7384
console.log( repeat( args[ 0 ], n ) ); // eslint-disable-line no-console
@@ -86,7 +97,12 @@ function main() {
8697
if ( error ) {
8798
return cli.error( error );
8899
}
89-
lines = data.toString().split( RE_EOL );
100+
lines = data.toString().split( split );
101+
102+
// Remove any trailing separators (e.g., trailing newline)...
103+
if ( lines[ lines.length-1 ] === '' ) {
104+
lines.pop();
105+
}
90106
for ( i = 0; i < lines.length; i++ ) {
91107
console.log( repeat( lines[ i ], n ) ); // eslint-disable-line no-console
92108
}

lib/node_modules/@stdlib/string/repeat/docs/usage.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Options:
66
-h, --help Print this message.
77
-V, --version Print the package version.
88
--n repeats Number of repetitions.
9+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
910

lib/node_modules/@stdlib/string/repeat/etc/cli_opts.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"version"
55
],
66
"string": [
7-
"n"
7+
"n",
8+
"split"
89
],
910
"alias": {
1011
"help": [

lib/node_modules/@stdlib/string/repeat/test/test.cli.js

+46
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,52 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
186186
}
187187
});
188188

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

0 commit comments

Comments
 (0)