Permalink
Browse files

readable lines example

  • Loading branch information...
1 parent 55baf6f commit e2fbeb93f135739503ed3e1e3b03668bc2c970e4 @substack committed Jun 27, 2013
Showing with 54 additions and 2 deletions.
  1. +16 −0 example/basics/lines.js
  2. +38 −2 readme.markdown
View
@@ -0,0 +1,16 @@
+var offset = 0;
+
+process.stdin.on('readable', function () {
+ var buf = process.stdin.read();
+ if (!buf) return;
+ for (; offset < buf.length; offset++) {
+ if (buf[offset] === 0x0a) {
+ console.dir(buf.slice(0, offset).toString());
+ buf = buf.slice(offset + 1);
+ offset = 0;
+ process.stdin.unshift(buf);
+ return;
+ }
+ }
+ process.stdin.unshift(buf);
+});
View
@@ -351,12 +351,48 @@ $ (echo abc; sleep 1; echo def; sleep 1; echo ghi) | node consume2.js
<Buffer 68 69 0a>
```
-You can also use `.unshift()` to put back data if you over-zealously read too
-much. `.unshift()` is super handy for making chunking up input by lines:
+You can also use `.unshift()` to put back data so that the same read logic will
+fire when `.read()` gives you more data than you wanted.
+
+Using `.unshift()` prevents us from making unnecessary buffer copies. Here we
+can build a readable parser to split on newlines:
``` js
+var offset = 0;
+
+process.stdin.on('readable', function () {
+ var buf = process.stdin.read();
+ if (!buf) return;
+ for (; offset < buf.length; offset++) {
+ if (buf[offset] === 0x0a) {
+ console.dir(buf.slice(0, offset).toString());
+ buf = buf.slice(offset + 1);
+ offset = 0;
+ process.stdin.unshift(buf);
+ return;
+ }
+ }
+ process.stdin.unshift(buf);
+});
+```
```
+$ tail -n +50000 /usr/share/dict/american-english | head -n10 | node lines.js
+'hearties'
+'heartiest'
+'heartily'
+'heartiness'
+'heartiness\'s'
+'heartland'
+'heartland\'s'
+'heartlands'
+'heartless'
+'heartlessly'
+```
+
+However, there are modules on npm such as
+[split](https://npmjs.org/package/split) that you should use instead of rolling
+your own line-parsing logic.
## writable streams

0 comments on commit e2fbeb9

Please sign in to comment.