splice.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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('splice', function (t) {
  7. var expected = {};
  8. expected.replacer = [ '333', '444', '5000', '6000' ];
  9. expected.d = [ 3, 4 ];
  10. expected.thousander = [ 5, 6 ];
  11. t.plan(4 + 2 + 2 + 1);
  12. var a = split();
  13. var b = through.obj(function (row, enc, next) {
  14. this.push(JSON.parse(row));
  15. next();
  16. });
  17. var c = through.obj(function (row, enc, next) {
  18. this.push(row.x);
  19. next();
  20. });
  21. var d = through.obj(function (x, enc, next) {
  22. t.equal(x, expected.d.shift(), 'd');
  23. this.push(String(x * 111));
  24. next();
  25. });
  26. var thousander = through.obj(function (x, enc, next) {
  27. t.equal(x, expected.thousander.shift(), 'thousander');
  28. this.push(String(x * 1000));
  29. next();
  30. });
  31. var replacer = through(function (buf, enc, next) {
  32. var ex = expected.replacer.shift();
  33. t.equal(buf.toString(), ex);
  34. if (expected.replacer.length === 2) {
  35. stream.splice(3, 1, thousander);
  36. }
  37. this.push(buf.toString('hex') + '\n');
  38. next();
  39. });
  40. var stream = pipeline([ a, b, c, d, replacer ]);
  41. stream.pipe(concat(function (body) {
  42. t.deepEqual(
  43. body.toString(),
  44. '333333\n343434\n35303030\n36303030\n'
  45. );
  46. }));
  47. stream.write('{"x":3}\n');
  48. stream.write('{"x":4}\n');
  49. stream.write('{"x":5}\n');
  50. stream.write('{"x":6}');
  51. stream.end();
  52. });