]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/augment.js/augment.js
Use augment.js for improved cross browser compatibility
[rails.git] / vendor / assets / augment.js / augment.js
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
4 //
5 //  Released under MIT license.
6 //
7 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
8 if (!Array.prototype.every) {
9   Array.prototype.every = function(fun /*, thisp */) {
10     "use strict";
11
12     if (this === void 0 || this === null)
13       throw new TypeError();
14
15     var t = Object(this);
16     var len = t.length >>> 0;
17     if (typeof fun !== "function")
18       throw new TypeError();
19
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))
23         return false;
24     }
25
26     return true;
27   };
28 }
29 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
30 if (!Array.prototype.filter) {
31   Array.prototype.filter = function(fun /*, thisp */) {
32     "use strict";
33
34     if (this === void 0 || this === null)
35       throw new TypeError();
36
37     var t = Object(this);
38     var len = t.length >>> 0;
39     if (typeof fun !== "function")
40       throw new TypeError();
41
42     var res = [];
43     var thisp = arguments[1];
44     for (var i = 0; i < len; i++) {
45       if (i in t) {
46         var val = t[i]; // in case fun mutates this
47         if (fun.call(thisp, val, i, t))
48           res.push(val);
49       }
50     }
51
52     return res;
53   };
54 }
55 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/foreach
56 if (!Array.prototype.forEach) {
57   Array.prototype.forEach = function(fun /*, thisp */) {
58     "use strict";
59
60     if (this === void 0 || this === null)
61       throw new TypeError();
62
63     var t = Object(this);
64     var len = t.length >>> 0;
65     if (typeof fun !== "function")
66       throw new TypeError();
67
68     var thisp = arguments[1];
69     for (var i = 0; i < len; i++) {
70       if (i in t)
71         fun.call(thisp, t[i], i, t);
72     }
73   };
74 }
75 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
76 if (!Array.prototype.indexOf) {
77   Array.prototype.indexOf = function(searchElement /*, fromIndex */) {
78     "use strict";
79
80     if (this === void 0 || this === null)
81       throw new TypeError();
82
83     var t = Object(this);
84     var len = t.length >>> 0;
85     if (len === 0)
86       return -1;
87
88     var n = 0;
89     if (arguments.length > 0) {
90       n = Number(arguments[1]);
91       if (n !== n) // shortcut for verifying if it's NaN
92         n = 0;
93       else if (n !== 0 && n !== (Infinity) && n !== -(Infinity))
94         n = (n > 0 || -1) * Math.floor(Math.abs(n));
95     }
96
97     if (n >= len)
98       return -1;
99
100     var k = n >= 0
101           ? n
102           : Math.max(len - Math.abs(n), 0);
103
104     for (; k < len; k++) {
105       if (k in t && t[k] === searchElement)
106         return k;
107     }
108     return -1;
109   };
110 }
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*/) {
116     "use strict";
117
118     if (this === void 0 || this === null)
119       throw new TypeError();
120
121     var t = Object(this);
122     var len = t.length >>> 0;
123     if (len === 0)
124       return -1;
125
126     var n = len;
127     if (arguments.length > 1) {
128       n = Number(arguments[1]);
129       if (n !== n)
130         n = 0;
131       else if (n !== 0 && n !== (Infinity) && n !== -(Infinity))
132         n = (n > 0 || -1) * Math.floor(Math.abs(n));
133     }
134
135     var k = n >= 0
136           ? Math.min(n, len - 1)
137           : len - Math.abs(n);
138
139     for (; k >= 0; k--) {
140       if (k in t && t[k] === searchElement)
141         return k;
142     }
143     return -1;
144   };
145 }
146 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
147 if (!Array.prototype.map) {
148   Array.prototype.map = function(fun /*, thisp */) {
149     "use strict";
150
151     if (this === void 0 || this === null)
152       throw new TypeError();
153
154     var t = Object(this);
155     var len = t.length >>> 0;
156     if (typeof fun !== "function")
157       throw new TypeError();
158
159     var res = new Array(len);
160     var thisp = arguments[1];
161     for (var i = 0; i < len; i++) {
162       if (i in t)
163         res[i] = fun.call(thisp, t[i], i, t);
164     }
165
166     return res;
167   };
168 }
169 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
170 if (!Array.prototype.reduce) {
171   Array.prototype.reduce = function(fun /*, initialValue */) {
172     "use strict";
173
174     if (this === void 0 || this === null)
175       throw new TypeError();
176
177     var t = Object(this);
178     var len = t.length >>> 0;
179     if (typeof fun !== "function")
180       throw new TypeError();
181
182     // no value to return if no initial value and an empty array
183     if (len == 0 && arguments.length == 1)
184       throw new TypeError();
185
186     var k = 0;
187     var accumulator;
188     if (arguments.length >= 2) {
189       accumulator = arguments[1];
190     } else {
191       do {
192         if (k in t) {
193           accumulator = t[k++];
194           break;
195         }
196
197         // if array contains no values, no initial value to return
198         if (++k >= len)
199           throw new TypeError();
200       }
201       while (true);
202     }
203
204     while (k < len) {
205       if (k in t)
206         accumulator = fun.call(undefined, accumulator, t[k], k, t);
207       k++;
208     }
209
210     return accumulator;
211   };
212 }
213 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ReduceRight
214 if (!Array.prototype.reduceRight) {
215   Array.prototype.reduceRight = function(callbackfn /*, initialValue */) {
216     "use strict";
217
218     if (this === void 0 || this === null)
219       throw new TypeError();
220
221     var t = Object(this);
222     var len = t.length >>> 0;
223     if (typeof callbackfn !== "function")
224       throw new TypeError();
225
226     // no value to return if no initial value, empty array
227     if (len === 0 && arguments.length === 1)
228       throw new TypeError();
229
230     var k = len - 1;
231     var accumulator;
232     if (arguments.length >= 2) {
233       accumulator = arguments[1];
234     } else {
235       do {
236         if (k in this) {
237           accumulator = this[k--];
238           break;
239         }
240
241         // if array contains no values, no initial value to return
242         if (--k < 0)
243           throw new TypeError();
244       }
245       while (true);
246     }
247
248     while (k >= 0) {
249       if (k in t)
250         accumulator = callbackfn.call(undefined, accumulator, t[k], k, t);
251       k--;
252     }
253
254     return accumulator;
255   };
256 }
257 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
258 if (!Array.prototype.some) {
259   Array.prototype.some = function(fun /*, thisp */) {
260     "use strict";
261
262     if (this === void 0 || this === null)
263       throw new TypeError();
264
265     var t = Object(this);
266     var len = t.length >>> 0;
267     if (typeof fun !== "function")
268       throw new TypeError();
269
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))
273         return true;
274     }
275
276     return false;
277   };
278 }
279 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/now
280 if (!Date.now) {
281   Date.now = function now () {
282     return +new Date();
283   };
284 }
285 if (!Date.prototype.toISOString) {
286   Date.prototype.toISOString = (function () {
287
288     var pad = function (n, length) {
289       length = length || 2
290       return (n = n + "", n.length === length) ? n : pad("0" + n, length);
291     }
292
293     return function () {
294       var year = this.getUTCFullYear()
295       year = (year < 0 ? '-' : (year > 9999 ? '+' : '')) + ('00000' + Math.abs(year)).slice(0 <= year && year <= 9999 ? -4 : -6);
296
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"
300     }
301   })()
302 };
303 if (!Date.prototype.toJSON) {
304   Date.prototype.toJSON = Date.prototype.toJSON
305 };
306 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
307 if (!Function.prototype.bind ) {
308
309   Function.prototype.bind = function(obj) {
310
311     if (typeof this !== 'function') throw new TypeError ("Function.prototype.bind - what is trying to be bound is not callable")
312
313     var slice = Array.prototype.slice,
314         args = slice.call(arguments, 1), 
315         self = this, 
316         nop = function () {}, 
317         bound = function () {
318
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
322           } else {
323             return self.apply(obj, args.concat(slice.call(arguments)))
324           };
325         };
326
327     nop.prototype = self.prototype;
328
329     bound.prototype = new nop();
330
331     return bound;
332   };
333 }
334 ;(function () { "use strict"
335
336   var ensureIsObject = function (param) {
337     if (param !== Object(param)) throw new TypeError('Object.getPrototypeOf called on non-object');
338   }
339
340   if (!Object.getPrototypeOf) {
341     if (typeof "test".__proto__ === "object") {
342       Object.getPrototypeOf = function (obj) {
343         ensureIsObject(obj)
344         return obj.__proto__
345       }
346     } else {
347       Object.getPrototypeOf = function (obj) {
348         ensureIsObject(obj)
349         return obj.constructor.prototype
350       }
351     };
352   };
353 })();
354 // http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
355 if (!Object.keys) {
356   Object.keys = (function () {
357     var hasOwnProperty = Object.prototype.hasOwnProperty,
358         hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
359         dontEnums = [
360           'toString',
361           'toLocaleString',
362           'valueOf',
363           'hasOwnProperty',
364           'isPrototypeOf',
365           'propertyIsEnumerable',
366           'constructor'
367         ],
368         dontEnumsLength = dontEnums.length
369
370     return function (obj) {
371       if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object')
372
373       var result = []
374
375       for (var prop in obj) {
376         if (hasOwnProperty.call(obj, prop)) result.push(prop)
377       }
378
379       if (hasDontEnumBug) {
380         for (var i=0; i < dontEnumsLength; i++) {
381           if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i])
382         }
383       }
384       return result
385     }
386   })()
387 };if (!String.prototype.trim) {
388   String.prototype.trim = (function () {
389
390     var trimLeft  = /^\s+/,
391         trimRight = /\s+$/
392
393     return function () {
394       return this.replace(trimLeft, "").replace(trimRight, "")
395     }
396   })()
397 };