Skip to content

Commit

Permalink
trim and empty
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Feb 16, 2016
1 parent 800a6f4 commit bc4100a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion source/com/kemsky/Iterator.as
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ package com.kemsky
_next--;
}

stream.removeItem(_current);
stream.removeItemAt(_current);
_current = -1;
}

Expand Down
33 changes: 28 additions & 5 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,13 @@ package com.kemsky
* @example
* <pre>
* var s:Stream = $(1, 2, 3);
* s.removeItem(0);
* s.removeItemAt(0);
* trace(s);
* //Stream{2, 3}
* </pre>
* @internal mutable
*/
public function removeItem(index:int):Boolean
public function removeItemAt(index:int):Boolean
{
if(index < 0)
{
Expand All @@ -616,6 +616,29 @@ package com.kemsky
}
}

/**
* Removes item (using strict equals ===).
* @param item target item.
* @return true if remove was successful.
* @example
* <pre>
* var s:Stream = $(1, 2, 3);
* s.removeItem(1);
* trace(s);
* //Stream{2, 3}
* </pre>
* @internal mutable
*/
public function removeItem(item:*):Boolean
{
var index:int = source.indexOf(item);
if(index >= 0)
{
return (source.splice(index, 1) as Array).length > 0;
}
return false;
}

/**
* Executes a test function on each item and calculates number of successful tests.
* @param callback The function to run on each item in the list.
Expand Down Expand Up @@ -1591,7 +1614,7 @@ package com.kemsky
{
if(callback(result.getItem(i), result.getItem(j)))
{
result.removeItem(j);
result.removeItemAt(j);
}
}
}
Expand All @@ -1604,7 +1627,7 @@ package com.kemsky
{
if(result.getItem(i) === result.getItem(j))
{
result.removeItem(j);
result.removeItemAt(j);
}
}
}
Expand Down Expand Up @@ -2304,7 +2327,7 @@ package com.kemsky
return false;
}

return removeItem(index);
return removeItemAt(index);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -517,21 +517,33 @@ package com.kemsky
assertTrue(s.getItem(3) === undefined);
}


[Test]
public function testRemove():void
{
var s:Stream = $(1, 2, 3);
s.removeItem(0);
s.removeItem(1);
s.removeItem(3);
assertEquals(s.length, 1);
assertEquals(s[0], 2);

var result:Boolean = new Stream().removeItem(1);
assertEquals(result, false);
}

[Test]
public function testRemoveAt():void
{
var s:Stream = $(1, 2, 3);
s.removeItemAt(0);
s.removeItemAt(1);
assertEquals(s.length, 1);
assertEquals(s[0], 2);

var result:Boolean = s.removeItem(-1);
var result:Boolean = s.removeItemAt(-1);
assertEquals(s.length, 0);
assertEquals(result, true);

assertEquals(s.removeItem(0), false);
assertEquals(s.removeItemAt(0), false);
}

[Test]
Expand Down

0 comments on commit bc4100a

Please sign in to comment.