Skip to content

Commit

Permalink
Merge pull request #2 from awkward/feature-map
Browse files Browse the repository at this point in the history
Added mapping helpers to NSArray
  • Loading branch information
renssies committed Mar 18, 2015
2 parents b3dc1e2 + 15ad754 commit 37baa06
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion AWKHelpers.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "AWKHelpers"
s.version = "0.5"
s.version = "0.6"
s.summary = "A growing collection of UIKit and Foundation categories (helpers)"
s.description = <<-DESC
A growing collection of UIKit and Foundation categories (helpers) we use at Awkward
Expand Down
24 changes: 24 additions & 0 deletions Classes/AWKArrayHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

#import <Foundation/Foundation.h>

typedef NS_OPTIONS(NSUInteger, AWKArrayMappingOptions) {
AWKArrayMapNilToNull = 1<<0,
AWKArrayMapNilToDeletion = 1<<1
};

@interface NSArray (AWKArrayHelper)

/**
Expand All @@ -33,6 +38,16 @@
*/
- (BOOL)hasObjectAtIndex:(NSUInteger)index;

/**
Creates a new array with new objects mapped to the values of the receicer array. The new values should be defined by the parameter block.
@param mapBlock A block to return the destination values for the receiver array values. By default if you return nil, the new object will be copied from the receiver, unless you use a mapping option.
@param options Mapping options
@return A newly created array with modified values based on the receiver array.
*/
- (NSArray *)arrayByMappingObjectsWithBlock:(id (^)(id obj, NSUInteger idx))mapBlock options:(AWKArrayMappingOptions)options;

@end

@interface NSMutableArray (AWKArrayHelper)
Expand All @@ -42,4 +57,13 @@
*/
- (void)reverse;

/**
Replaces all objects of the array with other objects, returned by the given mapping block.
@param mapBlock A block to return the new object to replace the old object in the array. By default, returning nil will result in an unchanged object, unless you use a mapping option.
@param options Mapping options
*/
- (void)replaceObjectsWithBlock:(id (^)(id obj, NSUInteger idx))mapBlock options:(AWKArrayMappingOptions)options;

@end
36 changes: 36 additions & 0 deletions Classes/AWKArrayHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ - (BOOL)hasObjectAtIndex:(NSUInteger)index {
return YES;
}

- (NSArray *)arrayByMappingObjectsWithBlock:(id (^)(id obj, NSUInteger idx))mapBlock options:(AWKArrayMappingOptions)options {
NSAssert(mapBlock!=nil, @"Map block should not be nil");
NSMutableArray *mappedArray = [[NSMutableArray alloc] initWithCapacity:self.count];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id result = mapBlock(obj, idx);
if (result) {
[mappedArray addObject:result];
} else if (options&AWKArrayMapNilToNull) {
[mappedArray addObject:[NSNull null]];
} else if ((options&AWKArrayMapNilToDeletion) == 0) {
[mappedArray addObject:obj];
}
}];
NSArray *resultArray = [mappedArray copy];
return resultArray;
}

@end

@implementation NSMutableArray (AWKArrayHelper)
Expand All @@ -61,4 +78,23 @@ - (void)reverse {
}
}

- (void)replaceObjectsWithBlock:(id (^)(id obj, NSUInteger idx))mapBlock options:(AWKArrayMappingOptions)options{
NSAssert(mapBlock!=nil, @"Map block should not be nil");
NSMutableIndexSet *deletedIndices = [[NSMutableIndexSet alloc] init];
for (NSUInteger idx = 0; idx < self.count; idx++) {
id oldObject = self[idx];
id newObject = mapBlock(oldObject, idx);
if (newObject) {
[self replaceObjectAtIndex:idx withObject:newObject];
} else if (options&AWKArrayMapNilToNull) {
[self replaceObjectAtIndex:idx withObject:[NSNull null]];
} else if (options&AWKArrayMapNilToDeletion) {
[deletedIndices addIndex:idx];
}
}
if (deletedIndices.count > 0) {
[self removeObjectsAtIndexes:deletedIndices];
}
}

@end

0 comments on commit 37baa06

Please sign in to comment.