Created
August 16, 2011 20:38
-
-
Save samshull/1150110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!('filter' in Array.prototype)) { | |
// conforms with the ECMA standard | |
Array.prototype.filter = function filter(fn, thisArg) { | |
var index = 0, length = this.length, value, returnValue = [], that = Object(this); | |
for (; index < length; ++index) { | |
if (index in that) { | |
value = that[index]; | |
if (fn.call(thisArg, value, index, that)) { | |
returnValue.push(value); | |
} | |
} | |
} | |
return returnValue; | |
}; | |
} | |
if (!('forEach' in Array.prototype)) { | |
Array.prototype.forEach = function forEach(fn, thisArg) { | |
var index = 0, length = this.length, that = Object(this), value; | |
for (; index < length; ++index) { | |
if (index in that) { | |
value = that[index]; | |
fn.call(thisArg, value, index, that); | |
} | |
} | |
}; | |
} | |
if (!('map' in Array.prototype)) { | |
Array.prototype.map = function map(fn, thisArg) { | |
var newArray = []; | |
this.forEach(function() { | |
newArray.push(fn.apply(this, arguments)); | |
}, thisArg); | |
return newArray; | |
}; | |
} | |
if (!('indexOf' in Array.prototype)) { | |
Array.prototype.indexOf = function indexOf(needle, index) { | |
index = Number(index); | |
var length = this.length, that = Object(this); | |
for (; index < length; ++index) { | |
if (index in that && that[index] === needle) { | |
return index; | |
} | |
} | |
return -1; | |
}; | |
} | |
if (!('lastIndexOf' in Array.prototype)) { | |
Array.prototype.lastIndexOf = function lastIndexOf(needle, index) { | |
index = Number(index); | |
var length = this.length, that = Object(this); | |
for (; length-- >= index;) { | |
if (length in that && that[length] === needle) { | |
return index; | |
} | |
} | |
return -1; | |
}; | |
} | |
if (!('reduce' in Array.prototype)) { | |
Array.prototype.reduce = function reduce(fn) { | |
var index, value, length = this.length, that = Object(this); | |
if (arguments.length > 1) { | |
index = 0; | |
value = arguments[1]; | |
} else { | |
index = 1; | |
value = this[0]; | |
} | |
for (; index < length; ++index) { | |
if (index in that) { | |
value = fn.call(this, value, that[index], index, this); | |
} | |
} | |
return value; | |
}; | |
} | |
Array.prototype.count = function count(arg, thisArg) { | |
if (typeof arg === "undefined") { | |
return this.length; | |
} | |
if (arg.call) { | |
return this.reduce(function(previous, current) { | |
return previous + Number(!!arg.call(this, current)); | |
}, 0); | |
} | |
return this.reduce(function(previous, current) { | |
return previous + Number(current === arg); | |
}, 0); | |
}; | |
Array.prototype.unique = function unique() { | |
var lastValue, result; | |
return this.sort().filter(function(value) { | |
result = value !== lastValue; | |
lastValue = result; | |
return result; | |
}); | |
}; | |
Array.prototype.compact = function compact() { | |
var undefined; | |
return this.filter(function(value) { | |
return value !== null && value !== undefined; | |
}); | |
}; | |
Array.prototype.reject = function reject(fn, thisArg) { | |
return this.filter(function() { | |
return !fn.apply(this, arguments); | |
}, thisArg); | |
}; | |
Array.prototype.last = function last() { | |
return this[this.length - 1]; | |
}; | |
Array.prototype.select = Array.prototype.filter; | |
Array.prototype.append = Array.prototype.push; | |
Array.prototype.prepend = Array.prototype.unshift; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment