11 var getTestArray = function(){
19 describe("Array", function(){
23 it('should flatten a multidimensional array', function(){
24 var arr = [1,2,3,[4,5,[6,7,[8]]], [[[[[9]]]]]];
25 expect(arr.flatten()).toEqual([1,2,3,4,5,6,7,8,9]);
28 it('should flatten arguments', function(){
29 var test = function(){
30 return Array.flatten(arguments);
32 expect(test(1,2,3)).toEqual([1,2,3]);
33 expect(test([1,2,3])).toEqual([1,2,3]);
34 expect(test(1,2,[3])).toEqual([1,2,3]);
39 it('should filter an array', function(){
40 var array = [1,2,3,0,0,0];
41 var arr = array.concat([false, null, 4]).filter(Type.isNumber);
42 expect(arr).toEqual(array.concat(4));
45 it('filter should skip deleted elements', function(){
47 getTestArray().filter(function(){
57 it('should clean an array from undefined and null values', function(){
58 var array = [null, 1, 0, true, false, "foo", undefined];
59 var arr = array.clean();
60 expect(arr).toEqual([1, 0, true, false, "foo"]);
65 it('should return a mapping of an array', function(){
66 var arr = [1,2,3,0,0,0].map(function(item){
70 expect(arr).toEqual([2,3,4,1,1,1]);
73 it('map should skip deleted elements', function(){
75 getTestArray().map(function(){
84 it('should return true if every item matches the comparator, otherwise false', function(){
85 expect([1,2,3,0,0,0].every(Type.isNumber)).toBeTruthy();
87 expect(['1',2,3,0].every(Type.isNumber)).toBeFalsy();
90 it('every should skip deleted elements', function(){
92 getTestArray().every(function(){
102 it('should return true if some of the items in the array match the comparator, otherwise false', function(){
103 expect(['1',2,3,0].some(Type.isNumber)).toBeTruthy();
105 expect([1,2,3,0,0,0].map(String).some(Type.isNumber)).toBeFalsy();
108 it('some should skip deleted elements', function(){
110 var a = getTestArray();
113 // skips the first three elements
114 a.some(function(value, index){
119 expect(i).toEqual(3);
124 it('should return the index of the item', function(){
125 expect([1,2,3,0,0,0].indexOf(0)).toEqual(3);
128 it('should return -1 if the item is not found in the array', function(){
129 expect([1,2,3,0,0,0].indexOf('not found')).toEqual(-1);
134 it('should remove all items in the array that match the specified item', function(){
135 var arr = [1,2,3,0,0,0].erase(0);
136 expect(arr).toEqual([1,2,3]);
141 it('should return true if the array contains the specified item', function(){
142 expect([1,2,3,0,0,0].contains(0)).toBeTruthy();
145 it('should return false if the array does not contain the specified item', function(){
146 expect([0,1,2].contains('not found')).toBeFalsy();
151 it('should associate an array with a specified array', function(){
152 var obj = [1,2,3,0,0,0].associate(['a', 'b', 'c', 'd']);
153 expect(obj).toEqual({a:1, b:2, c:3, d:0});
158 it('should append to an array', function(){
162 expect(a).toEqual([1,2,4,2,3,4,5]);
163 expect(b).toEqual([2,3,4,5]);
166 var isType = function(type){
167 return function(obj){
168 return typeOf(obj) == type;
173 it('should link an array items to a new object according to the specified matchers', function(){
174 var el = document.createElement('div');
175 var assoc2 = [100, 'Hello', {foo: 'bar'}, el, false].link({
176 myNumber: isType('number'),
177 myElement: isType('element'),
178 myObject: isType('object'),
179 myString: isType('string'),
180 myBoolean: isType('boolean')
183 expect(assoc2).toEqual({
186 myObject: {foo: 'bar'},
194 it('should extend an array', function(){
198 expect(a).toEqual([1,2,4,2,3,4,5]);
199 expect(b).toEqual([2,3,4,5]);
203 it('should append an array', function(){
207 expect(a).toEqual([1,2,4,2,3,4,5]);
208 expect(b).toEqual([2,3,4,5]);
213 it('should combine an array', function(){
214 var arr = [1,2,3,4].combine([3,1,4,5,6,7]);
215 expect(arr).toEqual([1,2,3,4,5,6,7]);
220 it('should include only new items', function(){
221 var arr = [1,2,3,4].include(1).include(5);
222 expect(arr).toEqual([1,2,3,4,5]);
227 it('should return the last item in the array', function(){
228 expect([1,2,3,0,0,0].getLast()).toEqual(0);
229 expect([3].getLast()).toEqual(3);
232 it('should return null if there are no items', function(){
233 expect([].getLast()).toEqual(null);
238 it('should empty the array', function(){
239 var arr = [1,2,3,4].empty();
240 expect(arr).toEqual([]);
245 describe("Array Color Methods", function(){
249 it('should return null if the length of the array is not 3', function(){
250 expect([].hexToRgb()).toBeNull();
253 it('should return a CSS rgb string', function(){
254 expect(['0','0','0'].hexToRgb()).toEqual('rgb(0,0,0)');
257 it('should support shorthand hex', function(){
258 expect(['c','c','c'].hexToRgb()).toEqual('rgb(204,204,204)');
261 it('should return an array with 16-based numbers when passed true', function(){
262 expect(['ff','ff','ff'].hexToRgb(true)).toEqual([255,255,255]);
267 it('should return null if the array does not have at least 3 times', function(){
268 expect([0,1].rgbToHex()).toBeNull();
271 it('should return a css hexadecimal string', function(){
272 expect(['255', '0', '0'].rgbToHex()).toEqual('#ff0000');
273 expect([0,0,255].rgbToHex()).toEqual('#0000ff');
276 it('should return an array with hexadecimal string items', function(){
277 expect([0,255,0].rgbToHex(true)).toEqual(['00', 'ff', '00']);
280 it('should return `transparent` if the fourth item is 0 and first param is not true', function(){
281 expect([0,0,0,0].rgbToHex()).toEqual('transparent');
286 describe('Array.getRandom', function(){
288 it('should get a random element from an array', function(){
291 expect(a.getRandom()).toEqual(1);
295 // Let's try a few times
296 expect(a).toContain(a.getRandom());
297 expect(a).toContain(a.getRandom());
298 expect(a).toContain(a.getRandom());
299 expect(a).toContain(a.getRandom());
304 describe('Array.pick', function(){
306 it('should pick a value that is not null from the array', function(){
307 expect([null, undefined, true, 1].pick()).toEqual(true);
310 it('should return null', function(){
311 expect([].pick()).toBeNull();
316 describe('Array', function(){
318 describe('Array.map', function(){
320 it('should return an array with the same length', function(){
321 expect([1, 2, 3, undefined].map(function(v){
323 }).length).toEqual(4);
326 it('shoud return an empty array when the thisArg does not has a length property', function(){
327 expect([].map.call({}, function(){
334 it('should accept thisArgs without length property', function(){
335 var object = {}, fn = function(){};
336 expect([].every.call(object, fn)).toBe(true);
337 expect([].filter.call(object, fn)).toEqual([]);
338 expect([].indexOf.call(object)).toEqual(-1);
339 expect([].map.call(object, fn)).toEqual([]);
340 expect([].some.call(object, fn)).toBe(false);
343 describe('Array.filter', function(){
345 it('should return the original item, and not any mutations.', function(){
347 var result = [0, 1, 2].filter(function(num, i, array){
349 array[i] = 'mutation';
354 expect(result[0]).toEqual(1);