Skip to content

Commit

Permalink
Change error behavior on empty / full
Browse files Browse the repository at this point in the history
If the queue is empty, attempting to dequeue something will return
undefined. If the queue is full, attempting to insert something will
return an error.
  • Loading branch information
erayd committed Sep 11, 2016
1 parent a8028f2 commit 225e7f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Spique = require('spique');
var s = new Spique(maxItems, ringSize);
```
`maxItems` sets the maximum number of items which may be stored in the queue at
any given time. Attempting to store more items than this will throw an error. If
any given time. Attempting to store more items than this will return an error. If
maxItems is falsy, then there is no maximum, and the queue may continue to grow
as long as there is available memory. By default, `maxItems` is unlimited.

Expand All @@ -44,7 +44,7 @@ s.push(myValue...);
s.enqueue(myValue...);
```
Append a value to the end of the queue. If a max queue size has been set, and the
queue is full, then this method will throw an error. `enqueue()` and `push()` are
queue is full, then this method will return an error. `enqueue()` and `push()` are
synonymous.

If more than one value is supplied, then they will all be added to the queue
Expand All @@ -56,15 +56,15 @@ var myValue = s.shift();
var myValue = s.dequeue();
```
Return the value at the head of the queue, and remove it from the queue. If the
queue is empty, this method will throw an error. `dequeue()` and `shift()` are
queue is empty, this method will return undefined. `dequeue()` and `shift()` are
synonymous.

### .unshift()
```javascript
s.unshift(myValue...);
```
Prepend a value to the head of the queue. If a max queue size has been set, and
the queue is full, then this method will throw an error.
the queue is full, then this method will return an error.

If more than one value is supplied, then they will all be added to the queue
in the order that they are supplied.
Expand All @@ -74,21 +74,21 @@ in the order that they are supplied.
var myValue = s.pop();
```
Return the value at the end of the queue, and remove it from the queue. If the
queue is empty, then this method will throw an error.
queue is empty, then this method will return undefined.

### .peek()
```javascript
var myValue = s.peek();
```
Return the value at the end of the queue. The value is not removed. If the queue
is empty, then this method will throw an error.
is empty, then this method will return undefined.

### .peekStart()
```javascript
var myValue = s.peekStart();
```
Return the value at the head of the queue. The value is not removed. If the queue
is empty, then this method will throw an error.
is empty, then this method will return undefined.

### Properties
#### .length
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spique",
"version": "1.0.5",
"version": "1.0.6",
"description": "A spiral deque - high performance and dynamic queue size",
"main": "spique.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions spique.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Spique(maxItems, circumference) {
this.enqueue = this.push = function() {
for(value of arguments) {
if(items >= maxItems)
throw new Error('Buffer is full');
return new Error('Buffer is full');
// add another ring if necessary
if(!lastRing.available()) {
var newRing = new RingBuffer(circumference);
Expand All @@ -53,7 +53,7 @@ function Spique(maxItems, circumference) {
this.unshift = function(value) {
for(value of arguments) {
if(items >= maxItems)
throw new Error('Buffer is full');
return new Error('Buffer is full');
// add another ring if necessary
if(!firstRing.available()) {
var newRing = new RingBuffer(circumference);
Expand Down Expand Up @@ -161,7 +161,7 @@ function RingBuffer(size) {
// pop an item off the end of the buffer
this.pop = function() {
if(this.isEmpty())
throw new Error('Buffer is empty');
return undefined;
var pos = (head + --items) % size;
var value = buffer[pos];
delete buffer[pos];
Expand All @@ -171,7 +171,7 @@ function RingBuffer(size) {
// pop an item off the start of the buffer
this.shift = function() {
if(this.isEmpty())
throw new Error('Buffer is empty');
return undefined;
var value = buffer[head];
delete buffer[head];
if(++head == size)
Expand All @@ -183,14 +183,14 @@ function RingBuffer(size) {
// peek at the end of the buffer
this.peek = function() {
if(this.isEmpty())
throw new Error('Buffer is empty');
return undefined;
return buffer[(head + (items - 1)) % size];
};

// peek at the start of the buffer
this.peekStart = function() {
if(this.isEmpty())
throw new Error('Buffer is empty');
return undefined;
return buffer[head];
};

Expand Down

0 comments on commit 225e7f0

Please sign in to comment.