Skip to content

Commit

Permalink
add convenience function skip()
Browse files Browse the repository at this point in the history
  • Loading branch information
bkw committed Oct 13, 2012
1 parent eda77c7 commit c282bee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ omitted, a null terminated string is assumed.

Returns the next `bytes` as a buffer.

### reader.skip([bytes])

Skips `bytes` bytes of the buffer.


## Writer API

The Writer has not been implemented yet.
Expand Down
7 changes: 7 additions & 0 deletions lib/Reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,10 @@ Reader.prototype.buffer = function(bytes) {
this._buffer.copy(ret, 0, this._offset - bytes, this._offset);
return ret;
};

Reader.prototype.skip = function(bytes) {
if (bytes > this.bytesAhead()) {
this.emit('error', new Error('tried to skip outsite of the buffer'));
}
this._offset += bytes;
};
7 changes: 7 additions & 0 deletions test/unit/test-Reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ test('Read Methods', {
var origResult = reader.buffer(1);
reader.write(new Buffer([2]));
assert.deepEqual(origResult, new Buffer([1]));
},

'skip' : function() {
var reader = new Reader(new Buffer([1, 2, 3, 4, 5]));
reader.skip(2);
assert.deepEqual(reader.buffer(3), new Buffer([3, 4, 5]));
assert.throws(function() { reader.skip(1); });
}
});

Expand Down

0 comments on commit c282bee

Please sign in to comment.