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()).to.eql([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)).to.eql([1,2,3]);
33 expect(test([1,2,3])).to.eql([1,2,3]);
34 expect(test(1,2,[3])).to.eql([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).to.eql(array.concat(4));
45 it('filter should skip deleted elements', function(){
47 getTestArray().filter(function(){
52 expect(i).to.equal(2);
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).to.eql([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).to.eql([2,3,4,1,1,1]);
73 it('map should skip deleted elements', function(){
75 getTestArray().map(function(){
79 expect(i).to.equal(2);
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)).to.equal(true);
87 expect(['1',2,3,0].every(Type.isNumber)).to.equal(false);
90 it('every should skip deleted elements', function(){
92 getTestArray().every(function(){
97 expect(i).to.equal(2);
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)).to.equal(true);
105 expect([1,2,3,0,0,0].map(String).some(Type.isNumber)).to.equal(false);
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).to.equal(3);
124 it('should return the index of the item', function(){
125 expect([1,2,3,0,0,0].indexOf(0)).to.equal(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')).to.equal(-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).to.eql([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)).to.equal(true);
145 it('should return false if the array does not contain the specified item', function(){
146 expect([0,1,2].contains('not found')).to.equal(false);
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).to.eql({a:1, b:2, c:3, d:0});
158 it('should append to an array', function(){
162 expect(a).to.eql([1,2,4,2,3,4,5]);
163 expect(b).to.eql([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 if (typeof document === 'undefined'){
175 var el = {$family: function(){ return 'element'; }};
177 var el = document.createElement('div');
180 var assoc2 = [100, 'Hello', {foo: 'bar'}, el, false].link({
181 myNumber: isType('number'),
182 myElement: isType('element'),
183 myObject: isType('object'),
184 myString: isType('string'),
185 myBoolean: isType('boolean')
188 expect(assoc2).to.eql({
191 myObject: {foo: 'bar'},
199 it('should extend an array', function(){
203 expect(a).to.eql([1,2,4,2,3,4,5]);
204 expect(b).to.eql([2,3,4,5]);
208 it('should append an array', function(){
212 expect(a).to.eql([1,2,4,2,3,4,5]);
213 expect(b).to.eql([2,3,4,5]);
218 it('should combine an array', function(){
219 var arr = [1,2,3,4].combine([3,1,4,5,6,7]);
220 expect(arr).to.eql([1,2,3,4,5,6,7]);
225 it('should include only new items', function(){
226 var arr = [1,2,3,4].include(1).include(5);
227 expect(arr).to.eql([1,2,3,4,5]);
232 it('should return the last item in the array', function(){
233 expect([1,2,3,0,0,0].getLast()).to.equal(0);
234 expect([3].getLast()).to.equal(3);
237 it('should return null if there are no items', function(){
238 expect([].getLast()).to.equal(null);
243 it('should empty the array', function(){
244 var arr = [1,2,3,4].empty();
245 expect(arr).to.eql([]);
250 describe("Array Color Methods", function(){
254 it('should return null if the length of the array is not 3', function(){
255 expect([].hexToRgb()).to.equal(null);
258 it('should return a CSS rgb string', function(){
259 expect(['0','0','0'].hexToRgb()).to.equal('rgb(0,0,0)');
262 it('should support shorthand hex', function(){
263 expect(['c','c','c'].hexToRgb()).to.equal('rgb(204,204,204)');
266 it('should return an array with 16-based numbers when passed true', function(){
267 expect(['ff','ff','ff'].hexToRgb(true)).to.eql([255,255,255]);
272 it('should return null if the array does not have at least 3 times', function(){
273 expect([0,1].rgbToHex()).to.equal(null);
276 it('should return a css hexadecimal string', function(){
277 expect(['255', '0', '0'].rgbToHex()).to.equal('#ff0000');
278 expect([0,0,255].rgbToHex()).to.equal('#0000ff');
281 it('should return an array with hexadecimal string items', function(){
282 expect([0,255,0].rgbToHex(true)).to.eql(['00', 'ff', '00']);
285 it('should return `transparent` if the fourth item is 0 and first param is not true', function(){
286 expect([0,0,0,0].rgbToHex()).to.equal('transparent');
291 describe('Array.getRandom', function(){
293 it('should get a random element from an array', function(){
296 expect(a.getRandom()).to.equal(1);
300 // Let's try a few times
301 expect(a).to.contain(a.getRandom());
302 expect(a).to.contain(a.getRandom());
303 expect(a).to.contain(a.getRandom());
304 expect(a).to.contain(a.getRandom());
309 describe('Array.pick', function(){
311 it('should pick a value that is not null from the array', function(){
312 expect([null, undefined, true, 1].pick()).to.equal(true);
315 it('should return null', function(){
316 expect([].pick()).to.equal(null);
321 describe('Array', function(){
323 describe('Array.map', function(){
325 it('should return an array with the same length', function(){
326 expect([1, 2, 3, undefined].map(function(v){
328 }).length).to.equal(4);
331 it('shoud return an empty array when the thisArg does not has a length property', function(){
332 expect([].map.call({}, function(){
339 it('should accept thisArgs without length property', function(){
340 var object = {}, fn = function(){};
341 expect([].every.call(object, fn)).to.equal(true);
342 expect([].filter.call(object, fn)).to.eql([]);
343 expect([].indexOf.call(object)).to.equal(-1);
344 expect([].map.call(object, fn)).to.eql([]);
345 expect([].some.call(object, fn)).to.equal(false);
348 describe('Array.filter', function(){
350 it('should return the original item, and not any mutations.', function(){
352 var result = [0, 1, 2].filter(function(num, i, array){
354 array[i] = 'mutation';
359 expect(result[0]).to.equal(1);