Merge pull request #2741 from timwienk/gruntfile-restructuring
[mootools.git] / Source / Types / Array.js
blob948b686da2c83f7cb00e03b60c1dffde7d476ced
1 /*
2 ---
4 name: Array
6 description: Contains Array Prototypes like each, contains, and erase.
8 license: MIT-style license.
10 requires: [Type]
12 provides: Array
14 ...
17 Array.implement({
19         /*<!ES5>*/
20         every: function(fn, bind){
21                 for (var i = 0, l = this.length >>> 0; i < l; i++){
22                         if ((i in this) && !fn.call(bind, this[i], i, this)) return false;
23                 }
24                 return true;
25         },
27         filter: function(fn, bind){
28                 var results = [];
29                 for (var value, i = 0, l = this.length >>> 0; i < l; i++) if (i in this){
30                         value = this[i];
31                         if (fn.call(bind, value, i, this)) results.push(value);
32                 }
33                 return results;
34         },
36         indexOf: function(item, from){
37                 var length = this.length >>> 0;
38                 for (var i = (from < 0) ? Math.max(0, length + from) : from || 0; i < length; i++){
39                         if (this[i] === item) return i;
40                 }
41                 return -1;
42         },
44         map: function(fn, bind){
45                 var length = this.length >>> 0, results = Array(length);
46                 for (var i = 0; i < length; i++){
47                         if (i in this) results[i] = fn.call(bind, this[i], i, this);
48                 }
49                 return results;
50         },
52         some: function(fn, bind){
53                 for (var i = 0, l = this.length >>> 0; i < l; i++){
54                         if ((i in this) && fn.call(bind, this[i], i, this)) return true;
55                 }
56                 return false;
57         },
58         /*</!ES5>*/
60         clean: function(){
61                 return this.filter(function(item){
62                         return item != null;
63                 });
64         },
66         invoke: function(methodName){
67                 var args = Array.slice(arguments, 1);
68                 return this.map(function(item){
69                         return item[methodName].apply(item, args);
70                 });
71         },
73         associate: function(keys){
74                 var obj = {}, length = Math.min(this.length, keys.length);
75                 for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
76                 return obj;
77         },
79         link: function(object){
80                 var result = {};
81                 for (var i = 0, l = this.length; i < l; i++){
82                         for (var key in object){
83                                 if (object[key](this[i])){
84                                         result[key] = this[i];
85                                         delete object[key];
86                                         break;
87                                 }
88                         }
89                 }
90                 return result;
91         },
93         contains: function(item, from){
94                 return this.indexOf(item, from) != -1;
95         },
97         append: function(array){
98                 this.push.apply(this, array);
99                 return this;
100         },
102         getLast: function(){
103                 return (this.length) ? this[this.length - 1] : null;
104         },
106         getRandom: function(){
107                 return (this.length) ? this[Number.random(0, this.length - 1)] : null;
108         },
110         include: function(item){
111                 if (!this.contains(item)) this.push(item);
112                 return this;
113         },
115         combine: function(array){
116                 for (var i = 0, l = array.length; i < l; i++) this.include(array[i]);
117                 return this;
118         },
120         erase: function(item){
121                 for (var i = this.length; i--;){
122                         if (this[i] === item) this.splice(i, 1);
123                 }
124                 return this;
125         },
127         empty: function(){
128                 this.length = 0;
129                 return this;
130         },
132         flatten: function(){
133                 var array = [];
134                 for (var i = 0, l = this.length; i < l; i++){
135                         var type = typeOf(this[i]);
136                         if (type == 'null') continue;
137                         array = array.concat((type == 'array' || type == 'collection' || type == 'arguments' || instanceOf(this[i], Array)) ? Array.flatten(this[i]) : this[i]);
138                 }
139                 return array;
140         },
142         pick: function(){
143                 for (var i = 0, l = this.length; i < l; i++){
144                         if (this[i] != null) return this[i];
145                 }
146                 return null;
147         },
149         hexToRgb: function(array){
150                 if (this.length != 3) return null;
151                 var rgb = this.map(function(value){
152                         if (value.length == 1) value += value;
153                         return parseInt(value, 16);
154                 });
155                 return (array) ? rgb : 'rgb(' + rgb + ')';
156         },
158         rgbToHex: function(array){
159                 if (this.length < 3) return null;
160                 if (this.length == 4 && this[3] == 0 && !array) return 'transparent';
161                 var hex = [];
162                 for (var i = 0; i < 3; i++){
163                         var bit = (this[i] - 0).toString(16);
164                         hex.push((bit.length == 1) ? '0' + bit : bit);
165                 }
166                 return (array) ? hex : '#' + hex.join('');
167         }
171 //<1.2compat>
173 Array.alias('extend', 'append');
175 var $pick = this.$pick = function(){
176         return Array.from(arguments).pick();
179 //</1.2compat>