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
15 changes: 15 additions & 0 deletions Classes/NSArray+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@
*/
- (NSArray *)reject:(BOOL (^)(id object))block;

/**
Iterate through current array and evaluate the condition on each element

@param A block that returns YES/NO
*/

- (BOOL)all:(BOOL (^)(id object))condition;

/**
Iterate through the current array and return true if element matches the condition given in the block

@param A block that returns YES/NO
*/

-(BOOL)any:(BOOL(^)(id object))condition;
/**
Recurse through self checking for NSArrays and extract all elements into one single array

Expand Down
18 changes: 18 additions & 0 deletions Classes/NSArray+ObjectiveSugar.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ - (NSArray *)select:(BOOL (^)(id object))block {
return array;
}

-(BOOL)all:(BOOL (^)(id))condition {

for (id object in self) {
if (!condition(object)) {
return false;
}
}
return true;
}

-(BOOL)any:(BOOL (^)(id))condition {
for (id object in self) {
if (condition(object)) {
return true;
}
}
return false;
}
- (id)detect:(BOOL (^)(id object))block {

for (id object in self) {
Expand Down
3 changes: 2 additions & 1 deletion Classes/NSSet+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
- (NSArray *)select:(BOOL (^)(id object))block;
- (NSArray *)reject:(BOOL (^)(id object))block;
- (NSArray *)map:(id (^)(id object))block;

- (BOOL)all:(BOOL (^)(id object))condition;
- (BOOL)any:(BOOL (^)(id object))condition;
- (NSArray *)sort;

@end
20 changes: 20 additions & 0 deletions Classes/NSSet+ObjectiveSugar.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ - (NSArray *)select:(BOOL (^)(id object))block {
return array;
}

-(BOOL)all:(BOOL (^)(id))condition {

for (id object in self) {
if (!condition(object)) {
return false;
}
}
return true;
}

-(BOOL)any:(BOOL (^)(id))condition {

for (id object in self) {
if (condition(object)) {
return true;
}
}
return false;
}

- (NSArray *)reject:(BOOL (^)(id object))block {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];

Expand Down

This file was deleted.

8 changes: 7 additions & 1 deletion Example/ObjectiveSugarTests/NSArrayCategoriesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
[emptyArray.sample shouldBeNil];
});


context(@"Iterating using block", ^{

it(@"iterates using -each:^", ^{
Expand Down Expand Up @@ -97,7 +98,12 @@
return [object integerValue] % 3 == 0;
}] should] equal:@[ @3, @6, @9 ]];
});

it(@"-all returns true if all elements match the condition in the block", ^{
[[@([sampleArray all:^BOOL(id object) {
return object!=nil;
}]) should] equal:@(YES)];
});

it(@"-detect returns the first element in NSArray for which block is true", ^{
[[[oneToTen detect:^BOOL(id object) {
return [object intValue] % 3 == 0;
Expand Down
6 changes: 5 additions & 1 deletion Example/ObjectiveSugarTests/NSSetTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
}];
[[duplicate should] beEmpty];
});

it(@"-all returns true if all elements match the condition in the block", ^{
[[@([sampleSet all:^BOOL(id object) {
return object!=nil;
}]) should] equal:@(YES)];
});
});

context(@"first, last, sample", ^{
Expand Down