Permalink
Browse files

Added NSDictionary filtering/mapping

  • Loading branch information...
1 parent 0bee516 commit bc2493ebf51c9e3d9ab44a2ebf09d75b48902a92 @stevestreza committed Oct 26, 2012
@@ -14,6 +14,7 @@
1EABBD6316223801001B9331 /* NSObject+Derp.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EABBD6216223801001B9331 /* NSObject+Derp.m */; };
1EABBD6516224065001B9331 /* NSObject+Derp.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1EABBD6116223801001B9331 /* NSObject+Derp.h */; };
1EC132C4163A693200CCF530 /* NSArray+Derp.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EC132C3163A693200CCF530 /* NSArray+Derp.m */; };
+ 1EC1AB1C163A70DE008A6B50 /* NSDictionary+Derp.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EC1AB1B163A70DE008A6B50 /* NSDictionary+Derp.m */; };
1ED3F1C515B2AD3B0011D729 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1ED3F1C415B2AD3B0011D729 /* Foundation.framework */; };
1ED3F1CA15B2AD3B0011D729 /* DerpKit.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1ED3F1C915B2AD3B0011D729 /* DerpKit.h */; };
1ED3F1D415B2AD3C0011D729 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1ED3F1D315B2AD3C0011D729 /* SenTestingKit.framework */; };
@@ -61,6 +62,8 @@
1EC132C2163A693200CCF530 /* NSArray+Derp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Derp.h"; sourceTree = "<group>"; };
1EC132C3163A693200CCF530 /* NSArray+Derp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Derp.m"; sourceTree = "<group>"; };
1EC1AB14163A6BE3008A6B50 /* Foundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Foundation.h; path = DerpKit/Foundation/Foundation.h; sourceTree = SOURCE_ROOT; };
+ 1EC1AB1A163A70DE008A6B50 /* NSDictionary+Derp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+Derp.h"; sourceTree = "<group>"; };
+ 1EC1AB1B163A70DE008A6B50 /* NSDictionary+Derp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+Derp.m"; sourceTree = "<group>"; };
1ED3F1C115B2AD3B0011D729 /* libDerpKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libDerpKit.a; sourceTree = BUILT_PRODUCTS_DIR; };
1ED3F1C415B2AD3B0011D729 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1ED3F1C815B2AD3B0011D729 /* DerpKit-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "DerpKit-Prefix.pch"; sourceTree = "<group>"; };
@@ -205,6 +208,8 @@
1ED3F1F415B2AE760011D729 /* NSData+Derp.m */,
1EC132C2163A693200CCF530 /* NSArray+Derp.h */,
1EC132C3163A693200CCF530 /* NSArray+Derp.m */,
+ 1EC1AB1A163A70DE008A6B50 /* NSDictionary+Derp.h */,
+ 1EC1AB1B163A70DE008A6B50 /* NSDictionary+Derp.m */,
);
path = Categories;
sourceTree = "<group>";
@@ -312,6 +317,7 @@
1E11A54A15B331F100500EBD /* UIViewController+Derp.m in Sources */,
1EC132C4163A693200CCF530 /* NSArray+Derp.m in Sources */,
1EABBD6316223801001B9331 /* NSObject+Derp.m in Sources */,
+ 1EC1AB1C163A70DE008A6B50 /* NSDictionary+Derp.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -47,14 +47,14 @@ -(NSArray *)derp_arrayByMappingWithHandler:(id (^)(id object, NSUInteger index,
}
-(NSArray *)derp_subarrayByFilteringWithHandler:(BOOL (^)(id object, NSUInteger index, BOOL *stop))handler{
- NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];
+ NSMutableArray *array = [self mutableCopy];
BOOL stop = NO;
for(NSUInteger index = 0; index < self.count; index++){
id object = self[index];
BOOL success = handler(object, index, &stop);
- if(success){
- [array addObject:object];
+ if(!success){
+ [array removeObject:object];
}
if(stop){
break;
@@ -0,0 +1,16 @@
+//
+// NSDictionary+Derp.h
+// DerpKit
+//
+// Created by Steve Streza on 10/26/12.
+// Copyright (c) 2012 Mustacheware. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSDictionary (Derp)
+
+-(NSDictionary *)derp_dictionaryByMappingWithHandler:(id (^)(id object, id key, BOOL *stop))handler;
+-(NSDictionary *)derp_subdictionaryByFilteringWithHandler:(BOOL (^)(id object, id key, BOOL *stop))handler;
+
+@end
@@ -0,0 +1,39 @@
+//
+// NSDictionary+Derp.m
+// DerpKit
+//
+// Created by Steve Streza on 10/26/12.
+// Copyright (c) 2012 Mustacheware. All rights reserved.
+//
+
+#import "NSDictionary+Derp.h"
+
+@implementation NSDictionary (Derp)
+
+-(NSDictionary *)derp_dictionaryByMappingWithHandler:(id (^)(id object, id key, BOOL *stop))handler{
+ NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:self.count];
+
+ [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
+ id object = handler(obj, key, stop);
+ if(object){
+ [dictionary setObject:object forKey:key];
+ }
+ }];
+
+ return [dictionary copy];
+}
+
+-(NSDictionary *)derp_subdictionaryByFilteringWithHandler:(BOOL (^)(id object, id key, BOOL *stop))handler{
+ NSMutableDictionary *dictionary = [self mutableCopy];
+
+ [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
+ BOOL success = handler(obj, key, stop);
+ if(!success){
+ [dictionary removeObjectForKey:key];
+ }
+ }];
+
+ return [dictionary copy];
+}
+
+@end
@@ -27,3 +27,4 @@
#import "NSString+Derp.h"
#import "NSData+Derp.h"
#import "NSArray+Derp.h"
+#import "NSDictionary+Derp.h"
@@ -69,7 +69,7 @@ -(void)testPercentEscaping{
STAssertEqualObjects(original, [encoded derp_stringByUnscapingPercents], @"Percent unescaping not working");
}
--(void)testMapping{
+-(void)testArrayMapping{
NSArray *values = [@[ @1, @3, @42, @373 ] derp_arrayByMappingWithHandler:^id(id object, NSUInteger index, BOOL *stop) {
int number = [object intValue];
return @(number * 3);
@@ -83,7 +83,7 @@ -(void)testMapping{
STAssertEqualObjects(values[3], @1119, @"Mapping[3] did not produce valid result - %@ != %@", values[3], @1119);
}
--(void)testFiltering{
+-(void)testArrayFiltering{
NSArray *values = [@[@1, @2, @3, @4, @5, @6] derp_subarrayByFilteringWithHandler:^BOOL(id object, NSUInteger index, BOOL *stop) {
return ([object intValue] % 2 == 0);
}];
@@ -95,4 +95,28 @@ -(void)testFiltering{
STAssertEqualObjects(values[2], @6, @"Filtering[2] did not produce valid result - %@ != %@", values[2], @6);
}
+-(void)testDictionaryMapping{
+ NSDictionary *values = [@{ @"foo": @1, @"bar": @14, @"baz": @42} derp_dictionaryByMappingWithHandler:^id(id object, id key, BOOL *stop) {
+ return [NSNumber numberWithInt:([object intValue] * 3)];
+ }];
+
+ STAssertTrue(values.count == 3, @"Mapping does not have correct number of values: %i", values.count);
+
+ STAssertEqualObjects(values[@"foo"], @3, @"Mapping[foo] did not produce valid result - %@ != %@", values[@"foo"], @3);
+ STAssertEqualObjects(values[@"bar"], @42, @"Mapping[bar] did not produce valid result - %@ != %@", values[@"bar"], @42);
+ STAssertEqualObjects(values[@"baz"], @126, @"Mapping[baz] did not produce valid result - %@ != %@", values[@"baz"], @126);
+}
+
+-(void)testDictionaryFiltering{
+ NSDictionary *values = [@{ @"foo": @1, @"bar": @14, @"baz": @42} derp_subdictionaryByFilteringWithHandler:^BOOL(id object, id key, BOOL *stop) {
+ return ([object intValue] % 2 == 0);
+ }];
+
+ STAssertTrue(values.count == 2, @"Filtering does not have correct number of values");
+
+ STAssertNil(values[@"foo"], @"Filtering[foo] is not nil");
+ STAssertEqualObjects(values[@"bar"], @14, @"Filtering[bar] did not produce valid result - %@ != %@", values[@"bar"], @14);
+ STAssertEqualObjects(values[@"baz"], @42, @"Filtering[baz] did not produce valid result - %@ != %@", values[@"baz"], @42);
+}
+
@end

0 comments on commit bc2493e

Please sign in to comment.