Forgot to revert getLast. Thanks magmoro.
[mootools.git] / Source / Native / Array.js
blobecbd426374f654f06b38ea2013b449d139c6664c
1 /*
2 Script: Array.js
3         Contains Array Prototypes like each, contains, and erase.
5 License:
6         MIT-style license.
7 */
9 Array.implement({
11         every: function(fn, bind){
12                 for (var i = 0, l = this.length; i < l; i++){
13                         if (!fn.call(bind, this[i], i, this)) return false;
14                 }
15                 return true;
16         },
18         filter: function(fn, bind){
19                 var results = [];
20                 for (var i = 0, l = this.length; i < l; i++){
21                         if (fn.call(bind, this[i], i, this)) results.push(this[i]);
22                 }
23                 return results;
24         },
26         clean: function() {
27                 return this.filter($defined);
28         },
30         indexOf: function(item, from){
31                 var len = this.length;
32                 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
33                         if (this[i] === item) return i;
34                 }
35                 return -1;
36         },
38         map: function(fn, bind){
39                 var results = [];
40                 for (var i = 0, l = this.length; i < l; i++) results[i] = fn.call(bind, this[i], i, this);
41                 return results;
42         },
44         some: function(fn, bind){
45                 for (var i = 0, l = this.length; i < l; i++){
46                         if (fn.call(bind, this[i], i, this)) return true;
47                 }
48                 return false;
49         },
51         associate: function(keys){
52                 var obj = {}, length = Math.min(this.length, keys.length);
53                 for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
54                 return obj;
55         },
57         link: function(object){
58                 var result = {};
59                 for (var i = 0, l = this.length; i < l; i++){
60                         for (var key in object){
61                                 if (object[key](this[i])){
62                                         result[key] = this[i];
63                                         delete object[key];
64                                         break;
65                                 }
66                         }
67                 }
68                 return result;
69         },
71         contains: function(item, from){
72                 return this.indexOf(item, from) != -1;
73         },
75         extend: function(array){
76                 for (var i = 0, j = array.length; i < j; i++) this.push(array[i]);
77                 return this;
78         },
79         
80         getLast: function(){
81                 return this[this.length - 1];
82         },
84         getRandom: function(){
85                 return (this.length) ? this[$random(0, this.length - 1)] : null;
86         },
88         include: function(item){
89                 if (!this.contains(item)) this.push(item);
90                 return this;
91         },
93         combine: function(array){
94                 for (var i = 0, l = array.length; i < l; i++) this.include(array[i]);
95                 return this;
96         },
98         erase: function(item){
99                 for (var i = this.length; i--; i){
100                         if (this[i] === item) this.splice(i, 1);
101                 }
102                 return this;
103         },
105         empty: function(){
106                 this.length = 0;
107                 return this;
108         },
110         flatten: function(){
111                 var array = [];
112                 for (var i = 0, l = this.length; i < l; i++){
113                         var type = $type(this[i]);
114                         if (!type) continue;
115                         array = array.concat((type == 'array' || type == 'collection' || type == 'arguments') ? Array.flatten(this[i]) : this[i]);
116                 }
117                 return array;
118         },
120         hexToRgb: function(array){
121                 if (this.length != 3) return null;
122                 var rgb = this.map(function(value){
123                         if (value.length == 1) value += value;
124                         return value.toInt(16);
125                 });
126                 return (array) ? rgb : 'rgb(' + rgb + ')';
127         },
129         rgbToHex: function(array){
130                 if (this.length < 3) return null;
131                 if (this.length == 4 && this[3] == 0 && !array) return 'transparent';
132                 var hex = [];
133                 for (var i = 0; i < 3; i++){
134                         var bit = (this[i] - 0).toString(16);
135                         hex.push((bit.length == 1) ? '0' + bit : bit);
136                 }
137                 return (array) ? hex : '#' + hex.join('');
138         }