Fixed some spelling errors in Specs and Docs for Array.item. -- thanks fabiomcosta
[mootools.git] / Specs / Native / Array.js
blobd33c8d8dc4300510015ae3c57326b88d545fdd26
1 /*
2 Script: Array.js
3         Specs for Array.js
5 License:
6         MIT-style license.
7 */
9 describe("Array Methods", {
11         // Array.flatten
13         'should flatten a multidimensional array': function(){
14                 var arr = [1,2,3,[4,5,[6,7,[8]]], [[[[[9]]]]]];
15                 value_of(arr.flatten()).should_be([1,2,3,4,5,6,7,8,9]);
16         },
18         'should flatten arguments': function(){
19                 var test = function(){
20                         return Array.flatten(arguments);
21                 };
22                 value_of(test(1,2,3)).should_be([1,2,3]);
23                 value_of(test([1,2,3])).should_be([1,2,3]);
24                 value_of(test(1,2,[3])).should_be([1,2,3]);
25         },
27         // Array.filter
29         'should filter an array': function(){
30                 var array = [1,2,3,0,0,0];
31                 var arr = array.concat([false, null, 4]).filter(Number.type);
32                 value_of(arr).should_be(array.concat(4));
33         },
35         // Array.clean
37         'should clean an array from undefined and null values': function(){
38                 var array = [null, 1, 0, true, false, "foo", undefined];
39                 var arr = array.clean();
40                 value_of(arr).should_be([1, 0, true, false, "foo"]);
41         },
43         // Array.map
45         'should return a mapping of an array': function(){
46                 var arr = [1,2,3,0,0,0].map(function(item){
47                         return (item + 1);
48                 });
50                 value_of(arr).should_be([2,3,4,1,1,1]);
51         },
53         // Array.every
55         'should return true if every item matches the comparator, otherwise false': function(){
56                 value_of([1,2,3,0,0,0].every(Number.type)).should_be_true();
58                 value_of(['1',2,3,0].every(Number.type)).should_be_false();
59         },
61         // Array.some
63         'should return true if some of the items in the array match the comparator, otherwise false': function(){
64                 value_of(['1',2,3,0].some(Number.type)).should_be_true();
66                 value_of([1,2,3,0,0,0].map(String).some(Number.type)).should_be_false();
67         },
69         // Array.indexOf
71         'should return the index of the item': function(){
72                 value_of([1,2,3,0,0,0].indexOf(0)).should_be(3);
73         },
75         'should return -1 if the item is not found in the array': function(){
76                 value_of([1,2,3,0,0,0].indexOf('not found')).should_be(-1);
77         },
79         // Array.erase
81         'should remove all items in the array that match the specified item': function(){
82                 var arr = [1,2,3,0,0,0].erase(0);
83                 value_of(arr).should_be([1,2,3]);
84         },
86         // Array.contains
88         'should return true if the array contains the specified item': function(){
89                 value_of([1,2,3,0,0,0].contains(0)).should_be_true();
90         },
92         'should return false if the array does not contain the specified item': function(){
93                 value_of([0,1,2].contains('not found')).should_be_false();
94         },
96         // Array.associate
98         'should associate an array with a specified array': function(){
99                 var obj = [1,2,3,0,0,0].associate(['a', 'b', 'c', 'd']);
100                 value_of(obj).should_be({a:1, b:2, c:3, d:0});
101         },
103         // Array.link
105         'should link an array items to a new object according to the specified matchers': function(){
106                 var el = document.createElement('div');
107                 var assoc2 = [100, 'Hello', {foo: 'bar'}, el, false].link({
108                         myNumber: Number.type,
109                         myElement: Element.type,
110                         myObject: Object.type,
111                         myString: String.type,
112                         myBoolean: $defined
113                 });
115                 value_of(assoc2).should_be({
116                         myNumber: 100,
117                         myElement: el,
118                         myObject: {foo: 'bar'},
119                         myString: 'Hello',
120                         myBoolean: false
121                 });
122         },
124         // Array.extend
126         'should extend an array': function(){
127                 var a = [1,2,4];
128                 var b = [2,3,4,5];
129                 a.extend(b);
130                 value_of(a).should_be([1,2,4,2,3,4,5]);
131                 value_of(b).should_be([2,3,4,5]);
132         },
134         // Array.combine
136         'should combine an array': function(){
137                 var arr = [1,2,3,4].combine([3,1,4,5,6,7]);
138                 value_of(arr).should_be([1,2,3,4,5,6,7]);
139         },
141         // Array.include
143         'should include only new items': function(){
144                 var arr = [1,2,3,4].include(1).include(5);
145                 value_of(arr).should_be([1,2,3,4,5]);
146         },
148         // Array.getLast
150         'should return the last item in the array': function(){
151                 value_of([1,2,3,0,0,0].getLast()).should_be(0);
152         },
154         'should return null if there are no items': function(){
155                 value_of([].getLast()).should_be(null);
156         },
158         // Array.empty
160         'should empty the array': function(){
161                 var arr = [1,2,3,4].empty();
162                 value_of(arr).should_be([]);
163         },
164         
165         // Array.item
166         
167         'should return the item': function(){
168             var arr = [1,2,3,4];
169             value_of(arr.item(0)).should_be(1);
170             value_of(arr.item(-1)).should_be(4);
171         },
172         
173         'should return null if no item or no items in the array': function(){
174             var arr = [1,2,3,4];
175             value_of(arr.item(10)).should_be(null);
176             
177             arr = [];
178             value_of(arr.item(-1)).should_be(null);
179         }
183 describe("Array Color Methods", {
185         // Array.hexToRgb
187         'should return null if the length of the array is not 3': function(){
188                 value_of([].hexToRgb()).should_be_null();
189         },
191         'should return a CSS rgb string': function(){
192                 value_of(['0','0','0'].hexToRgb()).should_be('rgb(0,0,0)');
193         },
195         'should support shorthand hex': function(){
196                 value_of(['c','c','c'].hexToRgb()).should_be('rgb(204,204,204)');
197         },
199         'should return an array with 16-based numbers when passed true': function(){
200                 value_of(['ff','ff','ff'].hexToRgb(true)).should_be([255,255,255]);
201         },
203         // Array.rgbToHex
205         'should return null if the array does not have at least 3 times': function(){
206                 value_of([0,1].rgbToHex()).should_be_null();
207         },
209         'should return a css hexadecimal string': function(){
210                 value_of(['255', '0', '0'].rgbToHex()).should_be('#ff0000');
211                 value_of([0,0,255].rgbToHex()).should_be('#0000ff');
212         },
214         'should return an array with hexadecimal string items': function(){
215                 value_of([0,255,0].rgbToHex(true)).should_be(['00', 'ff', '00']);
216         },
218         'should return `transparent` if the fourth item is 0 and first param is not true': function(){
219                 value_of([0,0,0,0].rgbToHex()).should_be('transparent');
220         }