From 3d118e0a0b40aa72db3011845c7f53423f03aaae Mon Sep 17 00:00:00 2001 From: chenly Date: Mon, 9 Sep 2013 10:00:37 +0800 Subject: [PATCH 1/3] add more syntaxs --- src/NSArray+ConciseKit.h | 3 +++ src/NSArray+ConciseKit.m | 12 ++++++++++++ src/NSDictionary+ConciseKit.h | 1 + src/NSDictionary+ConciseKit.m | 4 +++- src/NSString+ConciseKit.h | 3 +++ src/NSString+ConciseKit.m | 19 +++++++++++++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/NSArray+ConciseKit.h b/src/NSArray+ConciseKit.h index 5672317..c643f4f 100644 --- a/src/NSArray+ConciseKit.h +++ b/src/NSArray+ConciseKit.h @@ -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..2c98409 100644 --- a/src/NSDictionary+ConciseKit.h +++ b/src/NSDictionary+ConciseKit.h @@ -3,6 +3,7 @@ @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) From 7bf8a60b8f08f0266e7ca3df2770f933e58789e3 Mon Sep 17 00:00:00 2001 From: chenly Date: Mon, 9 Sep 2013 10:04:40 +0800 Subject: [PATCH 2/3] add clamp syntax --- src/CKMacros.h | 1 + 1 file changed, 1 insertion(+) 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 From 66ee704c3e96b64e13b9128cc19c3bc52b549181 Mon Sep 17 00:00:00 2001 From: chenly Date: Mon, 9 Sep 2013 11:52:28 +0800 Subject: [PATCH 3/3] import CKMacros --- src/NSArray+ConciseKit.h | 2 +- src/NSDictionary+ConciseKit.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NSArray+ConciseKit.h b/src/NSArray+ConciseKit.h index c643f4f..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; diff --git a/src/NSDictionary+ConciseKit.h b/src/NSDictionary+ConciseKit.h index 2c98409..6da57ad 100644 --- a/src/NSDictionary+ConciseKit.h +++ b/src/NSDictionary+ConciseKit.h @@ -1,5 +1,5 @@ #import - +#import "CKMacros.h" @interface NSDictionary (ConciseKit) - (id)$for:(id)aKey;