Skip to content

Commit

Permalink
deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Nov 13, 2015
1 parent 8f6136d commit 59a7be3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
56 changes: 51 additions & 5 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -1203,16 +1203,22 @@ package com.kemsky
* </pre>
* @internal immutable
*/
//todo nullable property
public function dictionary(property:String, weak:Boolean = false):Dictionary
public function dictionary(property:String = null, weak:Boolean = false):Dictionary
{
var dict:Dictionary = new Dictionary(weak);
for each (var item:* in source)
{
if(item.hasOwnProperty(property))
if(property != null && property.length > 0)
{
var value:* = item[property];
dict[value] = item;
if(item.hasOwnProperty(property))
{
var value:* = item[property];
dict[value] = item;
}
}
else
{
dict[item] = item;
}
}
return dict;
Expand Down Expand Up @@ -1320,6 +1326,46 @@ package com.kemsky
}
}

/**
* Create new stream from original by removing duplicate items.
* @param callback is compare function <i>function(a:*, b:*):int</i>.
* @return new stream without duplicate values.
* @example
* <pre>
* var s:Stream = $(1, 2, 3, 1);
* trace(s.deduplicate());
* //Stream{1, 2, 3}
* </pre>
* @internal immutable
*/
public function deduplicate(callback:Function = null):Stream
{
var result:Stream;
if(callback == null)
{
var map:Dictionary = this.dictionary();
result = Stream.from(map);
}
else
{
//todo
result = this.concat();
var i:int;
var j:int;
for(i = 0; i < length - 1; i++)
{
for(j = i + 1; j < length; j++)
{
if(callback(result.getItem(i), result.getItem(j)) == 0)
{
result.removeItem(j);
}
}
}
}
return result;
}

/*
* Array part
*/
Expand Down
14 changes: 14 additions & 0 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ package com.kemsky
{
}


[Test]
public function testDeduplicate():void
{
var s:Stream = $(1, 2, 4, 3, 2, 1);

var d1:Stream = s.deduplicate();
//assertEquals(d1.length, 4);
}

[Test]
public function testEntries():void
{
Expand Down Expand Up @@ -428,6 +438,10 @@ package com.kemsky
var d:Dictionary = s.dictionary("name");
assertEquals(d["1"], item1);
assertEquals(d["2"], item2);

var v:Dictionary = $(item1, item2, item1).dictionary();
assertEquals(v[item1], item1);
assertEquals(v[item2], item2);
}

[Test]
Expand Down

0 comments on commit 59a7be3

Please sign in to comment.