Skip to content

Commit 976ecbc

Browse files
committed
Auto-generated commit
1 parent 37206c3 commit 976ecbc

File tree

9 files changed

+102
-4
lines changed

9 files changed

+102
-4
lines changed

.github/.keepalive

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,35 @@ Options:
126126
127127
-h, --help Print this message.
128128
-V, --version Print the package version.
129+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
129130
```
130131

131132
</section>
132133

133134
<!-- /.usage -->
134135

136+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="notes">
139+
140+
### Notes
141+
142+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
143+
144+
```bash
145+
# Not escaped...
146+
$ echo -n $' \nboop' | remove-utf8-bom --split /\r?\n/
147+
148+
# Escaped...
149+
$ echo -n $' \nboop' | remove-utf8-bom --split /\\r?\\n/
150+
```
151+
152+
- The implementation ignores trailing delimiters.
153+
154+
</section>
155+
156+
<!-- /.notes -->
157+
135158
<section class="examples">
136159

137160
### Examples
@@ -150,6 +173,14 @@ $ echo -n '\ufeffbeep' | remove-utf8-bom
150173
beep
151174
```
152175

176+
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.
177+
178+
```bash
179+
$ echo -n '\ufeffbeep\t\ufeffboop' | remove-utf8-bom --split '\t'
180+
beep
181+
boop
182+
```
183+
153184
</section>
154185

155186
<!-- /.examples -->
@@ -239,6 +270,8 @@ Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
239270

240271
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
241272

273+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
274+
242275
</section>
243276

244277
<!-- /.links -->

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

3335

@@ -40,6 +42,7 @@ var removeBOM = 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( removeBOM( args[ 0 ] ) ); // eslint-disable-line no-console
@@ -83,7 +94,12 @@ function main() {
8394
return cli.error( error );
8495
}
8596

86-
lines = data.toString().split( RE_EOL );
97+
lines = data.toString().split( split );
98+
99+
// Remove any trailing separators (e.g., trailing newline)...
100+
if ( lines[ lines.length-1 ] === '' ) {
101+
lines.pop();
102+
}
87103
for ( i = 0; i < lines.length; i++ ) {
88104
console.log( removeBOM( lines[ i ] ) ); // eslint-disable-line no-console
89105
}

docs/usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Options:
55

66
-h, --help Print this message.
77
-V, --version Print the package version.
8+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.

etc/cli_opts.json

Lines changed: 3 additions & 0 deletions
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/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
// MODULES //
3434

35-
var removeUTF8BOM = require( './remove_utf_8_bom.js' );
35+
var removeUTF8BOM = require( './main.js' );
3636

3737

3838
// EXPORTS //
File renamed without changes.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
"url": "https://github.com/stdlib-js/stdlib/issues"
4141
},
4242
"dependencies": {
43+
"@stdlib/assert-is-regexp-string": "^0.0.x",
4344
"@stdlib/assert-is-string": "^0.0.x",
4445
"@stdlib/cli-ctor": "^0.0.x",
4546
"@stdlib/fs-read-file": "^0.0.x",
4647
"@stdlib/process-read-stdin": "^0.0.x",
4748
"@stdlib/regexp-eol": "^0.0.x",
4849
"@stdlib/streams-node-stdin": "^0.0.x",
49-
"@stdlib/string-format": "^0.0.x"
50+
"@stdlib/string-format": "^0.0.x",
51+
"@stdlib/utils-regexp-from-string": "^0.0.x"
5052
},
5153
"devDependencies": {
5254
"@stdlib/assert-is-browser": "^0.0.x",

test/test.cli.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,50 @@ tape( 'the command-line interface supports use as a standard stream', opts, func
188188
}
189189
});
190190

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

0 commit comments

Comments
 (0)