Skip to content
This repository was archived by the owner on Dec 12, 2018. 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
8 changes: 8 additions & 0 deletions Classes/NSArray+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@
*/
- (NSArray *)reject:(BOOL (^)(id object))block;

/**
Iterate through current array adding the element to a dictionary asking which key to use.

@param A block that returns the key that should be used to add the element to the dictionary
@return A dictionary with the elements grouped by the key
*/
- (NSDictionary *)groupBy:(id (^)(id object))key;

/**
Recurse through self checking for NSArrays and extract all elements into one single array

Expand Down
11 changes: 11 additions & 0 deletions Classes/NSArray+ObjectiveSugar.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ - (NSArray *)reject:(BOOL (^)(id object))block {
}]];
}

- (NSDictionary *)groupBy:(id (^)(id object))key {
return [self reduce:[NSMutableDictionary dictionary] withBlock:^id(NSMutableDictionary *accumulator, id object) {
if (!accumulator[key(object)]) {
accumulator[key(object)] = [NSMutableArray array];
}
[accumulator[key(object)] addObject:object];
return accumulator;
}];
}


- (id)detect:(BOOL (^)(id object))block {
for (id object in self) {
if (block(object))
Expand Down
10 changes: 10 additions & 0 deletions Example/ObjectiveSugarTests/NSArrayTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@
return @(accumulator.intValue + numbah.intValue);
}] should] equal:@60];
});

it(@"-groupBy: returns a dictionary grouped with the specified keys", ^{
NSDictionary *groupedByPairs = [oneToTen groupBy:^id(NSNumber *number) {
return @([number intValue] % 2 == 0);
}];
[[groupedByPairs[@YES] should] containObjects:@2, @4, @6, @8, @10, nil];
[[groupedByPairs[@YES] shouldNot] containObjects:@1, @3, @5, @7, @9, nil];
[[groupedByPairs[@NO] should] containObjects:@1, @3, @5, @7, @9, nil];
[[groupedByPairs[@NO] shouldNot] containObjects:@2, @4, @6, @8, @10, nil];
});

it(@"-unique produces a duplicate-free array", ^{
NSArray *arrayWithDuplicates = @[@1, @1, @"something", @"something"];
Expand Down
10 changes: 5 additions & 5 deletions Example/ObjectiveSugarTests/NSStringTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

it(@"NSStringWithFormat makes NSString -stringWithFormat", ^{

[[NSStringWithFormat(@"This is %@", @1) should] equal:@"This is 1"];
[[[NSString stringWithFormat:@"This is %@", @1] should] equal:@"This is 1"];
});

});
Expand Down Expand Up @@ -55,14 +55,14 @@

context(@"lowerCamelCase", ^{
it(@"converts snake_case to snakeCase", ^{
[[[@"snake_case" lowerCamelCase] should] equal:@"snakeCase"]
[[[@"snake_case" lowerCamelCase] should] equal:@"snakeCase"];
});

it(@"handles extraneous underscores", ^{
[[[@"_snake_case" lowerCamelCase] should] equal:@"snakeCase"]
[[[@"snake_case_" lowerCamelCase] should] equal:@"snakeCase"]
[[[@"_snake_case" lowerCamelCase] should] equal:@"snakeCase"];
[[[@"snake_case_" lowerCamelCase] should] equal:@"snakeCase"];
});
}
});

it(@"-strip strips whitespaces and newlines from both ends", ^{
[[[@"\n Look mo, no empties!\n \n\n " strip] should] equal:@"Look mo, no empties!"];
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ numbers[range];
[numbers reverse];
// [@6, @5, @4, @3, @2, @1]

// group pair numbers
[numbers groupBy:^id(NSNumber *number) {
return @([number intValue] % 2 == 0);
}];
// @{@NO : @[@1, @3, @5],
// @YES : @[@2, @4, @6]}


NSArray *fruits = @[ @"banana", @"mango", @"apple", @"pear" ];

Expand Down