Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
amacdougall committed May 7, 2013
2 parents 61bb2a4 + 0e90c6b commit 8ad79b5
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,5 +1,5 @@
*~
*.swp
*.sw[a-p]
.DS_Store
test.swf
test.swf.cache
6 changes: 5 additions & 1 deletion flexUnitTests/compile.sh
@@ -1,7 +1,11 @@
/usr/local/flex_sdk_4.5.1.21328/bin/mxmlc \
mxmlc \
-static-link-runtime-shared-libraries=true \
-debug=true \
-output=build/test.swf \
-library-path+=./libs \
-source-path+=./src \
src/TestContainer.mxml

# my personal mxmlc location -- @amacdougall
# /usr/local/flex_sdk_4.5.1.21328/bin/mxmlc \
#
Expand Up @@ -173,8 +173,8 @@ public class CollectionsTestCase {
_([1, 2, 3]).all(function(n:int):Boolean {return n > 0;}));
Assert.assertFalse("Invalid true result from all() test.",
_([1, 2, 3]).all(function(n:int):Boolean {return n < 1;}));
Assert.assertFalse("Invalid true result from all() test on empty collection.",
_([]).all(function(n:int):Boolean {return true;}));
Assert.assertTrue("Invalid false result from all() test on empty collection.",
_([]).all(function(n:int):Boolean {return false;}));
}

[Test]
Expand Down Expand Up @@ -223,20 +223,32 @@ public class CollectionsTestCase {
public function testMax():void {
var numbers:Array = [1, 4, 3, 2];
var lists:Array = [[1, 2, 3], [1, 2], [3, 4, 5], [4, 5, 6, 7]];
var dates:Array = [
new Date(1776, 07, 04), // American independence
new Date(1949, 10, 01), // Mao declares PRC
new Date(1789, 07, 14) // Bastille Day
];

Assert.assertEquals("Failed to get max number.", 4, _(numbers).max());
Assert.assertEquals("Failed to get longest list using length test.",
4, _(lists).max(function(list:Array):Number {return list.length;}).length);
Assert.assertEquals("Failed to get most recent date.", dates[1], _(dates).max());
}

[Test]
public function testMin():void {
var numbers:Array = [1, 4, 3, 2];
var lists:Array = [[1, 2, 3], [1, 2], [3, 4, 5], [4, 5, 6, 7]];
var dates:Array = [
new Date(1776, 07, 04),
new Date(1949, 10, 01),
new Date(1789, 07, 14)
];

Assert.assertEquals("Failed to get min number.", 1, _(numbers).min());
Assert.assertEquals("Failed to get shortest list using length test.",
2, _(lists).min(function(list:Array):Number {return list.length;}).length);
Assert.assertEquals("Failed to get least recent date.", dates[0], _(dates).min());
}

[Test]
Expand Down
12 changes: 7 additions & 5 deletions src/com/alanmacdougall/underscore/_.as
Expand Up @@ -202,7 +202,7 @@ public var _:* = (function():Function {
* iterator is omitted, tests the elements themselves for truthiness.
*/
_.all = function(obj:*, iterator:Function = null, context:Object = null):Boolean {
if (obj == null || _(obj).isEmpty()) return false;
if (obj == null) return false;
// TO DO: benchmark native Array.every
iterator = iterator || identity;
var result:Boolean = true;
Expand Down Expand Up @@ -265,7 +265,9 @@ public var _:* = (function():Function {
* Returns the maximum value in the collection. If an iterator is passed,
* it must return a numeric value for each element. Otherwise the element
* itself will be compared using gt/lt operators, with undefined results if
* the values are non-numeric.
* the values cannot be compared. Special cases: Arrays will be compared by
* length; Dates will be compared by their millisecond position in the Unix
* epoch, i.e. Date.getTime().
*/
_.max = function(obj:*, iterator:Function = null, context:Object = null):* {
// unlike in underscore.js, "value" means numeric value, "element" is the real item
Expand All @@ -274,7 +276,7 @@ public var _:* = (function():Function {
each (obj, function(element:*, index:*, list:*):void {
var value:Number = iterator != null ?
safeCall(iterator, context, element, index, list) :
element;
(element is Date ? element.getTime() : element);
if (value >= maxValue) {
maxValue = value;
maxElement = element;
Expand All @@ -296,7 +298,7 @@ public var _:* = (function():Function {
each (obj, function(element:*, index:*, list:*):void {
var value:Number = iterator != null ?
safeCall(iterator, context, element, index, list) :
element;
(element is Date ? element.getTime() : element);
if (value <= minValue) {
minValue = value;
minElement = element;
Expand Down Expand Up @@ -439,7 +441,7 @@ public var _:* = (function():Function {
_.without = function(list:Array, ...targets):Array {
return _(list).reject(function(element:*):Boolean {
return _(targets).any(function(target:*):Boolean {
return element == target;
return element === target;
});
});
};
Expand Down

0 comments on commit 8ad79b5

Please sign in to comment.