Closed danielstjules closed 9 years ago
well, it's like that because it's the same as String#split
there is also a mapper function though, which you can use to filter out any undefined lines.
input .pipe(split(null, function (e) { return e ? e : undefined }))
see here: https://github.com/dominictarr/split/blob/master/index.js#L25-L36 pass the first argument as null so that you still get the default matcher.
Thanks for the quick response! I realize my use-case might be a bit odd, but I'm hoping to only drop the last line if it's null - no others. So the mapper wouldn't end up working for me.
I'm trying to translate the following blocking example to using streams for a simple cli tool:
var pattern, data, newline, lines;
pattern = /\r\n|\r|\n/;
data = fs.readFileSync(path, {encoding: 'utf8'});
newline = data.match(pattern);
lines = data.split(newline);
// Ignore an empty last line
if (lines[lines.length - 1] === null) {
lines.pop();
}
return lines;
I'm thinking this might not be possible?
To clarify my above above post: by not possible, I mean without an intermediary stream that serves as a buffer, so that I can identify the last line by the end
event.
can you describe your usecase?
Contrived example: I want to count the number of lines in a file, a la wc -l
.
function example(array) {
array = array.map(function(x) {
return x * 2;
});
return new Foo(array);
}
It has 7 lines, but I'd have 8 elements pushed out of the split stream. Note, I can't just get the count and subtract 1. While it needs to accommodate this scenario, it's a cli tool for applying different filter/map/reduce operations
It seems to me that I'll have to have an intermediary stream that keeps a buffer of 1 object. That way I can catch the src stream end event, check if the last element was null, and drop it if so.
Gonna close this up - concluded that an intermediary stream would be the only way to approach this. Thanks for the help though!
In reference to: https://twitter.com/danielstjules/status/499816110066122752
I was wondering if there was an existing convention for ignoring the last emitted line from the stream, in particular when it's null? For example, a file with the structure
1\n2\n3\n
currently emits 4 lines['1', '2', '3', '']
. Instead, I'd like to have it emit only 3:['1','2','3']
Thanks!