Skip to content

Commit

Permalink
improve either filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Feb 9, 2016
1 parent 7405ced commit 1fa4441
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 6 additions & 4 deletions source/com/kemsky/filters/either.as
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ package com.kemsky.filters
{
import com.kemsky.$;
import com.kemsky.Stream;
import com.kemsky.support.Compare;
import com.kemsky.support.StreamError;
import com.kemsky.support.toValue;

/**
* Creates function that checks if value equals to one of the arguments
* @param value item or item property
* @param rest arguments
* @param enumeration arguments (any iterable type supported by stream)
* @param options comparison options
* @return function that checks if value equals to one of the arguments
*/
public function either(value:*, ...rest):Function
public function either(value:*, enumeration:*, options:uint = 0):Function
{
var values:Stream = $.apply(null, rest);
var values:Stream = $(enumeration);

if (values.length == 0)
{
Expand All @@ -32,7 +34,7 @@ package com.kemsky.filters

var result:Boolean = values.some(function (val2:*):Boolean
{
return val == toValue(item, val2);
return Compare.compare(val, toValue(item, val2), options, true) == 0;
});

return result;
Expand Down
8 changes: 8 additions & 0 deletions source/com/kemsky/support/Compare.as
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ package com.kemsky.support
{
result = 1;
}
else if(a === b)
{
result = 0;
}
else
{
var numeric:Boolean = (options & Stream.NUMERIC) == Stream.NUMERIC;
Expand Down Expand Up @@ -125,6 +129,10 @@ package com.kemsky.support
{
return UNDEFINED;
}
else if (object is Stream)
{
cls = Stream;
}
else if (object != null)
{
cls = object.constructor;
Expand Down
10 changes: 6 additions & 4 deletions testSrc/com/kemsky/filters/TestFilters.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package com.kemsky.filters

import flash.utils.Dictionary;

import mx.collections.ArrayCollection;

import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertFalse;
import org.flexunit.asserts.assertTrue;
Expand Down Expand Up @@ -415,14 +417,14 @@ package com.kemsky.filters
public function testEither():void
{
var s:Stream = $(1, 2, 3);
assertEquals(s.count(either(_, 1, 2)), 2);

assertEquals(s.count(either(_, [1, 2])), 2);
assertEquals(s.count(either(_, [4])), 0);
assertEquals(s.count(either(_, [1, 2])), 2);
assertEquals(s.count(either(_, $(4))), 0);
assertEquals(s.count(either(_, new ArrayCollection([4]))), 0);

try
{
s.count(either(_));
s.count(either(_, []));
assertFalse(true);
}
catch (e:Error)
Expand Down

0 comments on commit 1fa4441

Please sign in to comment.