Created
February 23, 2010 14:17
-
-
Save olleolleolle/312190 to your computer and use it in GitHub Desktop.
adds ECMA-262 methods to Array prototype
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
/** | |
* Adds ECMA-262 rev. 5 methods to Array prototype, if they are missing | |
* Please note that the global prototypes are updated, so only use this file | |
* if that is acceptable, or if you're running in an iframe | |
* | |
* Please not that this file WILL NOT BE COPYRIGHTED, as it merely wraps mozilla implementation | |
* of JavaScript 1.6 / ECMA-262 rev. 5 methods | |
* | |
* @author Morgan Roderick - [email protected] | |
*/ | |
/*jslint evil: false, strict: false, undef: true, white: false, onevar:false, browser:true, plusplus:false, bitwise:false */ | |
(function(){ | |
"use strict"; | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter | |
if (!Array.prototype.filter){ | |
Array.prototype.filter = function(fun /*, thisp*/){ | |
var len = this.length >>> 0; | |
if (typeof fun !== "function"){ | |
throw new TypeError(); | |
} | |
var res = []; | |
var thisp = arguments[1]; | |
for (var i = 0; i < len; i++){ | |
if (i in this){ | |
var val = this[i]; // in case fun mutates this | |
if (fun.call(thisp, val, i, this)){ | |
res.push(val); | |
} | |
} | |
} | |
return res; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf | |
if (!Array.prototype.indexOf){ | |
Array.prototype.indexOf = function( elt ){ | |
var len = this.length >>> 0; | |
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; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/lastIndexOf | |
if (!Array.prototype.lastIndexOf){ | |
Array.prototype.lastIndexOf = function(elt /*, from*/){ | |
var len = this.length; | |
var from = Number(arguments[1]); | |
if (isNaN(from)){ | |
from = len - 1; | |
} else { | |
from = (from < 0) ? Math.ceil(from) : Math.floor(from); | |
if (from < 0){ | |
from += len; | |
} else if (from >= len){ | |
from = len - 1; | |
} | |
} | |
for (; from > -1; from--){ | |
if (from in this && this[from] === elt){ | |
return from; | |
} | |
} | |
return -1; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every | |
if (!Array.prototype.every) { | |
Array.prototype.every = function(fun /*, thisp*/){ | |
var len = this.length >>> 0; | |
if (typeof fun !== "function"){ | |
throw new TypeError(); | |
} | |
var thisp = arguments[1]; | |
for (var i = 0; i < len; i++){ | |
if (i in this && !fun.call(thisp, this[i], i, this)){ | |
return false; | |
} | |
} | |
return true; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach | |
if (!Array.prototype.forEach){ | |
Array.prototype.forEach = function(fun /*, thisp*/) { | |
var len = this.length >>> 0; | |
if ( typeof fun !== "function" ){ | |
throw new TypeError(); | |
} | |
var thisp = arguments[1]; | |
for (var i = 0; i < len; i++){ | |
if (i in this){ | |
fun.call(thisp, this[i], i, this); | |
} | |
} | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/map | |
if (!Array.prototype.map){ | |
Array.prototype.map = function(fun /*, thisp*/){ | |
var len = this.length >>> 0; | |
if (typeof fun !== "function"){ | |
throw new TypeError(); | |
} | |
var res = new Array(len); | |
var thisp = arguments[1]; | |
for (var i = 0; i < len; i++){ | |
if (i in this){ | |
res[i] = fun.call(thisp, this[i], i, this); | |
} | |
} | |
return res; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/some | |
if (!Array.prototype.some){ | |
Array.prototype.some = function(fun /*, thisp*/){ | |
var i = 0, | |
len = this.length >>> 0; | |
if (typeof fun !== "function"){ | |
throw new TypeError(); | |
} | |
var thisp = arguments[1]; | |
for (; i < len; i++){ | |
if (i in this && fun.call(thisp, this[i], i, this)){ | |
return true; | |
} | |
} | |
return false; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce | |
if (!Array.prototype.reduce){ | |
Array.prototype.reduce = function(fun /*, initial*/){ | |
var len = this.length >>> 0; | |
if (typeof fun !== "function"){ | |
throw new TypeError(); | |
} | |
// no value to return if no initial value and an empty array | |
if (len === 0 && arguments.length === 1){ | |
throw new TypeError(); | |
} | |
var i = 0; | |
var rv; | |
if (arguments.length >= 2){ | |
rv = arguments[1]; | |
} else { | |
do { | |
if (i in this) { | |
rv = this[i++]; | |
break; | |
} | |
// if array contains no values, no initial value to return | |
if (++i >= len){ | |
throw new TypeError(); | |
} | |
} | |
while (true); | |
} | |
for (; i < len; i++){ | |
if (i in this){ | |
rv = fun.call(null, rv, this[i], i, this); | |
} | |
} | |
return rv; | |
}; | |
} | |
// Mozilla's ECMA-262 rev. 5 compliant implementation | |
// see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduceRight | |
if (!Array.prototype.reduceRight){ | |
Array.prototype.reduceRight = function(fun /*, initial*/){ | |
var len = this.length >>> 0; | |
if (typeof fun !== "function"){ | |
throw new TypeError(); | |
} | |
// no value to return if no initial value, empty array | |
if (len === 0 && arguments.length === 1){ | |
throw new TypeError(); | |
} | |
var i = len - 1; | |
var rv; | |
if (arguments.length >= 2){ | |
rv = arguments[1]; | |
} else { | |
do { | |
if (i in this) { | |
rv = this[i--]; | |
break; | |
} | |
// if array contains no values, no initial value to return | |
if (--i < 0){ | |
throw new TypeError(); | |
} | |
} | |
while (true); | |
} | |
for (; i >= 0; i--) { | |
if (i in this){ | |
rv = fun.call(null, rv, this[i], i, this); | |
} | |
} | |
return rv; | |
}; | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment