Skip to content

Instantly share code, notes, and snippets.

@thedustin
Created January 30, 2014 15:13
Show Gist options
  • Save thedustin/8710651 to your computer and use it in GitHub Desktop.
Save thedustin/8710651 to your computer and use it in GitHub Desktop.
A simple List-Class
/**
* List
*
* @author Dustin Breuer <dustin.breuer@thedust.in>
* @version 1.0
* @category Util
* @licence MIT http://opensource.org/licenses/MIT
*/
/**
*
* @constructor
*/
function List() {
/**
* An Array that contains all values
* @type {Object[]}
* @private
*/
this._aList = [];
}
/**
* Add Object <i>oObject</i> to this List
*
* @param {Object} oObject
*
* @returns {List}
*/
List.prototype.add = function (oObject) {
this._aList.push(oObject);
return this;
};
/**
* Remove Object <i>oObject</i> from this List
*
* @param {Object} oObject
*
* @returns {List}
*/
List.prototype.remove = function (oObject) {
var _iObjectIndex = this.index(oObject);
if (_iObjectIndex !== -1) {
this._aList = this._aList.splice(_iObjectIndex, 1);
}
return this;
};
/**
* Check if this List contains Object <i>oObject</i>
*
* @param {Object} oObject
*
* @returns {Boolean}
*/
List.prototype.has = function (oObject) {
return (this.index(oObject) !== -1);
};
/**
* Returns the length of this List
*
* @returns {Number}
*/
List.prototype.size = function () {
return this._aList.length;
};
/**
* Returns all listed ports
*
* @returns {Object[]}
*/
List.prototype.getArray = function () {
return this._aList;
};
/**
* Add Object <i>oObject</i> to this List
*
* @param {Object} oObject
*
* @returns {Number}
*/
List.prototype.index = function (oObject) {
return this._aList.indexOf(oObject);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment