until.js 816 B

123456789101112131415161718192021222324252627282930
  1. var test = require('tap').test;
  2. var streamBuffers = require("stream-buffers");
  3. var MatchStream = require('../');
  4. test("pipe until pattern", function (t) {
  5. t.plan(2);
  6. var ms = new MatchStream({ pattern: 'World'}, function (buf, matched, extra) {
  7. if (!matched) {
  8. return this.push(buf);
  9. }
  10. this.push(buf);
  11. t.equal(extra.toString(), 'World');
  12. return this.push(null); //end the stream
  13. });
  14. var sourceStream = new streamBuffers.ReadableStreamBuffer();
  15. sourceStream.put("Hello World");
  16. var writableStream = new streamBuffers.WritableStreamBuffer();
  17. sourceStream
  18. .pipe(ms)
  19. .pipe(writableStream)
  20. .once('close', function () {
  21. var str = writableStream.getContentsAsString('utf8');
  22. t.equal(str, 'Hello ');
  23. sourceStream.destroy();
  24. t.end();
  25. });
  26. });