A fixed-length, first-in-first-out array for Javascript.
Install
npm install fifo-array
-or-
npm install ben-bradley/fifo-array
Methods
All FifoArray
methods behave exactly like the Array.prototype
methods, but they trim the array down to have a length
equal to the max
. Which end of the array that is trimmed is the difference:
- FifoArray.push() - Trims from the front of the array until
length === max
. - FifoArray.unshift() - Trims from the back of the array until
length === max
. - FifoArray.splice() - Trims from the back of the array until
length === max
.
All other Array.prototype
methods are untouched and will behave as expected. I couldn't find any other methods that could add elements to the FifoArray
Examples
Basic usage
var FifoArray = ; var fifoArray = 3 'a' 'b' 'c' 'd' ;console; // => [ 'b', 'c', 'd' ]console; // => max: 3fifoArray;console; // => [ 'd', 1, 2 ]
#push()
var fifoArray = 3;fifoArray;console; // => [ 3, 4, 5 ]
#unshift()
var fifoArray = 3 0 1 2 ;fifoArray;console; // => [ 'a', 'b', 0 ]
#splice()
var fifoArray = 3 0 1 2 ;fifoArray; // at position 1, remove 2 elements and add 'a'console; // => [ 0, 'a' ] fifoArray; // at posotion 1, remove 0 elements and add 'b'console; // => [ 0, 'b', 'a' ]
.max
// if necessary, it chops the array down to the new maxvar fifoArray = 5 0 1 2 3 4 ;console; // => [ 0, 1, 2, 3, 4 ]fifoArraymax = 3;console; // => new max: 3console; // => [ 0, 1, 2 ] fifoArraymax = 10;fifoArray;console; // => [ 1, 2, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]
Version History
- 0.1.2 - Added readme.md bling
- 0.1.1 - Initial release
- 0.0.* - Internal development
References
Here are several sources that helped me write this module: