Comparing version 2.0.0 to 2.1.0
@@ -37,2 +37,6 @@ (function (global) { | ||
function mod(n, m) { | ||
return ((n % m) + m) % m; | ||
} | ||
CBuffer.prototype = { | ||
@@ -217,4 +221,13 @@ // properly set constructor | ||
}, | ||
// construct new CBuffer of same length, apply map function, and return new CBuffer | ||
map : function (callback, context) { | ||
var outCBuffer = new CBuffer(this.size); | ||
for (var i = 0; i < this.length; i++) { | ||
var n = (this.start + i) % this.size; | ||
outCBuffer.push(callback.call(context, this.data[n], i, this)); | ||
} | ||
return outCBuffer; | ||
}, | ||
// check items agains test until one returns true | ||
// TODO: figure out how to emuldate Array use better | ||
// TODO: figure out how to emulate Array use better | ||
some : function (callback, context) { | ||
@@ -284,3 +297,3 @@ var i = 0; | ||
get : function (arg) { | ||
return this.data[(this.start + arg) % this.size]; | ||
return this.data[mod(this.start + arg, this.size)]; | ||
}, | ||
@@ -298,2 +311,12 @@ isFull : function (arg) { | ||
}, | ||
// return a string based on the array | ||
join : function(separator) { | ||
if (!separator) separator = ','; | ||
var outString = new String(this.data[0]); | ||
for (var i = 1; i < this.length; i++) { | ||
var n = (this.start + i) % this.size; | ||
outString = outString.concat(separator, this.data[i]); | ||
} | ||
return outString; | ||
}, | ||
// slice the buffer to an arraay | ||
@@ -300,0 +323,0 @@ slice : function (start, end) { |
{ | ||
"name" : "CBuffer", | ||
"version" : "2.0.0", | ||
"version" : "2.1.0", | ||
"description" : "Circular Buffer JavaScript implementation", | ||
@@ -12,2 +12,3 @@ "homepage" : "https://github.com/trevnorris/cbuffer", | ||
}, | ||
"license": "MIT", | ||
"licenses" : [{ | ||
@@ -17,2 +18,9 @@ "type" : "MIT", | ||
}], | ||
"keywords": [ | ||
"circular buffer", | ||
"ring", | ||
"circular", | ||
"buffer", | ||
"data structure" | ||
], | ||
"devDependencies" : { | ||
@@ -22,3 +30,3 @@ "vows" : "latest" | ||
"scripts" : { | ||
"test" : "node ./node_modules/.bin/vows" | ||
"test" : "vows" | ||
}, | ||
@@ -25,0 +33,0 @@ "repository" : { |
@@ -1,5 +0,10 @@ | ||
## JavaScript [Circular Buffer](http://en.wikipedia.org/wiki/Circular_buffer) Utility | ||
## CBuffer: JavaScript [Circular Buffer](http://en.wikipedia.org/wiki/Circular_buffer) Utility | ||
[![npm](https://img.shields.io/npm/v/CBuffer.svg)](https://www.npmjs.com/package/CBuffer) | ||
[![Build Status](https://travis-ci.org/trevnorris/cbuffer.svg?branch=master)](https://travis-ci.org/trevnorris/cbuffer) | ||
[![npm license](https://img.shields.io/npm/l/CBuffer.svg)](./LICENSE) | ||
The end goal of this project is to implement the entire JavaScript `Array.prototype`, and some | ||
additional utility methods, as a circular buffer. | ||
additional utility methods, as a **circular buffer**, a **ring buffer** structure. | ||
@@ -42,33 +47,33 @@ Note: This is called a circular buffer because of what this library accomplishes, but is implemented | ||
* 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 length. | ||
* reverse - Reverses the order of the elements of a circular buffer. | ||
* rotateLeft - Rotates all elements left 1, or n, times. | ||
* rotateRight - Rotates all elements right 1, or n, times. | ||
* shift - Removes the first element from a circular buffer and returns that element. | ||
* 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 length. | ||
* `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 length. | ||
* `reverse` - Reverses the order of the elements of a circular buffer. | ||
* `rotateLeft` - Rotates all elements left 1, or n, times. | ||
* `rotateRight` - Rotates all elements right 1, or n, times. | ||
* `shift` - Removes the first element from a circular buffer and returns that element. | ||
* `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 length. | ||
#### Accessor Methods | ||
* indexOf - Returns the first (least) index of an element within the circular buffer equal to the specified value, or -1 if none is found. | ||
* lastIndexOf - Returns the last (greatest) index of an element within the circular buffer equal to the specified value, or -1 if none is found. | ||
* sortedIndex - Returns the position some `value` would be inserted into a sorted circular buffer ranked by an optional comparitor. | ||
* `indexOf` - Returns the first (least) index of an element within the circular buffer equal to the specified value, or -1 if none is found. | ||
* `lastIndexOf` - Returns the last (greatest) index of an element within the circular buffer equal to the specified value, or -1 if none is found. | ||
* `sortedIndex` - Returns the position some `value` would be inserted into a sorted circular buffer ranked by an optional comparitor. | ||
#### Iteration Methods | ||
* every - Returns true if every element in the circular buffer satisfies the provided testing function. | ||
* forEach - Calls a function for each element in the circular buffer. | ||
* some - Returns true if at least one element in the circular buffer satisfies the provided testing function. | ||
* `every` - Returns true if every element in the circular buffer satisfies the provided testing function. | ||
* `forEach` - Calls a function for each element in the circular buffer. | ||
* `some` - Returns true if at least one element in the circular buffer satisfies the provided testing function. | ||
#### Utility Methods | ||
* empty - Equivalent to setting `Array.length = 0`. | ||
* fill - Fill with passed argument. Also supports functions. | ||
* first - Returns first value in circular buffer. | ||
* last - Returns last value in circular buffer. | ||
* get - Get value at specific index. | ||
* set - Set value as specific index. | ||
* toArray - Return clean ordered array of buffer. | ||
* overflow - Set to function and will be called when data is about to be overwritten. | ||
* slice - Return a slice of the buffer as an array. | ||
* `empty` - Equivalent to setting `Array.length = 0`. | ||
* `fill` - Fill with passed argument. Also supports functions. | ||
* `first` - Returns first value in circular buffer. | ||
* `last` - Returns last value in circular buffer. | ||
* `get` - Get value at specific index. | ||
* `set` - Set value as specific index. | ||
* `toArray` - Return clean ordered array of buffer. | ||
* `overflow` - Set to function and will be called when data is about to be overwritten. | ||
* `slice` - Return a slice of the buffer as an array. |
36845
36
1147
79