pop.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var pipeline = require('../');
  2. var through = require('through2');
  3. var split = require('split');
  4. var concat = require('concat-stream');
  5. var test = require('tape');
  6. test('pop', function (t) {
  7. var expected = {};
  8. expected.replacer = [ '333', '444' ];
  9. t.plan(3);
  10. var a = split();
  11. var b = through.obj(function (row, enc, next) {
  12. this.push(JSON.parse(row));
  13. next();
  14. });
  15. var c = through.obj(function (row, enc, next) {
  16. this.push(row.x);
  17. next();
  18. });
  19. var d = through.obj(function (x, enc, next) {
  20. this.push(String(x * 111));
  21. next();
  22. });
  23. var replacer = through(function (buf, enc, next) {
  24. var ex = expected.replacer.shift();
  25. t.equal(buf.toString(), ex);
  26. this.push(buf.toString('hex') + '\n');
  27. if (expected.replacer.length === 0) {
  28. stream.pop();
  29. }
  30. next();
  31. });
  32. var stream = pipeline([ a, b, c, d, replacer ]);
  33. stream.pipe(concat(function (body) {
  34. t.deepEqual(body.toString(), '333333\n343434\n555666');
  35. }));
  36. stream.write('{"x":3}\n');
  37. stream.write('{"x":4}\n');
  38. stream.write('{"x":5}\n');
  39. stream.write('{"x":6}');
  40. stream.end();
  41. });