Check out a free preview of the full Networking and Streams course

The "split2" Lesson is part of the full, Networking and Streams course featured in this preview video. Here's what you'd learn in this lesson:

James introduces split2, which breaks up a stream and reassemble that stream so that each line is a chunk.


Transcript from the "split2" Lesson

>> So the next thing that I wanted to cover is a handy little module called split2, the 2 is because of this streams 2 interface. So this module can split on new lines or any other kind of delimiter. So if you, for example, have a little program here and you wanna count the number of lines, the number of times a new line occurs, you could do that by scanning through the buffers for new line characters.

But here's another way to do it where you actually buffer up the input chunks, which could be any size. So those input chunks could span new lines or not. But the split2 module, by default, will split on a new line delimiter. So you can be assured that each buffer will be a single line.

So here's a little program. I'll go ahead and just type this one out. So it's like line-count.js. So we load split2, we can also load through, And we can take stdin and pipe that into split, and pipe that into through. And then inside of through, every chunk should be a separate line.

So I can do, first, I'll just print out the buffer so you can see that everyone is on a single line. And if I run this, I gotta do next. Yeah, if your stream seems to stall, then you're probably not calling next and that's why. So if you ever hit that problem.

So one, two, three. Or just to prove that it's actually buffering by line, so I'll do it more in one chunk. So that's peculiar.
>> You didn't put it in [CROSSTALK]
>> Forgot your n.
>> Thank you. Yeah, it was a tab character. There we go. Three lines, fantastic.

So we can also now start counting the number of lines, so cap a little counter. I call it lineCount = 0. And then increament that for every time we get junk, I'm gonna move these out to an end functions cuz I think it's a little bit easier to do it that way.

So then in our end function, we can just print out the lineCount. The end function also gets the next callback, which if we were consuming the stream anywhere would forward along the sort of the stream signal, I guess you could call it. Anyways, this program is more less what wc -l does on the cmdline.

So okay, like if I do wc -l line-count-js, you see 16 lines. And now, if I run line-count program in node, I also get 16 lines, cool. Or you know any other file will work here as well. So I think code that txt has 3 lines yeah. Cool, so there's a example of a line counter using the split module.

One thing you should note about split is that it removes the delimiter. So just something to have to know about. You can also give it a custom regex. So if you remember from the really early on, we had the Moby-Dick example. So in that command line example, we split using said every whitespace character returned into a new line so that we could filter with grep.

So it's kind of a similar thing that you can do with this split package here. If you wanted to like do some kind of custom regex delimiter you can.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now