Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Underscore/USArrayWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions Underscore/USArrayWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Underscore/USConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#import <Foundation/Foundation.h>


typedef id (^UnderscoreValueBlock)(id obj);
typedef BOOL (^UnderscoreTestBlock)(id obj);
typedef id (^UnderscoreReduceBlock)(id memo, id obj);

Expand Down
8 changes: 8 additions & 0 deletions Underscore/Underscore+Functional.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -162,6 +169,7 @@ @implementation Underscore (Functional)
return Underscore.array(array).filter(block).unwrap;
};
}

+ (NSArray *(^)(NSArray *, UnderscoreTestBlock))reject
{
return ^(NSArray *array, UnderscoreTestBlock block) {
Expand Down
39 changes: 39 additions & 0 deletions UnderscoreTests/USArrayTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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