Skip to content

Commit bab1a80

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

5 files changed

Lines changed: 101 additions & 3 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,35 @@ Options:
115115
-V, --version Print the package version.
116116
--len length Minimum string length.
117117
--pad str String used to pad. Default: ' '.
118+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
118119
```
119120

120121
</section>
121122

122123
<!-- /.usage -->
123124

125+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="notes">
128+
129+
### Notes
130+
131+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
132+
133+
```bash
134+
# Not escaped...
135+
$ echo -n $'beep\nboop' | lpad -len 8 --split /\r?\n/
136+
137+
# Escaped...
138+
$ echo -n $'beep\nboop' | lpad -len 8 --split /\\r?\\n/
139+
```
140+
141+
- The implementation ignores trailing delimiters.
142+
143+
</section>
144+
145+
<!-- /.notes -->
146+
124147
<section class="examples">
125148

126149
### Examples
@@ -137,6 +160,14 @@ $ echo -n 'beep' | lpad --len 8
137160
beep
138161
```
139162

163+
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.
164+
165+
```bash
166+
$ echo -n 'boop\tbeep' | lpad --len 8 --split '\t'
167+
boop
168+
beep
169+
```
170+
140171
</section>
141172

142173
<!-- /.examples -->
@@ -166,6 +197,8 @@ $ echo -n 'beep' | lpad --len 8
166197

167198
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
168199

200+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
201+
169202
<!-- <related-links> -->
170203

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

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

3335

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

7780
// Check if we are receiving data from `stdin`...
7881
if ( !stdinStream.isTTY ) {
82+
if ( flags.split ) {
83+
if ( !isRegExpString( flags.split ) ) {
84+
flags.split = '/'+flags.split+'/';
85+
}
86+
split = reFromString( flags.split );
87+
} else {
88+
split = RE_EOL;
89+
}
7990
return stdin( onRead );
8091
}
8192
console.log( lpad( str, len, pad ) ); // eslint-disable-line no-console
@@ -94,7 +105,12 @@ function main() {
94105
if ( error ) {
95106
return cli.error( error );
96107
}
97-
lines = data.toString().split( RE_EOL );
108+
lines = data.toString().split( split );
109+
110+
// Remove any trailing separators (e.g., trailing newline)...
111+
if ( lines[ lines.length-1 ] === '' ) {
112+
lines.pop();
113+
}
98114
for ( i = 0; i < lines.length; i++ ) {
99115
console.log( lpad( lines[ i ], len, pad ) ); // eslint-disable-line no-console
100116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Options:
77
-V, --version Print the package version.
88
--len length Minimum string length.
99
--pad str String used to pad. Default: ' '.
10-
10+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
],
66
"string": [
77
"len",
8-
"pad"
8+
"pad",
9+
"split"
910
],
1011
"alias": {
1112
"help": [

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,54 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
209209
}
210210
});
211211

212+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) {
213+
var cmd = [
214+
'printf \'beep\tboop\'',
215+
'|',
216+
EXEC_PATH,
217+
fpath,
218+
'--split \'\t\'',
219+
'--len=6',
220+
'--pad=!'
221+
];
222+
223+
exec( cmd.join( ' ' ), done );
224+
225+
function done( error, stdout, stderr ) {
226+
if ( error ) {
227+
t.fail( error.message );
228+
} else {
229+
t.strictEqual( stdout.toString(), '!!beep\n!!boop\n', 'expected value' );
230+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
231+
}
232+
t.end();
233+
}
234+
});
235+
236+
tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) {
237+
var cmd = [
238+
'printf \'beep\tboop\'',
239+
'|',
240+
EXEC_PATH,
241+
fpath,
242+
'--split=/\\\\t/',
243+
'--len=6',
244+
'--pad=!'
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+
212260
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 ) {
213261
var script;
214262
var opts;

0 commit comments

Comments
 (0)