Skip to content

Commit 03881e5

Browse files
committed
Add and document split option for CLI
1 parent 6aaf19b commit 03881e5

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

lib/node_modules/@stdlib/string/truncate-middle/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Options:
130130
-V, --version Print the package version.
131131
--len length String length.
132132
--seq str Custom replacement sequence. Default: '...'.
133+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
133134
```
134135

135136
</section>
@@ -140,6 +141,20 @@ Options:
140141

141142
<section class="notes">
142143

144+
### Notes
145+
146+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
147+
148+
```bash
149+
# Not escaped...
150+
$ echo -n $'Hello, World!\nBeep Boop Baz' | truncate-middle --len 6 --split /\r?\n/
151+
152+
# Escaped...
153+
$ echo -n $'Hello, World!\nBeep Boop Baz' | truncate-middle --len 6 --split /\\r?\\n/
154+
```
155+
156+
- The implementation ignores trailing delimiters.
157+
143158
</section>
144159

145160
<!-- /.notes -->
@@ -158,6 +173,21 @@ $ truncate-middle 'Hello, World!' --len 6 --seq '!'
158173
Hel|d!
159174
```
160175

176+
To use as a [standard stream][standard-streams],
177+
178+
```bash
179+
$ echo -n 'Hello, World!' | truncate-middle --len 8
180+
Hel...d!
181+
```
182+
183+
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.
184+
185+
```bash
186+
$ echo -n 'Hello, World!\tBeep Boop' | truncate-middle --split '\t' --len 6 --seq '!'
187+
Hel|d!
188+
Bee|op
189+
```
190+
161191
</section>
162192

163193
<!-- /.examples -->
@@ -192,6 +222,10 @@ Hel|d!
192222

193223
<section class="links">
194224

225+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
226+
227+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
228+
195229
<!-- <related-links> -->
196230

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

lib/node_modules/@stdlib/string/truncate-middle/bin/cli

+12-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 truncateMiddle = require( './../lib' );
3234

3335

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

6871
// Check if we are receiving data from `stdin`...
6972
if ( !stdinStream.isTTY ) {
73+
if ( flags.split ) {
74+
if ( !isRegExpString( flags.split ) ) {
75+
flags.split = '/'+flags.split+'/';
76+
}
77+
split = reFromString( flags.split );
78+
} else {
79+
split = RE_EOL;
80+
}
7081
return stdin( onRead );
7182
}
7283
if ( flags.seq ) {
@@ -89,7 +100,7 @@ function main() {
89100
if ( error ) {
90101
return cli.error( error );
91102
}
92-
lines = data.toString().split( RE_EOL );
103+
lines = data.toString().split( split );
93104
if ( flags.seq ) {
94105
for ( i = 0; i < lines.length; i++ ) {
95106
console.log( truncateMiddle( lines[ i ], len, flags.seq ) ); // eslint-disable-line no-console

lib/node_modules/@stdlib/string/truncate-middle/docs/usage.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Options:
77
-V, --version Print the package version.
88
--len length String length.
99
--seq str Custom replacement sequence. Default: '...'.
10+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

lib/node_modules/@stdlib/string/truncate-middle/etc/cli_opts.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"string": [
33
"len",
4-
"seq"
4+
"seq",
5+
"split"
56
],
67
"boolean": [
78
"help",

lib/node_modules/@stdlib/string/truncate-middle/test/test.cli.js

+46
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,52 @@ tape( 'the command-line interface supports use as a standard stream (custom sequ
235235
}
236236
});
237237

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

0 commit comments

Comments
 (0)