Skip to content

Commit 6a4a330

Browse files
committed
Add split option support
1 parent 2ed3342 commit 6a4a330

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,35 @@ Options:
212212
--lpad str String used to left pad. Default: ''.
213213
--rpad str String used to right pad. Default: ' '.
214214
--cright Center right in the event of a tie.
215+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
215216
```
216217

217218
</section>
218219

219220
<!-- /.usage -->
220221

222+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223+
224+
<section class="notes">
225+
226+
### Notes
227+
228+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
229+
230+
```bash
231+
# Not escaped...
232+
$ echo -n $'beep\nboop' | padstr --len 10 --split /\r?\n/
233+
234+
# Escaped...
235+
$ echo -n $'beep\nboop' | padstr --len 10 --split /\\r?\\n/
236+
```
237+
238+
- The implementation ignores trailing delimiters.
239+
240+
</section>
241+
242+
<!-- /.notes -->
243+
221244
<section class="examples">
222245

223246
### Examples
@@ -231,7 +254,15 @@ To use as a [standard stream][standard-streams],
231254

232255
```bash
233256
$ echo -n 'beep' | pad --len 9 --lpad a --rpad o
234-
aabeepoo0
257+
aabeepooo
258+
```
259+
260+
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.
261+
262+
```bash
263+
$ echo -n 'beep\tboop' | pad --len 9 --lpad a --rpad o --split '\t'
264+
aabeepooo
265+
aaboopooo
235266
```
236267

237268
</section>
@@ -263,6 +294,8 @@ aabeepoo0
263294

264295
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
265296

297+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
298+
266299
<!-- <related-links> -->
267300

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

lib/node_modules/@stdlib/string/pad/bin/cli

Lines changed: 16 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 pad = require( './../lib' );
3234

3335

@@ -85,6 +87,14 @@ function main() {
8587

8688
// Check if we are receiving data from `stdin`...
8789
if ( !stdinStream.isTTY ) {
90+
if ( flags.split ) {
91+
if ( !isRegExpString( flags.split ) ) {
92+
flags.split = '/'+flags.split+'/';
93+
}
94+
opts.split = reFromString( flags.split );
95+
} else {
96+
opts.split = RE_EOL;
97+
}
8898
return stdin( onRead );
8999
}
90100
console.log( pad( str, len, opts ) ); // eslint-disable-line no-console
@@ -103,7 +113,12 @@ function main() {
103113
if ( error ) {
104114
return cli.error( error );
105115
}
106-
lines = data.toString().split( RE_EOL );
116+
lines = data.toString().split( opts.split );
117+
118+
// Remove any trailing separators (e.g., trailing newline)...
119+
if ( lines[ lines.length-1 ] === '' ) {
120+
lines.pop();
121+
}
107122
for ( i = 0; i < lines.length; i++ ) {
108123
console.log( pad( lines[ i ], len, opts ) ); // eslint-disable-line no-console
109124
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Options:
99
--lpad str String used to left pad. Default: ''.
1010
--rpad str String used to right pad. Default: ' '.
1111
--cright Center right in the event of a tie.
12+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"string": [
88
"len",
99
"lpad",
10-
"rpad"
10+
"rpad",
11+
"split"
1112
],
1213
"alias": {
1314
"help": [

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,56 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
232232
}
233233
});
234234

235+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) {
236+
var cmd = [
237+
'printf \'beep\tboop\'',
238+
'|',
239+
EXEC_PATH,
240+
fpath,
241+
'--split \'\t\'',
242+
'--len=6',
243+
'--rpad=!',
244+
'--lpad=%'
245+
];
246+
247+
exec( cmd.join( ' ' ), done );
248+
249+
function done( error, stdout, stderr ) {
250+
if ( error ) {
251+
t.fail( error.message );
252+
} else {
253+
t.strictEqual( stdout.toString(), '%beep!\n%boop!\n', 'expected value' );
254+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
255+
}
256+
t.end();
257+
}
258+
});
259+
260+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) {
261+
var cmd = [
262+
'printf \'beep\tboop\'',
263+
'|',
264+
EXEC_PATH,
265+
fpath,
266+
'--split=/\\\\t/',
267+
'--len=6',
268+
'--rpad=!',
269+
'--lpad=%'
270+
];
271+
272+
exec( cmd.join( ' ' ), done );
273+
274+
function done( error, stdout, stderr ) {
275+
if ( error ) {
276+
t.fail( error.message );
277+
} else {
278+
t.strictEqual( stdout.toString(), '%beep!\n%boop!\n', 'expected value' );
279+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
280+
}
281+
t.end();
282+
}
283+
});
284+
235285
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 ) {
236286
var script;
237287
var opts;

0 commit comments

Comments
 (0)