1 // augment.js JavaScript 1.8.5 methods for all, version: 0.4.2
2 // using snippets from Mozilla - https://developer.mozilla.org/en/JavaScript
3 // (c) 2011 Oliver Nightingale
5 // Released under MIT license.
7 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
8 if (!Array.prototype.every) {
9 Array.prototype.every = function(fun /*, thisp */) {
12 if (this === void 0 || this === null)
13 throw new TypeError();
16 var len = t.length >>> 0;
17 if (typeof fun !== "function")
18 throw new TypeError();
20 var thisp = arguments[1];
21 for (var i = 0; i < len; i++) {
22 if (i in t && !fun.call(thisp, t[i], i, t))
29 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
30 if (!Array.prototype.filter) {
31 Array.prototype.filter = function(fun /*, thisp */) {
34 if (this === void 0 || this === null)
35 throw new TypeError();
38 var len = t.length >>> 0;
39 if (typeof fun !== "function")
40 throw new TypeError();
43 var thisp = arguments[1];
44 for (var i = 0; i < len; i++) {
46 var val = t[i]; // in case fun mutates this
47 if (fun.call(thisp, val, i, t))
55 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/foreach
56 if (!Array.prototype.forEach) {
57 Array.prototype.forEach = function(fun /*, thisp */) {
60 if (this === void 0 || this === null)
61 throw new TypeError();
64 var len = t.length >>> 0;
65 if (typeof fun !== "function")
66 throw new TypeError();
68 var thisp = arguments[1];
69 for (var i = 0; i < len; i++) {
71 fun.call(thisp, t[i], i, t);
75 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
76 if (!Array.prototype.indexOf) {
77 Array.prototype.indexOf = function(searchElement /*, fromIndex */) {
80 if (this === void 0 || this === null)
81 throw new TypeError();
84 var len = t.length >>> 0;
89 if (arguments.length > 0) {
90 n = Number(arguments[1]);
91 if (n !== n) // shortcut for verifying if it's NaN
93 else if (n !== 0 && n !== (Infinity) && n !== -(Infinity))
94 n = (n > 0 || -1) * Math.floor(Math.abs(n));
102 : Math.max(len - Math.abs(n), 0);
104 for (; k < len; k++) {
105 if (k in t && t[k] === searchElement)
111 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
112 Array.isArray = Array.isArray || function(o) { return Object.prototype.toString.call(o) === '[object Array]'; };
113 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
114 if (!Array.prototype.lastIndexOf) {
115 Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
118 if (this === void 0 || this === null)
119 throw new TypeError();
121 var t = Object(this);
122 var len = t.length >>> 0;
127 if (arguments.length > 1) {
128 n = Number(arguments[1]);
131 else if (n !== 0 && n !== (Infinity) && n !== -(Infinity))
132 n = (n > 0 || -1) * Math.floor(Math.abs(n));
136 ? Math.min(n, len - 1)
139 for (; k >= 0; k--) {
140 if (k in t && t[k] === searchElement)
146 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
147 if (!Array.prototype.map) {
148 Array.prototype.map = function(fun /*, thisp */) {
151 if (this === void 0 || this === null)
152 throw new TypeError();
154 var t = Object(this);
155 var len = t.length >>> 0;
156 if (typeof fun !== "function")
157 throw new TypeError();
159 var res = new Array(len);
160 var thisp = arguments[1];
161 for (var i = 0; i < len; i++) {
163 res[i] = fun.call(thisp, t[i], i, t);
169 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
170 if (!Array.prototype.reduce) {
171 Array.prototype.reduce = function(fun /*, initialValue */) {
174 if (this === void 0 || this === null)
175 throw new TypeError();
177 var t = Object(this);
178 var len = t.length >>> 0;
179 if (typeof fun !== "function")
180 throw new TypeError();
182 // no value to return if no initial value and an empty array
183 if (len == 0 && arguments.length == 1)
184 throw new TypeError();
188 if (arguments.length >= 2) {
189 accumulator = arguments[1];
193 accumulator = t[k++];
197 // if array contains no values, no initial value to return
199 throw new TypeError();
206 accumulator = fun.call(undefined, accumulator, t[k], k, t);
213 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ReduceRight
214 if (!Array.prototype.reduceRight) {
215 Array.prototype.reduceRight = function(callbackfn /*, initialValue */) {
218 if (this === void 0 || this === null)
219 throw new TypeError();
221 var t = Object(this);
222 var len = t.length >>> 0;
223 if (typeof callbackfn !== "function")
224 throw new TypeError();
226 // no value to return if no initial value, empty array
227 if (len === 0 && arguments.length === 1)
228 throw new TypeError();
232 if (arguments.length >= 2) {
233 accumulator = arguments[1];
237 accumulator = this[k--];
241 // if array contains no values, no initial value to return
243 throw new TypeError();
250 accumulator = callbackfn.call(undefined, accumulator, t[k], k, t);
257 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
258 if (!Array.prototype.some) {
259 Array.prototype.some = function(fun /*, thisp */) {
262 if (this === void 0 || this === null)
263 throw new TypeError();
265 var t = Object(this);
266 var len = t.length >>> 0;
267 if (typeof fun !== "function")
268 throw new TypeError();
270 var thisp = arguments[1];
271 for (var i = 0; i < len; i++) {
272 if (i in t && fun.call(thisp, t[i], i, t))
279 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/now
281 Date.now = function now () {
285 if (!Date.prototype.toISOString) {
286 Date.prototype.toISOString = (function () {
288 var pad = function (n, length) {
290 return (n = n + "", n.length === length) ? n : pad("0" + n, length);
294 var year = this.getUTCFullYear()
295 year = (year < 0 ? '-' : (year > 9999 ? '+' : '')) + ('00000' + Math.abs(year)).slice(0 <= year && year <= 9999 ? -4 : -6);
297 var date = [year, pad(this.getUTCMonth() + 1), pad(this.getUTCDate())].join("-")
298 var time = [pad(this.getUTCHours()), pad(this.getUTCMinutes()), pad(this.getUTCSeconds())].join(":") + "." + pad(this.getUTCMilliseconds(), 3)
299 return [date, time].join("T") + "Z"
303 if (!Date.prototype.toJSON) {
304 Date.prototype.toJSON = Date.prototype.toJSON
306 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
307 if (!Function.prototype.bind ) {
309 Function.prototype.bind = function(obj) {
311 if (typeof this !== 'function') throw new TypeError ("Function.prototype.bind - what is trying to be bound is not callable")
313 var slice = Array.prototype.slice,
314 args = slice.call(arguments, 1),
316 nop = function () {},
317 bound = function () {
319 if (nop.prototype && this instanceof nop) {
320 var result = self.apply(new nop, args.concat(slice.call(arguments)))
321 return (Object(result) === result) ? result : self
323 return self.apply(obj, args.concat(slice.call(arguments)))
327 nop.prototype = self.prototype;
329 bound.prototype = new nop();
334 ;(function () { "use strict"
336 var ensureIsObject = function (param) {
337 if (param !== Object(param)) throw new TypeError('Object.getPrototypeOf called on non-object');
340 if (!Object.getPrototypeOf) {
341 if (typeof "test".__proto__ === "object") {
342 Object.getPrototypeOf = function (obj) {
347 Object.getPrototypeOf = function (obj) {
349 return obj.constructor.prototype
354 // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
356 Object.keys = (function () {
357 var hasOwnProperty = Object.prototype.hasOwnProperty,
358 hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
365 'propertyIsEnumerable',
368 dontEnumsLength = dontEnums.length
370 return function (obj) {
371 if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object')
375 for (var prop in obj) {
376 if (hasOwnProperty.call(obj, prop)) result.push(prop)
379 if (hasDontEnumBug) {
380 for (var i=0; i < dontEnumsLength; i++) {
381 if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i])
387 };if (!String.prototype.trim) {
388 String.prototype.trim = (function () {
390 var trimLeft = /^\s+/,
394 return this.replace(trimLeft, "").replace(trimRight, "")