added Browser.version to the docs
[mootools/dkf.git] / Source / Types / Array.js
blobfeee3617f1f6def008063e1a2c4c241a1d462b7b
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         invoke: function(methodName){
20                 var args = Array.slice(arguments, 1);
21                 return this.map(function(item){
22                         return item[methodName].apply(item, args);
23                 });
24         },
26         every: function(fn, bind){
27                 for (var i = 0, l = this.length; i < l; i++){
28                         if ((i in this) && !fn.call(bind, this[i], i, this)) return false;
29                 }
30                 return true;
31         },
33         filter: function(fn, bind){
34                 var results = [];
35                 for (var i = 0, l = this.length; i < l; i++){
36                         if ((i in this) && fn.call(bind, this[i], i, this)) results.push(this[i]);
37                 }
38                 return results;
39         },
41         clean: function(){
42                 return this.filter(function(item){
43                         return item != null;
44                 });
45         },
47         indexOf: function(item, from){
48                 var len = this.length;
49                 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
50                         if (this[i] === item) return i;
51                 }
52                 return -1;
53         },
55         map: function(fn, bind){
56                 var results = [];
57                 for (var i = 0, l = this.length; i < l; i++){
58                         if (i in this) results[i] = fn.call(bind, this[i], i, this);
59                 }
60                 return results;
61         },
63         some: function(fn, bind){
64                 for (var i = 0, l = this.length; i < l; i++){
65                         if ((i in this) && fn.call(bind, this[i], i, this)) return true;
66                 }
67                 return false;
68         },
70         associate: function(keys){
71                 var obj = {}, length = Math.min(this.length, keys.length);
72                 for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
73                 return obj;
74         },
76         link: function(object){
77                 var result = {};
78                 for (var i = 0, l = this.length; i < l; i++){
79                         for (var key in object){
80                                 if (object[key](this[i])){
81                                         result[key] = this[i];
82                                         delete object[key];
83                                         break;
84                                 }
85                         }
86                 }
87                 return result;
88         },
90         contains: function(item, from){
91                 return this.indexOf(item, from) != -1;
92         },
94         append: function(array){
95                 this.push.apply(this, array);
96                 return this;
97         },
99         getLast: function(){
100                 return (this.length) ? this[this.length - 1] : null;
101         },
103         getRandom: function(){
104                 return (this.length) ? this[Number.random(0, this.length - 1)] : null;
105         },
107         include: function(item){
108                 if (!this.contains(item)) this.push(item);
109                 return this;
110         },
112         combine: function(array){
113                 for (var i = 0, l = array.length; i < l; i++) this.include(array[i]);
114                 return this;
115         },
117         erase: function(item){
118                 for (var i = this.length; i--;){
119                         if (this[i] === item) this.splice(i, 1);
120                 }
121                 return this;
122         },
124         empty: function(){
125                 this.length = 0;
126                 return this;
127         },
129         flatten: function(){
130                 var array = [];
131                 for (var i = 0, l = this.length; i < l; i++){
132                         var type = typeOf(this[i]);
133                         if (type == 'null') continue;
134                         array = array.concat((type == 'array' || type == 'collection' || type == 'arguments' || instanceOf(this[i], Array)) ? Array.flatten(this[i]) : this[i]);
135                 }
136                 return array;
137         },
139         pick: function(){
140                 for (var i = 0, l = this.length; i < l; i++){
141                         if (this[i] != null) return this[i];
142                 }
143                 return null;
144         },
146         hexToRgb: function(array){
147                 if (this.length != 3) return null;
148                 var rgb = this.map(function(value){
149                         if (value.length == 1) value += value;
150                         return value.toInt(16);
151                 });
152                 return (array) ? rgb : 'rgb(' + rgb + ')';
153         },
155         rgbToHex: function(array){
156                 if (this.length < 3) return null;
157                 if (this.length == 4 && this[3] == 0 && !array) return 'transparent';
158                 var hex = [];
159                 for (var i = 0; i < 3; i++){
160                         var bit = (this[i] - 0).toString(16);
161                         hex.push((bit.length == 1) ? '0' + bit : bit);
162                 }
163                 return (array) ? hex : '#' + hex.join('');
164         }
168 //<1.2compat>
170 Array.alias('extend', 'append');
172 var $pick = function(){
173         return Array.from(arguments).pick();
176 //</1.2compat>