Skip to content
Browse files

passing sync text

  • Loading branch information...
1 parent 3ba2355 commit d603d3a24fe812490f0396d3666aa01dc2c23dc0 @substack committed Dec 3, 2011
Showing with 68 additions and 0 deletions.
  1. 0 example/{transform.js → async.js}
  2. +9 −0 example/sync.js
  3. +59 −0 test/sync.js
View
0 example/transform.js → example/async.js
File renamed without changes.
View
9 example/sync.js
@@ -0,0 +1,9 @@
+var transfuse = require('../');
+
+var tr = transfuse(function (doc) {
+ return { x : doc.a || doc.b };
+});
+
+process.stdin.pipe(tr);
+tr.pipe(process.stdout);
+process.stdin.resume();
View
59 test/sync.js
@@ -0,0 +1,59 @@
+var transfuse = require('../');
+var test = require('tap').test;
+var Stream = require('stream').Stream;
+var chunky = require('chunky');
+
+test('sync', function (t) {
+ var pending = 20;
+ t.plan(pending * 2);
+
+ check(t, function end () {
+ if (--pending === 0) t.end()
+ else check(t, end)
+ });
+});
+
+function check (t, end) {
+ var tr = transfuse(function (doc) {
+ return { x : (doc.a || doc.b) + doc.c };
+ });
+
+ var stream = {
+ in : new Stream,
+ out : new Stream,
+ };
+ stream.in.readable = true;
+ stream.out.writable = true;
+
+ var data = [];
+ stream.out.write = function (buf) {
+ data.push(buf.toString());
+ };
+ stream.out.end = function () {
+ t.equal(data.length, 4);
+ t.deepEqual(
+ JSON.parse(data.join('')),
+ [ { x : 102 }, { x : 6 }, { x : 0 } ]
+ );
+ end();
+ };
+
+ stream.in.pipe(tr);
+ tr.pipe(stream.out);
+
+ var buffers = chunky(JSON.stringify([
+ { a : 2, c : 100 },
+ { b : 5, c : 1 },
+ { a : 5, b : 10, c : -5 },
+ ]));
+
+ var iv = setInterval(function () {
+ var buf = buffers.shift();
+ stream.in.emit('data', buf);
+
+ if (buffers.length === 0) {
+ clearInterval(iv);
+ stream.in.emit('end');
+ }
+ }, 10);
+}

0 comments on commit d603d3a

Please sign in to comment.
Something went wrong with that request. Please try again.