oxjs/source/Ox/js/Fallback.js

148 lines
4.2 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
if (!Array.prototype.filter) {
Array.prototype.filter = function(fn, that) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length >>> 0,
ret = [],
val;
for (i = 0; i < len; i++) {
// save val in case fn mutates it
if (i in arr && fn.call(that, val = arr[i], i, arr)) {
ret.push(val);
}
}
return ret;
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fn, that) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length >>> 0;
for (i = 0; i < len; i++) {
if (i in arr) {
fn.call(that, arr[i], i, arr);
}
}
2012-01-07 07:20:02 +00:00
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(val) {
if (this === void 0 || this === null) {
throw new TypeError();
}
var arr = Object(this),
i,
2011-10-31 14:46:07 +00:00
len = arr.length >>> 0,
ret = -1;
for (i = 0; i < len; i++) {
if (i in arr && arr[i] === val) {
ret = val;
2012-05-25 12:49:47 +00:00
break;
}
}
return ret;
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf = function(val) {
if (this === void 0 || this === null) {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length >>> 0,
ret = -1;
for (i = len - 1; i >= 0; i--) {
if (i in arr && arr[i] === val) {
ret = val;
break;
}
}
return ret;
2012-01-07 07:20:02 +00:00
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
if (!Array.prototype.map) {
Array.prototype.map = function(fn, that) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length >>> 0,
ret = new Array(len);
for (i = 0; i < len; i++) {
if (i in arr) {
ret[i] == fn.call(that, arr[i], i, arr);
}
}
return ret;
2012-01-07 07:20:02 +00:00
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
if (!Array.prototype.reduce) {
Array.prototype.reduce = function reduce(fn, ret) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = this.length;
if (!len && ret === void 0) {
throw new TypeError();
}
if (ret === void 0) {
ret = arr[0];
i = 1;
}
for (i = i || 0; i < len ; ++i) {
if (i in arr) {
ret = fn.call(void 0, ret, arr[i], i, arr);
}
}
return ret;
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
Object.keys = function(obj) {
if (obj !== Object(obj)) {
throw new TypeError();
}
var key,
ret = [];
for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
ret.push(key);
}
}
return ret;
2012-01-07 07:20:02 +00:00
};
2011-10-31 14:46:07 +00:00
}
2012-05-25 12:16:51 +00:00
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}