diff --git a/src/CKMacros.h b/src/CKMacros.h index 75b06c2..0193ea8 100644 --- a/src/CKMacros.h +++ b/src/CKMacros.h @@ -47,3 +47,4 @@ #define $size(val) [NSValue valueWithSize:(val)] #define $safe(obj) ((NSNull *)(obj) == [NSNull null] ? nil : (obj)) +#define $clamp(val, min, max) MAX(MIN(val, min), max) \ No newline at end of file diff --git a/src/NSArray+ConciseKit.h b/src/NSArray+ConciseKit.h index 5672317..066f2ee 100644 --- a/src/NSArray+ConciseKit.h +++ b/src/NSArray+ConciseKit.h @@ -1,5 +1,5 @@ #import - +#import "CKMacros.h" @interface NSArray (ConciseKit) - (id)$first; @@ -11,6 +11,9 @@ - (NSArray *)$eachWithIndexAndStop:(void (^)(id obj, NSUInteger idx, BOOL *stop))block; - (NSArray *)$map:(id (^)(id obj))block; - (NSArray *)$mapWithIndex:(id (^)(id obj, NSUInteger idx))block; +- (id)$safeFirst; +- (id)$safeLast; +- (id)$safeAt:(NSUInteger)index; - (id)$reduce:(id (^)(id memo, id obj))block; - (id)$reduceStartingAt:(id)starting with:(id (^)(id memo, id obj))block; - (NSArray *)$select:(BOOL(^)(id obj))block; diff --git a/src/NSArray+ConciseKit.m b/src/NSArray+ConciseKit.m index 0cfc67d..2111d64 100644 --- a/src/NSArray+ConciseKit.m +++ b/src/NSArray+ConciseKit.m @@ -58,6 +58,18 @@ @implementation NSArray (ConciseKit) return array; } +- (id)$safeFirst { + return $safe([self objectAtIndex:0]); +} + +- (id)$safeLast { + return $safe([self lastObject]); +} + +- (id)$safeAt:(NSUInteger)index { + return $safe([self objectAtIndex:index]); +} + - (id)$reduce:(id (^)(id memo, id obj))block { __block id ret = nil; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { diff --git a/src/NSDictionary+ConciseKit.h b/src/NSDictionary+ConciseKit.h index 3397882..6da57ad 100644 --- a/src/NSDictionary+ConciseKit.h +++ b/src/NSDictionary+ConciseKit.h @@ -1,8 +1,9 @@ #import - +#import "CKMacros.h" @interface NSDictionary (ConciseKit) - (id)$for:(id)aKey; +- (id)$safeFor:(id)aKey; - (NSArray *)$keys; - (NSArray *)$values; - (NSDictionary *)$each:(void (^)(id key, id value))block; diff --git a/src/NSDictionary+ConciseKit.m b/src/NSDictionary+ConciseKit.m index 49df3f4..8cc525b 100644 --- a/src/NSDictionary+ConciseKit.m +++ b/src/NSDictionary+ConciseKit.m @@ -5,7 +5,9 @@ @implementation NSDictionary (ConciseKit) - (id)$for:(id)aKey { return [self objectForKey:aKey]; } - +- (id)$safeFor:(id)aKey { + return $safe([self objectForKey:aKey]); +} - (NSArray *)$keys { return [self allKeys]; } diff --git a/src/NSString+ConciseKit.h b/src/NSString+ConciseKit.h index 9648b8c..3f787d3 100644 --- a/src/NSString+ConciseKit.h +++ b/src/NSString+ConciseKit.h @@ -6,6 +6,9 @@ - (NSString *)$prepend:(NSString *)aString; - (NSArray *)$split:(NSString *)aString; - (NSArray *)$split; +- (BOOL)$isBlank; +- (NSInteger)$indexOf:(NSString *)theSubString from:(NSInteger)theFrom; +- (NSString *)$substringFrom:(NSInteger)theFrom to:(NSInteger)theTo; @end diff --git a/src/NSString+ConciseKit.m b/src/NSString+ConciseKit.m index 4083227..5b5a1b5 100644 --- a/src/NSString+ConciseKit.m +++ b/src/NSString+ConciseKit.m @@ -18,6 +18,25 @@ @implementation NSString (ConciseKit) return [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } +- (BOOL)$isBlank { + return ([self length] == 0); +} + +- (NSInteger)$indexOf:(NSString *)theSubString from:(NSInteger)theFrom { + NSRange theRange = NSMakeRange(theFrom, self.length - theFrom); + + NSRange theIndex = [self rangeOfString:theSubString options:NSLiteralSearch range:theRange]; + if (theIndex.location == NSNotFound) { + return -1; + } + return theIndex.location + theIndex.length; +} + +- (NSString *)$substringFrom:(NSInteger)theFrom to:(NSInteger)theTo { + NSRange theRange = NSMakeRange(theFrom, theTo - theFrom); + return [self substringWithRange:theRange]; +} + @end @implementation NSMutableString (ConciseKit)