Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

CBuffer

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

CBuffer - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

.npmignore

137

cbuffer.js

@@ -0,2 +1,5 @@

(function( global ) {
function CBuffer() {
var i = 0;
// handle cases where "new" keyword wasn't used

@@ -19,2 +22,6 @@ if (!( this instanceof CBuffer )) {

this.data = new Array( arguments[0] );
// force preallocation of memory to each array slot, for quicker operation on buffer
for ( ; i < arguments[0]; i++ ) {
this.data[i] = undefined;
}
this.end = ( this.length = arguments[0] ) - 1;

@@ -26,2 +33,16 @@ }

CBuffer.prototype = {
// properly set constructor
constructor : CBuffer,
/* mutator methods */
// pop last item
pop : function() {
var item;
if ( this.size === 0 ) return;
item = this.data[ this.end ];
delete this.data[( this.size + this.start - 1 ) % this.length ];
this.size--;
this.end = ( this.end - 1 + this.length ) % this.length;
return item;
},
// push item to the end

@@ -43,12 +64,22 @@ push : function() {

// recalculate start
this.start = this.end - this.size + 1;
if ( this.start < 0 ) this.start += this.length;
this.start = ( this.length + this.end - this.size + 1 ) % this.length;
// return number current number of items in CBuffer
return this.size;
},
// shift first item
// reverse order of the buffer
reverse : function() {
var i = 0,
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;
}
return this;
},
// remove and return first item
shift : function() {
var item;
// check if there are any items in CBuff
if ( this.size === 0 ) return undefined;
if ( this.size === 0 ) return;
// store first item for return

@@ -64,2 +95,64 @@ item = this.data[ this.start ];

},
// sort items
sort : function( fn ) {
if ( fn ) this.data.sort( fn );
else this.data.sort();
this.start = 0;
this.end = this.size - 1;
return this;
},
// add item to beginning of buffer
unshift : function() {
var i = 0;
for ( ; i < arguments.length; i++ ) {
this.data[( this.length + this.start - ( i % this.length ) - 1 ) % this.length ] = 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 ) {
if ( this.size + i > this.length ) this.size = this.length;
else this.size += i;
}
this.start -= arguments.length;
if ( this.start < 0 ) this.start = this.length + ( this.start % this.length );
return this.size;
},
/* accessor methods */
// return index of first matched element
indexOf : function( arg, idx ) {
if ( !idx ) idx = 0;
for ( ; idx < this.size; idx++ ) {
if ( this.data[( this.start + idx ) % this.length ] === arg ) return idx;
}
return -1;
},
// return last index of the first match
lastIndexOf : function( arg, idx ) {
if ( !idx ) idx = this.size - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.data[( this.start + idx ) % this.length ] === arg ) return idx;
}
return -1;
},
/* iteration methods */
// check every item in the array against a test
every : function( callback, context ) {
var i = 0;
if ( context ) {
for ( ; i < this.size; i++ ) {
if ( !callback.call( context, this.data[( this.start + i ) % this.length ], i, this ))
return false;
}
} else {
for ( ; i < this.size; i++ ) {
if ( !callback( this.data[( this.start + i ) % this.length ], i, this ))
return false;
}
}
return true;
},
// loop through each item in buffer

@@ -70,11 +163,29 @@ forEach : function( callback, context ) {

if ( context ) {
for (; i < this.size; i++ ) {
callback.call( context, this.idx( i ), i, this );
for ( ; i < this.size; i++ ) {
callback.call( context, this.data[( this.start + i ) % this.length ], i, this );
}
} else {
for (; i < this.size; i++ ) {
callback( this.idx( i ), i, this );
for ( ; i < this.size; i++ ) {
callback( this.data[( this.start + i ) % this.length ], i, this );
}
}
},
// check items agains test until one returns true
some : function( callback, context ) {
var i = 0;
if ( context ) {
for ( ; i < this.size; i++ ) {
if ( callback.call( context, this.data[( this.start + i ) % this.length ], i, this ))
return true;
}
} else {
for ( ; i < this.size; i++ ) {
if ( callback( this.data[( this.start + i ) % this.length ], i, this ))
return true;
}
}
return false;
},
/* utility methods */
// return first item in buffer

@@ -89,7 +200,13 @@ first : function() {

// return specific index in buffer
idx : function( arg ) {
get : function( arg ) {
return this.data[( this.start + arg ) % this.length ];
},
// set value at specified index
set : function( idx, arg ) {
return this.data[( this.start + idx ) % this.length ] = arg;
}
};
this.CBuffer = CBuffer;
global.CBuffer = CBuffer;
}( this ));

5

package.json
{
"name" : "CBuffer",
"version" : "0.0.5",
"version" : "0.0.6",
"description" : "Circular Buffer JavaScript implementation",

@@ -16,2 +16,5 @@ "homepage" : "http://github.com/trevnorris/cbuffer",

}],
"devDependencies" : {
"vows" : "0.6.x"
},
"repository" : {

@@ -18,0 +21,0 @@ "type" : "git",

@@ -1,12 +0,44 @@

```
_____ ______ __ __
/ __ \| ___ \ / _|/ _|
| / \/| |_/ /_ _| |_| |_ ___ _ __
| | | ___ \ | | | _| _/ _ \ '__|
| \__/\| |_/ / |_| | | | || __/ |
\____/\____/ \__,_|_| |_| \___|_|
```
## JavaScript [Circular Buffer](http://en.wikipedia.org/wiki/Circular_buffer) Utility
JavaScript Circular Buffer Utility
The end goal of this project is to implement the entire JavaScript `Array.prototype`, and some
additional utility methods, as a circular buffer.
For more information read [my blog post](http://blog.trevorjnorris.com/2012/03/javascript-circular-buffers/)
As JavaScript is used for more and more computationally intensive tasks, it's important to reduce
execution and garbage collection time. By using a circular buffer instead of a native array less
extraneous data bits are generated that will need to be cleaned up later. So use of this library is
two fold: First, if you need a circular buffer for the classical reasons (animation queue, video
streaming) this library can help you out. Second, if you know the maximum size of an array you'll be
working with, using CBuffer can drastically reduce the memory footprint of your application.
While the entire `Array.prototype` API is on the roadmap, it's not all quite here. Below is the
currently implemented API.
### API
#### Mutator Methods
* pop()
* push()
* reverse()
* shift()
* sort()
* unshift()
#### Accessor Methods
* indexOf()
* lastIndexOf()
#### Iteration Methods
* every()
* forEach()
* some()
#### Utility Methods
* first()
* last()
* get()
* set()
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc