diff --git a/asx/src/asx/array/inGroupsOf.as b/asx/src/asx/array/inGroupsOf.as index 2ff4bbd..ff8ff44 100644 --- a/asx/src/asx/array/inGroupsOf.as +++ b/asx/src/asx/array/inGroupsOf.as @@ -1,27 +1,27 @@ package asx.array { /** - * + * */ public function inGroupsOf(iterable:Object, size:int):Array { - var i:int = 0; - var group:Array = []; - var groups:Array = [group]; - - for each (var item:Object in iterable) + var i:int = size; + var group:Array; + var groups:Array = []; + + for each(var item:Object in iterable) { - group[group.length] = item; - i++; - - if (i == size) + if( i == size ) { - i = 0; + i = 0; group = []; groups[groups.length] = group; } - } + group[group.length] = item; + i++; + } + return groups; } -} \ No newline at end of file +} diff --git a/asx_test/src/asx/array/InGroupsOfTest.as b/asx_test/src/asx/array/InGroupsOfTest.as index 65afc43..f7e1554 100644 --- a/asx_test/src/asx/array/InGroupsOfTest.as +++ b/asx_test/src/asx/array/InGroupsOfTest.as @@ -1,6 +1,7 @@ package asx.array { import org.flexunit.assertThat; + import org.hamcrest.collection.array; import org.hamcrest.object.equalTo; public class InGroupsOfTest @@ -11,5 +12,19 @@ package asx.array var values:Array = [1, 2, 3, 4, 5, 6, 7]; assertThat(inGroupsOf(values, 3), equalTo([[1, 2, 3], [4, 5, 6], [7]])); } + + [Test] + public function inGroupsOf_withArray_evenLength_shouldSplitItemsIntoArraysOfGivenSize():void + { + var values:Array = [1, 2, 3, 4, 5, 6]; + assertThat(inGroupsOf(values, 3), equalTo([[1, 2, 3], [4, 5, 6]])); + } + + [Test] + public function inGroupsOf_withArray_shouldSplitItemsIntoArraysOfGivenSize_1():void + { + var values:Array = [1, 2, 3, 4, 5, 6]; + assertThat(inGroupsOf(values, 1), equalTo([[1], [2], [3], [4], [5], [6]])); + } } } \ No newline at end of file