unshift.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var pipeline = require('../');
  2. var through = require('through2');
  3. var test = require('tape');
  4. test('unshift', function (t) {
  5. var expected = {};
  6. expected.a = [ 5, 6 ];
  7. expected.b = [ 3, 4, 500, 600 ];
  8. expected.c = [ 13, 14, 510, 610 ];
  9. expected.output = [ 13/2, 7, 255, 305 ];
  10. t.plan(2 + 4 + 4 + 4);
  11. var a = through.obj(function (x, enc, next) {
  12. var ex = expected.a.shift();
  13. t.equal(x, ex, 'a');
  14. this.push(x * 100);
  15. next();
  16. });
  17. var b = through.obj(function (x, enc, next) {
  18. var ex = expected.b.shift();
  19. t.equal(x, ex, 'b');
  20. if (expected.b.length === 2) p.unshift(a)
  21. this.push(x + 10);
  22. next();
  23. });
  24. var c = through.obj(function (x, enc, next) {
  25. var ex = expected.c.shift();
  26. t.equal(x, ex, 'c');
  27. this.push(x / 2);
  28. next();
  29. });
  30. var p = pipeline.obj([ b, c ]);
  31. p.pipe(through.obj(function (x, enc, next) {
  32. var ex = expected.output.shift();
  33. t.equal(x, ex);
  34. next();
  35. }));
  36. p.write(3);
  37. p.write(4);
  38. p.write(5);
  39. p.write(6);
  40. p.end();
  41. });