diff --git a/Underscore/USArrayWrapper.h b/Underscore/USArrayWrapper.h index 4c8162b..5c0bd2b 100644 --- a/Underscore/USArrayWrapper.h +++ b/Underscore/USArrayWrapper.h @@ -61,6 +61,9 @@ @property (readonly) USArrayWrapper *uniq; +@property (readonly) id (^min)(UnderscoreValueBlock block); +@property (readonly) id (^max)(UnderscoreValueBlock block); + @property (readonly) id (^find)(UnderscoreTestBlock block); @property (readonly) USArrayWrapper *(^filter)(UnderscoreTestBlock block); diff --git a/Underscore/USArrayWrapper.m b/Underscore/USArrayWrapper.m index 0d5ae4e..3cc9f63 100644 --- a/Underscore/USArrayWrapper.m +++ b/Underscore/USArrayWrapper.m @@ -258,6 +258,45 @@ - (USArrayWrapper *)uniq return [[USArrayWrapper alloc] initWithArray:[set array]]; } +- (id (^)(UnderscoreValueBlock))min +{ + return ^id (UnderscoreValueBlock test) { + + if( !test ) return [self.array valueForKeyPath:@"@min.doubleValue"]; + + id result = nil; + NSNumber *minValue = nil; + for (id obj in self.array) { + NSNumber *value = test(obj); + + if( !minValue ) minValue = value, result = obj; + else if( [minValue compare:value] == NSOrderedDescending ) minValue = value, result = obj; + } + + return result; + }; +} + +- (id (^)(UnderscoreValueBlock))max +{ + return ^id (UnderscoreValueBlock test) { + + if( !test ) return [self.array valueForKeyPath:@"@max.doubleValue"]; + + id result = nil; + NSNumber *maxValue = nil; + for (id obj in self.array) { + NSNumber *value = test(obj); + + if( !maxValue ) maxValue = value, result = obj; + else if( [maxValue compare:value] == NSOrderedAscending ) maxValue = value, result = obj; + } + + return result; + }; +} + + - (id (^)(UnderscoreTestBlock))find { return ^id (UnderscoreTestBlock test) { diff --git a/Underscore/USConstants.h b/Underscore/USConstants.h index 0eaba66..f0d6bd1 100644 --- a/Underscore/USConstants.h +++ b/Underscore/USConstants.h @@ -26,6 +26,8 @@ #import + +typedef id (^UnderscoreValueBlock)(id obj); typedef BOOL (^UnderscoreTestBlock)(id obj); typedef id (^UnderscoreReduceBlock)(id memo, id obj); diff --git a/Underscore/Underscore+Functional.m b/Underscore/Underscore+Functional.m index fb9612b..1a8d61a 100644 --- a/Underscore/Underscore+Functional.m +++ b/Underscore/Underscore+Functional.m @@ -149,6 +149,13 @@ @implementation Underscore (Functional) }; } ++ (id (^)(NSArray *, UnderscoreValueBlock))max +{ + return ^(NSArray *array, UnderscoreValueBlock block) { + return Underscore.array(array).max(block); + }; +} + + (id (^)(NSArray *, UnderscoreTestBlock))find { return ^(NSArray *array, UnderscoreTestBlock block) { @@ -162,6 +169,7 @@ @implementation Underscore (Functional) return Underscore.array(array).filter(block).unwrap; }; } + + (NSArray *(^)(NSArray *, UnderscoreTestBlock))reject { return ^(NSArray *array, UnderscoreTestBlock block) { diff --git a/UnderscoreTests/USArrayTest.m b/UnderscoreTests/USArrayTest.m index 718a075..e86f335 100644 --- a/UnderscoreTests/USArrayTest.m +++ b/UnderscoreTests/USArrayTest.m @@ -643,4 +643,43 @@ - (void)testSort _.array(notSorted).sort(numericalSort).unwrap); } +- (void)testMin +{ + NSDictionary *dict = @{@"name": @"mark", @"value" : @2}; + + NSArray *dictArray = @[ @{@"name": @"python", @"value" : @3}, @{@"name": @"monthy", @"value" : @3.3}, @{@"name": @"mark", @"value" : @2} ]; + NSArray *numbersArray = @[ @100, @3.3, @3 ]; + + UnderscoreValueBlock minBlock1 = ^id(NSDictionary *dict){ return dict[@"value"]; }; + UnderscoreValueBlock minBlock2 = nil; + + + STAssertEqualObjects(_.array(dictArray).min(minBlock1), + dict, + @"Can find min element"); + + STAssertEqualObjects(_.array(numbersArray).min(minBlock2), + @3, + @"Can find min element"); +} + +- (void)testMax +{ + NSDictionary *dict = @{@"name": @"monthy", @"value" : @3.3}; + + NSArray *dictArray = @[ @{@"name": @"python", @"value" : @3}, @{@"name": @"monthy", @"value" : @3.3}, @{@"name": @"mark", @"value" : @2} ]; + NSArray *numbersArray = @[ @100, @3.3, @3 ]; + + UnderscoreValueBlock maxBlock1 = ^id(NSDictionary *dict){ return dict[@"value"]; }; + UnderscoreValueBlock maxBlock2 = nil; + + STAssertEqualObjects(_.array(dictArray).max(maxBlock1), + dict, + @"Can find max element"); + + STAssertEqualObjects(_.array(numbersArray).max(maxBlock2), + @100, + @"Can find max element"); +} + @end