e6aa03ab3c6299b9aa073d11417c23edd3c3001c
[gnome.introspection-doc-generator] / Array.js
1 /**
2  * Compat array utils..
3  *
4  * <script type="text/javascript">
5  *
6  */
7 Array.prototype.filter = function(fun /*, thisp*/)
8   {
9     var len = this.length;
10     if (typeof(fun) != "function")
11       throw "typeerror";
12
13     var res = new Array();
14     var thisp = arguments[1];
15     var t = this;
16     for (var i = 0; i < len; i++)
17     {
18      
19         var val = t[i]; // in case fun mutates this
20         if (fun.call(thisp, val, i, t))
21           res.push(val);
22       //}
23     }
24
25     return res;
26   };
27
28 Array.prototype.map = function(fun /*, thisp*/)
29   {
30     var len = this.length;
31     if (typeof(fun) != "function")
32       throw "typeerror";
33
34     var res = new Array(len);
35     var thisp = arguments[1];
36     for (var i = 0; i < len; i++)
37     {
38       
39         res[i] = fun.call(thisp, this[i], i, this);
40     }
41
42     return res;
43   };
44
45 if (typeof(Array.prototype['lastIndexOf']) == 'undefined')
46 {
47   Array.prototype.lastIndexOf = function(elt /*, from*/)
48   {
49     var len = this.length;
50
51     var from = Number(arguments[1]);
52     if (isNaN(from)) {
53       from = len - 1;
54     }   else   {
55       from = (from < 0)
56            ? Math.ceil(from)
57            : Math.floor(from);
58       if (from < 0)
59         from += len;
60       else if (from >= len)
61         from = len - 1;
62     }
63     var _t = this;
64     for (; from > -1; from--)
65     {
66       if ((typeof(_t[from]) !== 'undefined') &&
67           this[from] === elt)
68         return from;
69     }
70     return -1;
71   };
72 }