Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions asx/src/asx/array/inGroupsOf.as
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
15 changes: 15 additions & 0 deletions asx_test/src/asx/array/InGroupsOfTest.as
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package asx.array
{
import org.flexunit.assertThat;
import org.hamcrest.collection.array;
import org.hamcrest.object.equalTo;

public class InGroupsOfTest
Expand All @@ -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]]));
}
}
}