1
0
Fork 0
forked from 0x2620/oxjs

add tests

This commit is contained in:
rolux 2012-05-25 16:25:41 +02:00
commit 9d4128c787

View file

@ -1,212 +1,237 @@
'use strict'; 'use strict';
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every Ox.fallback = {};
if (!Array.prototype.every) {
Array.prototype.every = function(fn, that) { /*@
if (this === void 0 || this === null || typeof fn !== 'function') { Ox.fallback.every <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
> Ox.fallback.every.call([0, 1, 2], function(v, i) { return v == i; })
true
> Ox.fallback.every.call([true, true, false], Ox.identity)
false
@*/
Ox.fallback.every = function(iterator, that) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length >>> 0, ret = true;
i, for (i = 0; i < length; i++) {
len = arr.length >>> 0, if (i in array && !iterator.call(that, array[i], i, array)) {
ret = true;
for (i = 0; i < len; i++) {
if (i in arr && !fn.call(that, arr[i], i, arr)) {
ret = false; ret = false;
break; break;
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter /*@
if (!Array.prototype.filter) { Ox.fallback.filter <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
Array.prototype.filter = function(fn, that) { > Ox.fallback.filter.call([2, 1, 0], function(v, i) { return v == i; })
if (this === void 0 || this === null || typeof fn !== 'function') { [1]
@*/
Ox.fallback.filter = function(iterator, that) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length >>> 0, ret = [], value;
i, for (i = 0; i < length; i++) {
len = arr.length >>> 0, // save value in case iterator mutates it
ret = [], if (i in array && iterator.call(that, value = array[i], i, array)) {
val; ret.push(value);
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; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach /*@
if (!Array.prototype.forEach) { Ox.fallback.forEach <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
Array.prototype.forEach = function(fn, that) { <script>
if (this === void 0 || this === null || typeof fn !== 'function') { Ox.test.array = [];
Ox.fallback.forEach.call([1, 2, 3], function(v, i) { Ox.test.array.push([v, i])})
</script>
> Ox.test.array
[[1, 0], [2, 1], [3, 2]]
@*/
Ox.fallback.forEach = function(iterator, that) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length >>> 0;
i, for (i = 0; i < length; i++) {
len = arr.length >>> 0; if (i in array) {
for (i = 0; i < len; i++) { iterator.call(that, array[i], i, array);
if (i in arr) {
fn.call(that, arr[i], i, arr);
} }
} }
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf /*@
if (!Array.prototype.indexOf) { Ox.fallback.indexOf <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
Array.prototype.indexOf = function(val) { > Ox.fallback.indexOf.call([1, 2, 3, 2, 1], 2)
1
> Ox.fallback.indexOf.call([1, 2, 3, 2, 1], 4)
-1
@*/
Ox.fallback.indexOf = function(value) {
if (this === void 0 || this === null) { if (this === void 0 || this === null) {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length >>> 0, ret = -1;
i, for (i = 0; i < length; i++) {
len = arr.length >>> 0, if (i in array && array[i] === value) {
ret = -1; ret = i;
for (i = 0; i < len; i++) {
if (i in arr && arr[i] === val) {
ret = val;
break; break;
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf /*@
if (!Array.prototype.lastIndexOf) { Ox.fallback.lastIndexOf <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
Array.prototype.lastIndexOf = function(val) { > Ox.fallback.lastIndexOf.call([1, 2, 3, 2, 1], 2)
3
> Ox.fallback.lastIndexOf.call([1, 2, 3, 2, 1], 4)
-1
@*/
Ox.fallback.lastIndexOf = function(value) {
if (this === void 0 || this === null) { if (this === void 0 || this === null) {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length >>> 0, ret = -1;
i, for (i = length - 1; i >= 0; i--) {
len = arr.length >>> 0, if (i in array && array[i] === value) {
ret = -1; ret = i;
for (i = len - 1; i >= 0; i--) {
if (i in arr && arr[i] === val) {
ret = val;
break; break;
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map /*@
if (!Array.prototype.map) { Ox.fallback.map <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
Array.prototype.map = function(fn, that) { > Ox.fallback.map.call([2, 1, 0], function(v, i) { return v == i; })
if (this === void 0 || this === null || typeof fn !== 'function') { [false, true, false]
@*/
Ox.fallback.map = function(iterator, that) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length >>> 0,
i, ret = new Array(length);
len = arr.length >>> 0, for (i = 0; i < length; i++) {
ret = new Array(len); if (i in array) {
for (i = 0; i < len; i++) { ret[i] = iterator.call(that, array[i], i, array);
if (i in arr) {
ret[i] == fn.call(that, arr[i], i, arr);
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some /*@
if (!Array.prototype.some) { Ox.fallback.reduce <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
Array.prototype.some = function(fn, that) { > Ox.fallback.reduce.call([1, 2, 3], function(p, c, i) { return p + c + i; }, 1)
if (this === void 0 || this === null || typeof fn !== 'function') { 10
@*/
Ox.fallback.reduce = function(iterator, ret) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length;
i, if (!length && ret === void 0) {
len = arr.length >>> 0,
ret = false;
for (i = 0; i < len; i++) {
if (i in arr && fn.call(that, arr[i], i, arr)) {
ret = true;
break;
}
}
return ret;
};
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(fn, ret) {
if (this === void 0 || this === null || typeof fn !== 'function') {
throw new TypeError();
}
var arr = Object(this),
i,
len = arr.length;
if (!len && ret === void 0) {
throw new TypeError(); throw new TypeError();
} }
if (ret === void 0) { if (ret === void 0) {
ret = arr[0]; ret = array[0];
i = 1; i = 1;
} }
for (i = i || 0; i < len; i++) { for (i = i || 0; i < length; i++) {
if (i in arr) { if (i in array) {
ret = fn.call(void 0, ret, arr[i], i, arr); ret = iterator.call(void 0, ret, array[i], i, array);
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduceRight /*@
if (!Array.prototype.reduceRight) { Ox.fallback.reduceRight <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduceRight
Array.prototype.reduceRight = function(fn, ret) { > Ox.fallback.reduceRight.call([1, 2, 3], function(p, c, i) { return p + c + i; }, 1)
if (this === void 0 || this === null || typeof fn !== 'function') { 10
@*/
Ox.fallback.reduceRight = function(iterator, ret) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var arr = Object(this), var array = Object(this), i, length = array.length;
i, if (!length && ret === void 0) {
len = arr.length;
if (!len && ret === void 0) {
throw new TypeError(); throw new TypeError();
} }
if (ret === void 0) { if (ret === void 0) {
ret = arr[len - 1]; ret = array[length - 1];
i = len - 2; i = length - 2;
} }
for (i = i || len - 1; i >= 0; i--) { for (i = i || length - 1; i >= 0; i--) {
if (i in arr) { if (i in array) {
ret = fn.call(void 0, ret, arr[i], i, arr); ret = iterator.call(void 0, ret, array[i], i, array);
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys /*@
if (!Object.keys) { Ox.fallback.some <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
Object.keys = function(obj) { > Ox.fallback.some.call([2, 1, 0], function(v, i) { return v == i; })
if (obj !== Object(obj)) { true
> Ox.fallback.some.call([false, false, false], Ox.identity)
false
@*/
Ox.fallback.some = function(iterator, that) {
if (this === void 0 || this === null || typeof iterator !== 'function') {
throw new TypeError(); throw new TypeError();
} }
var key, var array = Object(this), i, length = array.length >>> 0, ret = false;
ret = []; for (i = 0; i < length; i++) {
for (key in obj) { if (i in array && iterator.call(that, array[i], i, array)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) { ret = true;
break;
}
}
return ret;
};
/*@
Ox.fallback.keys <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
> Ox.fallback.keys({a: 1, b: 2, c: 3})
['a', 'b', 'c']
@*/
Ox.fallback.keys = function(object) {
if (object !== Object(object)) {
throw new TypeError();
}
var key, ret = [];
for (key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
ret.push(key); ret.push(key);
} }
} }
return ret; return ret;
}; };
}
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim /*@
if (!String.prototype.trim) { Ox.fallback.trim <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim
String.prototype.trim = function() { > Ox.fallback.trim.call(' foo ')
'foo'
@*/
Ox.fallback.trim = function() {
return this.replace(/^\s+|\s+$/g, ''); return this.replace(/^\s+|\s+$/g, '');
}; };
}
(function() {
var method, object;
for (method in Ox.fallback) {
object = method == 'keys' ? Object
: method == 'trim' ? String.prototype
: Array.prototype;
for (method in Ox.fallback[object]) {
if (!object[method]) {
object[method] = Ox.fallback[method];
}
}
}
})();