// --- Methods below provided by the Mozilla Foundation --- // // -------------------------------------------------------- // // Array.filter() // -------------------------- if (!Array.prototype.filter) { Array.prototype.filter = function(fun) { var len = this.length; if (typeof fun != "function") { throw new TypeError(); } var res = new Array(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; if (fun.call(thisp, val, i, this)) { res.push(val); } } } return res; }; } // Array.indexOf() // -------------------------- if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt) { var len = this.length; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) { from += len; } for (; from < len; from++) { if (from in this && this[from] === elt) { return from; } } return -1; }; } // --- CUSTOM METHODS --- // // ---------------------- // // Array.found() // -------------------------- if (!Array.prototype.found) { Array.prototype.found = function(v) { var r = false; var len = this.length; var s = 0; if (typeof v == "boolean" || typeof v == "number" || typeof v == "string") { for (; s < len; s++) { if (s in this && this[s] === v) { r = true; } } } return r; }; } // Array.slap() // - removes 1st item from the array, if any items exist // -------------------------- if (!Array.prototype.slap) { Array.prototype.slap = function() { var len = this.length; var na = []; var first = 0; var slapped; var s = 0; var n = 0; var r = 0; if (arguments[0]) { if (typeof arguments[0] == "number") { r = (arguments[0] == 1 ? 1 : 0); } } if (len > 1) { for (; s < len; s++) { if (first == 0) { slapped = this[s]; first++; } else { na[n] = this[s]; this[n] = this[s]; n++; } } } this.length = na.length; if (r == 1) { return slapped; } }; } // Array.punch() // - removes an item from the array, if it exists (1st occurence) // - indicating '1' for the 2nd argument will remove all instances // -------------------------- if (!Array.prototype.punch) { Array.prototype.punch = function(v) { var len = this.length; var s = 0; var n = 0; var na = []; var remove_all = Number(arguments[1]) || 0; if (remove_all !== 1 && remove_all !== 0) { remove_all = 0; } if (this.found(v) === true) { for (; s < len; s++) { if (s in this && this[s] !== v) { na[n] = this[s]; this[n] = this[s]; n++; } } this.length = na.length; if (remove_all == 1 && this.found(v) === true) { this.punch(v,1); } } }; } // Array.push_unique() // - adds an item to the array, if it is unique // -------------------------- if (!Array.prototype.push_unique) { Array.prototype.push_unique = function(v) { if (this.found(v) === false) { this.push(v); } }; } // Array.implode() // -------------------------- if (!Array.prototype.implode) { Array.prototype.implode = function() { var len = this.length; var r = ""; var s = 0; var d = arguments[0]; if (typeof d != "string" && typeof d != "number") { d = ""; } if (len > 0) { for (; s < len; s++) { if (s in this && this[s] !== "") { r += this[s]+(s < (len-1) ? d : ""); } } } return r; }; }