Skip to content

Commit

Permalink
Add reconfigure() to set and unset the pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Oxfeld committed Feb 26, 2013
1 parent 4be879b commit e8b49b6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ us.on('readable', function() {
* [new UntilStream([options])](#untilStreamConstructor)
* [read([size])](#untilStreamRead)
* [pipe(destination, [options])](#untilStreamPipe)
* [reconfigure([options])](#untilStreamReconfigure)

UntilStream also includes stream.Readable and
stream.Writable methods. See the node v0.9 [Stream documentation]
Expand Down Expand Up @@ -153,6 +154,32 @@ loremIpsumStream.pipe(us).pipe(outputStream).on('close', function() {
});
```

<a name="untilStreamReconfigure" />
### us.reconfigure([options])

Reconfigure the pattern option. It's unwise to call this method
while piping to a destination stream.

__Arguments__

* options (optional)
* pattern - String or Buffer If provided, UntilStream will
stop reads or pipes when reached

__Example__

```javascript
var us = new UntilStream();

us.write("Hello\nWorld");
us.reconfigure({ pattern: '\n' });
var hello = us.read();
console.log(hello.toString('utf8'));
us.read(); //matches '\n' pattern!
var world = us.read();
console.log(world.toString('utf8'));
```

## License

MIT
28 changes: 25 additions & 3 deletions test/setPattern.js → test/reconfigurePattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ test("set pattern after the fact", function (t) {
var us = new UntilStream();

var sourceStream = new streamBuffers.ReadableStreamBuffer();
sourceStream.put("The quick brown fox jumps over the lazy dog");
sourceStream.put('The quick brown fox jumps over the lazy dog');

us.on('finish', function () {
sourceStream.destroy();
us.setPattern('jumps');
us.reconfigure({ pattern: 'jumps' });
var writableStream = new streamBuffers.WritableStreamBuffer();
us.pipe(writableStream);
writableStream.on('close', function () {
Expand All @@ -28,4 +28,26 @@ test("set pattern after the fact", function (t) {
});

sourceStream.pipe(us);
});
});

test("remove pattern after the fact", function (t) {
t.plan(1);
var us = new UntilStream({ pattern: 'jumps' });

var sourceStream = new streamBuffers.ReadableStreamBuffer();
sourceStream.put('The quick brown fox jumps over the lazy dog');

us.on('finish', function () {
sourceStream.destroy();
us.reconfigure();
var writableStream = new streamBuffers.WritableStreamBuffer();
us.pipe(writableStream);
writableStream.on('close', function () {
var str = writableStream.getContentsAsString('utf8');
t.equal(str, 'The quick brown fox jumps over the lazy dog');
t.end();
});
});

sourceStream.pipe(us);
});
10 changes: 6 additions & 4 deletions until.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ inherits(Until, PassThrough);


function Until(opts) {
this._opts = opts || {};
this.setPattern(this._opts.pattern);
this.reconfigure(opts);
this._buf = Buffers();
this._flushing = false;
this.unpiping = false;
Expand Down Expand Up @@ -103,15 +102,18 @@ Until.prototype._flush = function(output, cb) {
process.nextTick(cb);
}

Until.prototype.setPattern = function (pattern) {
Until.prototype.reconfigure = function (opts) {
opts = opts || {};
var pattern = opts && opts.pattern ? opts.pattern : null;
if (pattern) {
if (typeof pattern === "string") {
pattern = new Buffer(pattern);
} else if (!pattern instanceof Buffer) {
throw new Error('Invalid pattern type')
}
this._opts.pattern = pattern;
}
this._opts = this._opts || opts;
this._opts.pattern = pattern;
};

function writePipes(data) {
Expand Down

0 comments on commit e8b49b6

Please sign in to comment.