Skip to content

Commit

Permalink
add groupBy method
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Jan 21, 2016
1 parent 099602e commit 39add42
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
75 changes: 56 additions & 19 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,43 @@ package com.kemsky
return dict;
}

/**
* Creates new Object from the current list using specified property as keys
* @param property callback to map item to object property name
* @param value callback to map item to property value
* @return A new Object from current Stream using specified property as keys
* @example
* <pre>
* var item1:Object = {id: 1, key: "key1"};
* var item2:Object = {id: 2, key: "key1"};
* var item3:Object = {id: 3, key: "key2"};
* var s:Stream = $(item1, item2, item3);
* var d:Object = s.object(member("key"));
* trace(d["key1"]);
* //Stream{item1, item2}
* trace(d["key2"]);
* //Stream{item3}
* </pre>
* @internal immutable
*/
public function object(property:Function, value:Function = null):Object
{
var dict:Object = {};

if(value == null)
{
value = _;
}

for each (var item:* in source)
{
var key:* = property(item);
dict[key] = value(item);
}

return dict;
}

/**
* Groups items by key obtained via callback.
* @param key The function to calculate key from item: function(item:*):*
Expand Down Expand Up @@ -1365,40 +1402,40 @@ package com.kemsky
}

/**
* Creates new Object from the current list using specified property as keys
* @param property callback to map item to object property name
* @param value callback to map item to property value
* @return A new Object from current Stream using specified property as keys
* Groups items by key obtained via callback.
* @param key The function to calculate key from item: function(item:*):*
* @return A new Stream which contains groups.
* @example
* <pre>
* var item1:Object = {id: 1, key: "key1"};
* var item2:Object = {id: 2, key: "key1"};
* var item3:Object = {id: 3, key: "key2"};
* var s:Stream = $(item1, item2, item3);
* var d:Object = s.object(member("key"));
* trace(d["key1"]);
* var d:Stream = s.groupBy("key");
* trace(d.first);
* //Stream{item1, item2}
* trace(d["key2"]);
* trace(d.second);
* //Stream{item3}
* </pre>
* @internal immutable
*/
public function object(property:Function, value:Function = null):Object
public function groupBy(key:Function):Stream
{
var dict:Object = {};

if(value == null)
{
value = _;
}

var result:Stream = new Stream();
var dict:Dictionary = new Dictionary();
for each (var item:* in source)
{
var key:* = property(item);
dict[key] = value(item);
var name:* = key(item);
var s:Stream = dict[name];
if(s == null)
{
s = new Stream();
dict[name] = s;
result.push(s);
}
s.push(item);
}

return dict;
return result;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,24 @@ package com.kemsky
assertEquals(g2.third, 6);
}

[Test]
public function testGroupBy():void
{
var s:Stream = $(1, 2, 3, 4, 5, 6);
var groups:Stream = s.groupBy(function (item:Number):Number
{
return item > 3 ? 2 : 1;
});

var g1:Stream = groups.first;
assertEquals(g1.length, 3);
assertEquals(g1.third, 3);

var g2:Stream = groups.second;
assertEquals(g2.length, 3);
assertEquals(g2.third, 6);
}

[Test]
public function testCount():void
{
Expand Down

0 comments on commit 39add42

Please sign in to comment.