Comparing version 1.1.1 to 2.0.0
150
cbuffer.js
@@ -15,5 +15,5 @@ (function (global) { | ||
if (arguments.length === 0) | ||
throw new Error('Missing Argument: You must pass a valid buffer length'); | ||
throw new Error('Missing Argument: You must pass a valid buffer size'); | ||
// this is the same in either scenario | ||
this.size = this.start = 0; | ||
this.length = this.start = 0; | ||
// set to callback fn if data is about to be overwritten | ||
@@ -24,7 +24,7 @@ this.overflow = null; | ||
this.data = new Array(arguments.length); | ||
this.end = (this.length = arguments.length) - 1; | ||
this.end = (this.size = arguments.length) - 1; | ||
this.push.apply(this, arguments); | ||
} else { | ||
this.data = new Array(arguments[0]); | ||
this.end = (this.length = arguments[0]) - 1; | ||
this.end = (this.size = arguments[0]) - 1; | ||
} | ||
@@ -47,8 +47,8 @@ // need to `return this` so `return CBuffer.apply` works | ||
var item; | ||
if (this.size === 0) return; | ||
if (this.length === 0) return; | ||
item = this.data[this.end]; | ||
// remove the reference to the object so it can be garbage collected | ||
delete this.data[this.end]; | ||
this.end = (this.end - 1 + this.length) % this.length; | ||
this.size--; | ||
this.end = (this.end - 1 + this.size) % this.size; | ||
this.length--; | ||
return item; | ||
@@ -60,6 +60,6 @@ }, | ||
// check if overflow is set, and if data is about to be overwritten | ||
if (this.overflow && this.size + arguments.length > this.length) { | ||
if (this.overflow && this.length + arguments.length > this.size) { | ||
// call overflow function and send data that's about to be overwritten | ||
for (; i < this.size + arguments.length - this.length; i++) { | ||
this.overflow(this.data[(this.end + i + 1) % this.length], this); | ||
for (; i < this.length + arguments.length - this.size; i++) { | ||
this.overflow(this.data[(this.end + i + 1) % this.size], this); | ||
} | ||
@@ -70,15 +70,15 @@ } | ||
for (i = 0; i < arguments.length; i++) { | ||
this.data[(this.end + i + 1) % this.length] = arguments[i]; | ||
this.data[(this.end + i + 1) % this.size] = arguments[i]; | ||
} | ||
// recalculate size | ||
if (this.size < this.length) { | ||
if (this.size + i > this.length) this.size = this.length; | ||
else this.size += i; | ||
// recalculate length | ||
if (this.length < this.size) { | ||
if (this.length + i > this.size) this.length = this.size; | ||
else this.length += i; | ||
} | ||
// recalculate end | ||
this.end = (this.end + i) % this.length; | ||
this.end = (this.end + i) % this.size; | ||
// recalculate start | ||
this.start = (this.length + this.end - this.size + 1) % this.length; | ||
this.start = (this.size + this.end - this.length + 1) % this.size; | ||
// return number current number of items in CBuffer | ||
return this.size; | ||
return this.length; | ||
}, | ||
@@ -89,6 +89,6 @@ // reverse order of the buffer | ||
tmp; | ||
for (; i < ~~(this.size / 2); i++) { | ||
tmp = this.data[(this.start + i) % this.length]; | ||
this.data[(this.start + i) % this.length] = this.data[(this.start + (this.size - i - 1)) % this.length]; | ||
this.data[(this.start + (this.size - i - 1)) % this.length] = tmp; | ||
for (; i < ~~(this.length / 2); i++) { | ||
tmp = this.data[(this.start + i) % this.size]; | ||
this.data[(this.start + i) % this.size] = this.data[(this.start + (this.length - i - 1)) % this.size]; | ||
this.data[(this.start + (this.length - i - 1)) % this.size] = tmp; | ||
} | ||
@@ -119,9 +119,9 @@ return this; | ||
// check if there are any items in CBuff | ||
if (this.size === 0) return; | ||
if (this.length === 0) return; | ||
// store first item for return | ||
item = this.data[this.start]; | ||
// recalculate start of CBuffer | ||
this.start = (this.start + 1) % this.length; | ||
// decrement size | ||
this.size--; | ||
this.start = (this.start + 1) % this.size; | ||
// decrement length | ||
this.length--; | ||
return item; | ||
@@ -133,3 +133,3 @@ }, | ||
this.start = 0; | ||
this.end = this.size - 1; | ||
this.end = this.length - 1; | ||
return this; | ||
@@ -141,22 +141,22 @@ }, | ||
// check if overflow is set, and if data is about to be overwritten | ||
if (this.overflow && this.size + arguments.length > this.length) { | ||
if (this.overflow && this.length + arguments.length > this.size) { | ||
// call overflow function and send data that's about to be overwritten | ||
for (; i < this.size + arguments.length - this.length; i++) { | ||
this.overflow(this.data[this.end - (i % this.length)], this); | ||
for (; i < this.length + arguments.length - this.size; i++) { | ||
this.overflow(this.data[this.end - (i % this.size)], this); | ||
} | ||
} | ||
for (i = 0; i < arguments.length; i++) { | ||
this.data[(this.length + this.start - (i % this.length) - 1) % this.length] = arguments[i]; | ||
this.data[(this.size + this.start - (i % this.size) - 1) % this.size] = arguments[i]; | ||
} | ||
if (this.length - this.size - i < 0) { | ||
this.end += this.length - this.size - i; | ||
if (this.end < 0) this.end = this.length + (this.end % this.length); | ||
if (this.size - this.length - i < 0) { | ||
this.end += this.size - this.length - i; | ||
if (this.end < 0) this.end = this.size + (this.end % this.size); | ||
} | ||
if (this.size < this.length) { | ||
if (this.size + i > this.length) this.size = this.length; | ||
else this.size += i; | ||
if (this.length < this.size) { | ||
if (this.length + i > this.size) this.length = this.size; | ||
else this.length += i; | ||
} | ||
this.start -= arguments.length; | ||
if (this.start < 0) this.start = this.length + (this.start % this.length); | ||
return this.size; | ||
if (this.start < 0) this.start = this.size + (this.start % this.size); | ||
return this.length; | ||
}, | ||
@@ -168,4 +168,4 @@ | ||
if (!idx) idx = 0; | ||
for (; idx < this.size; idx++) { | ||
if (this.data[(this.start + idx) % this.length] === arg) return idx; | ||
for (; idx < this.length; idx++) { | ||
if (this.data[(this.start + idx) % this.size] === arg) return idx; | ||
} | ||
@@ -176,5 +176,5 @@ return -1; | ||
lastIndexOf : function (arg, idx) { | ||
if (!idx) idx = this.size - 1; | ||
if (!idx) idx = this.length - 1; | ||
for (; idx >= 0; idx--) { | ||
if (this.data[(this.start + idx) % this.length] === arg) return idx; | ||
if (this.data[(this.start + idx) % this.size] === arg) return idx; | ||
} | ||
@@ -188,4 +188,5 @@ return -1; | ||
comparitor = comparitor || defaultComparitor; | ||
var low = this.start, | ||
high = this.size - 1; | ||
var isFull = this.length === this.size, | ||
low = this.start, | ||
high = isFull ? this.length - 1 : this.length; | ||
@@ -204,4 +205,5 @@ // Tricky part is finding if its before or after the pivot | ||
} | ||
// http://stackoverflow.com/a/18618273/1517919 | ||
return (((low - this.start) % this.size) + this.size) % this.size; | ||
return !isFull ? low : | ||
// http://stackoverflow.com/a/18618273/1517919 | ||
(((low - this.start) % this.length) + this.length) % this.length; | ||
}, | ||
@@ -213,4 +215,4 @@ | ||
var i = 0; | ||
for (; i < this.size; i++) { | ||
if (!callback.call(context, this.data[(this.start + i) % this.length], i, this)) | ||
for (; i < this.length; i++) { | ||
if (!callback.call(context, this.data[(this.start + i) % this.size], i, this)) | ||
return false; | ||
@@ -224,4 +226,4 @@ } | ||
var i = 0; | ||
for (; i < this.size; i++) { | ||
callback.call(context, this.data[(this.start + i) % this.length], i, this); | ||
for (; i < this.length; i++) { | ||
callback.call(context, this.data[(this.start + i) % this.size], i, this); | ||
} | ||
@@ -233,4 +235,4 @@ }, | ||
var i = 0; | ||
for (; i < this.size; i++) { | ||
if (callback.call(context, this.data[(this.start + i) % this.length], i, this)) | ||
for (; i < this.length; i++) { | ||
if (callback.call(context, this.data[(this.start + i) % this.size], i, this)) | ||
return true; | ||
@@ -242,7 +244,7 @@ } | ||
avg : function () { | ||
return this.size == 0 ? 0 : (this.sum() / this.size); | ||
return this.length == 0 ? 0 : (this.sum() / this.length); | ||
}, | ||
// loop through each item in buffer and calculate sum | ||
sum : function () { | ||
var index = this.size; | ||
var index = this.length; | ||
var s = 0; | ||
@@ -254,3 +256,3 @@ while (index--) s += this.data[index]; | ||
median : function () { | ||
if (this.size === 0) | ||
if (this.length === 0) | ||
return 0; | ||
@@ -270,4 +272,4 @@ var values = this.slice().sort(defaultComparitor); | ||
var i = 0; | ||
this.size = this.start = 0; | ||
this.end = this.length - 1; | ||
this.length = this.start = 0; | ||
this.end = this.size - 1; | ||
return this; | ||
@@ -279,10 +281,10 @@ }, | ||
if (typeof arg === 'function') { | ||
while(this.data[i] = arg(), ++i < this.length); | ||
while(this.data[i] = arg(), ++i < this.size); | ||
} else { | ||
while(this.data[i] = arg, ++i < this.length); | ||
while(this.data[i] = arg, ++i < this.size); | ||
} | ||
// reposition start/end | ||
this.start = 0; | ||
this.end = this.length - 1; | ||
this.size = this.length; | ||
this.end = this.size - 1; | ||
this.length = this.size; | ||
return this; | ||
@@ -300,10 +302,10 @@ }, | ||
get : function (arg) { | ||
return this.data[(this.start + arg) % this.length]; | ||
return this.data[(this.start + arg) % this.size]; | ||
}, | ||
isFull : function (arg) { | ||
return this.length === this.size; | ||
return this.size === this.length; | ||
}, | ||
// set value at specified index | ||
set : function (idx, arg) { | ||
return this.data[(this.start + idx) % this.length] = arg; | ||
return this.data[(this.start + idx) % this.size] = arg; | ||
}, | ||
@@ -316,3 +318,3 @@ // return clean array of values | ||
slice : function (start, end) { | ||
var length = this.size; | ||
var size = this.length; | ||
@@ -324,17 +326,17 @@ start = +start || 0; | ||
return []; | ||
start = (-start > length) ? 0 : length + start; | ||
start = (-start > size) ? 0 : size + start; | ||
} | ||
if (end == null || end > length) | ||
end = length; | ||
if (end == null || end > size) | ||
end = size; | ||
else if (end < 0) | ||
end += length; | ||
end += size; | ||
else | ||
end = +end || 0; | ||
length = start < end ? end - start : 0; | ||
size = start < end ? end - start : 0; | ||
var result = Array(length); | ||
for (var index = 0; index < length; index++) { | ||
result[index] = this.data[(this.start + start + index) % this.length]; | ||
var result = Array(size); | ||
for (var index = 0; index < size; index++) { | ||
result[index] = this.data[(this.start + start + index) % this.size]; | ||
} | ||
@@ -341,0 +343,0 @@ return result; |
{ | ||
"name" : "CBuffer", | ||
"version" : "1.1.1", | ||
"version" : "2.0.0", | ||
"description" : "Circular Buffer JavaScript implementation", | ||
@@ -5,0 +5,0 @@ "homepage" : "https://github.com/trevnorris/cbuffer", |
@@ -18,4 +18,4 @@ ## JavaScript [Circular Buffer](http://en.wikipedia.org/wiki/Circular_buffer) Utility | ||
```javascript | ||
new CBuffer(10); // empty buffer with length of 10 | ||
new CBuffer(1,2,3,4); // buffer with length 4 | ||
new CBuffer(10); // empty buffer with size of 10 | ||
new CBuffer(1,2,3,4); // buffer with size 4 | ||
CBuffer(5); // For those who are really lazy, new is optional | ||
@@ -44,3 +44,3 @@ ``` | ||
* pop - Removes the last element from a circular buffer and returns that element. | ||
* push - Adds one or more elements to the end of a circular buffer and returns the new size. | ||
* push - Adds one or more elements to the end of a circular buffer and returns the new length. | ||
* reverse - Reverses the order of the elements of a circular buffer. | ||
@@ -51,3 +51,3 @@ * rotateLeft - Rotates all elements left 1, or n, times. | ||
* sort - Sorts the elements of a circular buffer. Unlike native `sort`, the default comparitor sorts by `a > b`. | ||
* unshift - Adds one or more elements to the front of a circular buffer and returns the new size. | ||
* unshift - Adds one or more elements to the front of a circular buffer and returns the new length. | ||
@@ -54,0 +54,0 @@ #### Accessor Methods |
@@ -43,2 +43,8 @@ var vows = require('vows'), | ||
assert.equal(buffer.sortedIndex(8), 7); | ||
}, | ||
'can determine postion in a fixed length buffer': function(buffer) { | ||
assert.equal(buffer.sortedIndex(0), 0); | ||
assert.equal(buffer.sortedIndex(1), 0); | ||
assert.equal(buffer.sortedIndex(3), 2); | ||
assert.equal(buffer.sortedIndex(10), 8); | ||
} | ||
@@ -94,4 +100,2 @@ }, | ||
} | ||
} | ||
@@ -98,0 +102,0 @@ }); |
@@ -33,7 +33,7 @@ var vows = require('vows'), | ||
'length' : function (CBuffer) { | ||
assert.equal(CBuffer(3).length, 3); | ||
assert.equal(CBuffer(3).length, 0); | ||
assert.equal(CBuffer(1, 2, 3).length, 3); | ||
}, | ||
'size' : function (CBuffer) { | ||
assert.equal(CBuffer(3).size, 0); | ||
assert.equal(CBuffer(3).size, 3); | ||
assert.equal(CBuffer(1, 2, 3).size, 3); | ||
@@ -40,0 +40,0 @@ }, |
@@ -31,3 +31,3 @@ var vows = require('vows'), | ||
assert.equal(tmp.end, 1); | ||
assert.equal(tmp.size, 2); | ||
assert.equal(tmp.length, 2); | ||
} | ||
@@ -34,0 +34,0 @@ } |
@@ -26,3 +26,3 @@ var vows = require('vows'), | ||
tmp.push(1, 2); | ||
assert.equal(tmp.size, 2); | ||
assert.equal(tmp.length, 2); | ||
assert.equal(tmp.start, 0); | ||
@@ -29,0 +29,0 @@ assert.equal(tmp.end, 1); |
@@ -29,3 +29,3 @@ var vows = require('vows'), | ||
tmp.shift(); | ||
assert.equal(tmp.size, 2); | ||
assert.equal(tmp.length, 2); | ||
assert.equal(tmp.start, 1); | ||
@@ -37,3 +37,3 @@ assert.equal(tmp.end, 2); | ||
tmp.shift(); | ||
assert.equal(tmp.size, 2); | ||
assert.equal(tmp.length, 2); | ||
assert.equal(tmp.start, 2); | ||
@@ -40,0 +40,0 @@ assert.equal(tmp.end, 0); |
@@ -26,3 +26,3 @@ var vows = require('vows'), | ||
tmp.unshift(1, 2); | ||
assert.equal(tmp.size, 2); | ||
assert.equal(tmp.length, 2); | ||
assert.equal(tmp.start, 1); | ||
@@ -29,0 +29,0 @@ assert.equal(tmp.end, 2); |
@@ -17,3 +17,3 @@ var vows = require('vows'), | ||
tmp.empty(); | ||
assert.equal(tmp.size, 0); | ||
assert.equal(tmp.length, 0); | ||
}, | ||
@@ -20,0 +20,0 @@ 'fill' : function (CBuffer) { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33897
1071
0