From 9d4128c787fffd5cf6a75a3b90f8791a0a926206 Mon Sep 17 00:00:00 2001 From: rolux Date: Fri, 25 May 2012 16:25:41 +0200 Subject: [PATCH] add tests --- source/Ox/js/Fallback.js | 437 +++++++++++++++++++++------------------ 1 file changed, 231 insertions(+), 206 deletions(-) diff --git a/source/Ox/js/Fallback.js b/source/Ox/js/Fallback.js index 1db44e28..489b3494 100644 --- a/source/Ox/js/Fallback.js +++ b/source/Ox/js/Fallback.js @@ -1,212 +1,237 @@ 'use strict'; -// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every -if (!Array.prototype.every) { - Array.prototype.every = 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 = true; - for (i = 0; i < len; i++) { - if (i in arr && !fn.call(that, arr[i], i, arr)) { - ret = false; - break; - } - } - return ret; - }; -} +Ox.fallback = {}; -// 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); - } - } - }; -} - -// 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, - len = arr.length >>> 0, - ret = -1; - for (i = 0; i < len; i++) { - if (i in arr && arr[i] === val) { - ret = val; - 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; - }; -} - -// 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; - }; -} - -// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some -if (!Array.prototype.some) { - Array.prototype.some = function(fn, that) { - if (this === void 0 || this === null || typeof fn !== 'function') { - throw new TypeError(); - } - var arr = Object(this), - i, - len = arr.length >>> 0, +/*@ +Ox.fallback.every 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(); + } + var array = Object(this), i, length = array.length >>> 0, ret = true; + for (i = 0; i < length; i++) { + if (i in array && !iterator.call(that, array[i], i, array)) { ret = false; - for (i = 0; i < len; i++) { - if (i in arr && fn.call(that, arr[i], i, arr)) { - ret = true; - break; + break; + } + } + return ret; +}; + +/*@ +Ox.fallback.filter see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter + > Ox.fallback.filter.call([2, 1, 0], function(v, i) { return v == i; }) + [1] +@*/ +Ox.fallback.filter = function(iterator, that) { + if (this === void 0 || this === null || typeof iterator !== 'function') { + throw new TypeError(); + } + var array = Object(this), i, length = array.length >>> 0, ret = [], value; + for (i = 0; i < length; i++) { + // save value in case iterator mutates it + if (i in array && iterator.call(that, value = array[i], i, array)) { + ret.push(value); + } + } + return ret; +}; + +/*@ +Ox.fallback.forEach see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach + + > 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(); + } + var array = Object(this), i, length = array.length >>> 0; + for (i = 0; i < length; i++) { + if (i in array) { + iterator.call(that, array[i], i, array); + } + } +}; + +/*@ +Ox.fallback.indexOf see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf + > 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) { + throw new TypeError(); + } + var array = Object(this), i, length = array.length >>> 0, ret = -1; + for (i = 0; i < length; i++) { + if (i in array && array[i] === value) { + ret = i; + break; + } + } + return ret; +}; + +/*@ +Ox.fallback.lastIndexOf see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf + > 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) { + throw new TypeError(); + } + var array = Object(this), i, length = array.length >>> 0, ret = -1; + for (i = length - 1; i >= 0; i--) { + if (i in array && array[i] === value) { + ret = i; + break; + } + } + return ret; +}; + +/*@ +Ox.fallback.map see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map + > Ox.fallback.map.call([2, 1, 0], function(v, i) { return v == i; }) + [false, true, false] +@*/ +Ox.fallback.map = function(iterator, that) { + if (this === void 0 || this === null || typeof iterator !== 'function') { + throw new TypeError(); + } + var array = Object(this), i, length = array.length >>> 0, + ret = new Array(length); + for (i = 0; i < length; i++) { + if (i in array) { + ret[i] = iterator.call(that, array[i], i, array); + } + } + return ret; +}; + +/*@ +Ox.fallback.reduce see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce + > Ox.fallback.reduce.call([1, 2, 3], function(p, c, i) { return p + c + i; }, 1) + 10 +@*/ +Ox.fallback.reduce = function(iterator, ret) { + if (this === void 0 || this === null || typeof iterator !== 'function') { + throw new TypeError(); + } + var array = Object(this), i, length = array.length; + if (!length && ret === void 0) { + throw new TypeError(); + } + if (ret === void 0) { + ret = array[0]; + i = 1; + } + for (i = i || 0; i < length; i++) { + if (i in array) { + ret = iterator.call(void 0, ret, array[i], i, array); + } + } + return ret; +}; + +/*@ +Ox.fallback.reduceRight see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduceRight + > Ox.fallback.reduceRight.call([1, 2, 3], function(p, c, i) { return p + c + i; }, 1) + 10 +@*/ +Ox.fallback.reduceRight = function(iterator, ret) { + if (this === void 0 || this === null || typeof iterator !== 'function') { + throw new TypeError(); + } + var array = Object(this), i, length = array.length; + if (!length && ret === void 0) { + throw new TypeError(); + } + if (ret === void 0) { + ret = array[length - 1]; + i = length - 2; + } + for (i = i || length - 1; i >= 0; i--) { + if (i in array) { + ret = iterator.call(void 0, ret, array[i], i, array); + } + } + return ret; +}; + +/*@ +Ox.fallback.some see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some + > Ox.fallback.some.call([2, 1, 0], function(v, i) { return v == i; }) + 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(); + } + var array = Object(this), i, length = array.length >>> 0, ret = false; + for (i = 0; i < length; i++) { + if (i in array && iterator.call(that, array[i], i, array)) { + ret = true; + break; + } + } + return ret; +}; + +/*@ +Ox.fallback.keys 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); + } + } + return ret; +}; + +/*@ +Ox.fallback.trim see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim + > Ox.fallback.trim.call(' foo ') + 'foo' +@*/ +Ox.fallback.trim = function() { + 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]; } } - 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(); - } - 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/Array/reduceRight -if (!Array.prototype.reduceRight) { - Array.prototype.reduceRight = 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(); - } - if (ret === void 0) { - ret = arr[len - 1]; - i = len - 2; - } - for (i = i || len - 1; i >= 0; 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; - }; -} - -// 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, ''); - }; -} + } +})();