Skip to content

Commit

Permalink
Fixing CircularBuffer.push
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Jan 25, 2019
1 parent 186b319 commit fbb5240
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Adding `FixedDeque`.
* Adding `CircularBuffer.unshift`.
* Changing `CircularBuffer` semantics to now overwrite values when wrapping around.

## 0.26.0

Expand Down
44 changes: 44 additions & 0 deletions circular-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,50 @@ Object.keys(FixedDeque.prototype).forEach(paste);
if (typeof Symbol !== 'undefined')
Object.getOwnPropertySymbols(FixedDeque.prototype).forEach(paste);

/**
* Method used to append a value to the buffer.
*
* @param {any} item - Item to append.
* @return {number} - Returns the new size of the buffer.
*/
CircularBuffer.prototype.push = function(item) {
var index = (this.start + this.size) % this.capacity;

this.items[index] = item;

// Overwriting?
if (this.size === this.capacity) {

// If start is at the end, we wrap around the buffer
this.start = (index + 1) % this.capacity;

return this.size;
}

return ++this.size;
};

/**
* Method used to prepend a value to the buffer.
*
* @param {any} item - Item to prepend.
* @return {number} - Returns the new size of the buffer.
*/
CircularBuffer.prototype.unshift = function(item) {
if (this.size === this.capacity)
throw new Error('mnemonist/circular-buffer.unshift: buffer capacity (' + this.capacity + ') exceeded!');

var index = this.start - 1;

if (this.start === 0)
index = this.capacity - 1;

this.items[index] = item;
this.start = index;

return ++this.size;
};

/**
* Static @.from function taking an abitrary iterable & converting it into
* a circular buffer.
Expand Down
30 changes: 24 additions & 6 deletions test/circular-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,32 @@ describe('CircularBuffer', function() {
assert.strictEqual(buffer.capacity, 10);
});

it('exceeding capacity should throw.', function() {
var buffer = new CircularBuffer(Array, 1);
it('should be possible to wrap buffer around.', function() {
var buffer = new CircularBuffer(Array, 3);

buffer.push('test');
buffer.push(1);
buffer.push(2);
buffer.push(3);
buffer.push(4);

assert.throws(function() {
buffer.push('test');
}, /exceed/);
assert.deepEqual(buffer.toArray(), [2, 3, 4]);
assert.strictEqual(buffer.size, 3);

buffer.push(5);

assert.deepEqual(buffer.toArray(), [3, 4, 5]);
assert.strictEqual(buffer.size, 3);

buffer.push(6);

assert.deepEqual(buffer.toArray(), [4, 5, 6]);
assert.strictEqual(buffer.size, 3);

buffer.push(7);
buffer.push(8);

assert.deepEqual(buffer.toArray(), [6, 7, 8]);
assert.strictEqual(buffer.size, 3);
});

it('should be possible to clear the buffer.', function() {
Expand Down

0 comments on commit fbb5240

Please sign in to comment.