Skip to content

Instantly share code, notes, and snippets.

@dansimpson
Created November 11, 2009 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansimpson/231548 to your computer and use it in GitHub Desktop.
Save dansimpson/231548 to your computer and use it in GitHub Desktop.
Array.prototype.each = function(fn, scope) {
var count = 0;
while(count < this.length) {
fn.call(scope || window, this[count], count)
count++;
}
};
Array.prototype.search = function(fn) {
var result = [];
var count = 0;
while(count < this.length) {
if(fn(this[count])) {
result.push(this[count]);
}
count++;
}
return result;
};
Array.prototype.collect = function(fn, scope) {
var result = [];
var count = 0;
while(count < this.length) {
result.push(fn.call(scope || window, this[count]));
count++;
}
return result;
};
Array.prototype.aggregate = function(fn) {
var result = 0;
var count = 0;
while(count < this.length) {
result += fn(this[count]);
count++;
}
return result;
};
Array.prototype.contains = function(item) {
return this.indexOf(item) != -1;
};
Array.prototype.first = function() {
return this[0]
};
Array.prototype.last = function() {
return this[this.length - 1]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment