diff --git a/ConciseKit.podspec b/ConciseKit.podspec index f8c45c5..09a9566 100644 --- a/ConciseKit.podspec +++ b/ConciseKit.podspec @@ -10,7 +10,7 @@ Pod::Spec.new do |s| s.source_files = 'src/**/*.{h,m}' - s.clean_paths = 'ConciseKitSpecs' + s.frameworks = 'Foundation' end diff --git a/ConciseKitSpecs/Spec/ConciseKitSpec.m b/ConciseKitSpecs/Spec/ConciseKitSpec.m index a08233d..7ffc45e 100644 --- a/ConciseKitSpecs/Spec/ConciseKitSpec.m +++ b/ConciseKitSpecs/Spec/ConciseKitSpec.m @@ -166,7 +166,7 @@ [$ waitUntil:condition timeOut:20.0]; NSArray *array = [CKMocks callsForSelector:@selector(waitUntil:timeOut:interval:) in:[$ class]]; assertThatUnsignedInteger([array count], equalToUnsignedInteger(1)); - assertThat([array $at:0], equalTo($arr(condition, $double(20.0), $double(0.1)))); + assertThat([array at:0], equalTo($arr(condition, $double(20.0), $double(0.1)))); [$ swizzleClassMethod:@selector(waitUntil:timeOut:interval:) in:[$ class] with:@selector(waitUntil:timeOut:interval:) in:[CKMocks class]]; }); }); @@ -177,7 +177,7 @@ [$ waitUntil:condition]; NSArray *array = [CKMocks callsForSelector:@selector(waitUntil:timeOut:interval:) in:[$ class]]; assertThatUnsignedInteger([array count], equalToUnsignedInteger(1)); - assertThat([array $at:0], equalTo($arr(condition, $double(10.0), $double(0.1)))); + assertThat([array at:0], equalTo($arr(condition, $double(10.0), $double(0.1)))); [$ swizzleClassMethod:@selector(waitUntil:timeOut:interval:) in:[$ class] with:@selector(waitUntil:timeOut:interval:) in:[CKMocks class]]; }); }); diff --git a/ConciseKitSpecs/Spec/Fixtures/CKMocks.m b/ConciseKitSpecs/Spec/Fixtures/CKMocks.m index 58a624c..21f4f55 100644 --- a/ConciseKitSpecs/Spec/Fixtures/CKMocks.m +++ b/ConciseKitSpecs/Spec/Fixtures/CKMocks.m @@ -25,7 +25,7 @@ + (void)addReceivedValues:(NSArray *)values forSelector:(SEL)selector in:(Class) if([dict objectForKey:key] == nil) { [dict setObject:$marr(nil) forKey:key]; } - [(NSMutableArray *)[dict objectForKey:key] $push:values]; + [(NSMutableArray *)[dict objectForKey:key] push:values]; } + (void)resetAll { diff --git a/ConciseKitSpecs/Spec/NSArrayConciseKitSpec.m b/ConciseKitSpecs/Spec/NSArrayConciseKitSpec.m index 7b03b17..87437e4 100644 --- a/ConciseKitSpecs/Spec/NSArrayConciseKitSpec.m +++ b/ConciseKitSpecs/Spec/NSArrayConciseKitSpec.m @@ -17,26 +17,26 @@ describe(@"-$first", ^{ it(@"returns the first object", ^{ - assertThat([array $first], equalTo(@"foo")); + assertThat([array first], equalTo(@"foo")); }); }); describe(@"-$last", ^{ it(@"returns the first object", ^{ - assertThat([array $last], equalTo(@"baz")); + assertThat([array last], equalTo(@"baz")); }); }); describe(@"-$at:", ^{ it(@"returns the object at the given index", ^{ - assertThat([array $at:1], equalTo(@"bar")); + assertThat([array at:1], equalTo(@"bar")); }); }); describe(@"-$each:", ^{ it(@"runs block for each item, passing the item as an argument", ^{ __block NSInteger i=0; - [$arr($integer(1), $integer(2), $integer(3)) $each:^(id obj) { + [$arr($integer(1), $integer(2), $integer(3)) each:^(id obj) { i += [obj integerValue]; }]; assertThatInteger(i, equalToInteger(6)); @@ -46,7 +46,7 @@ describe(@"-$eachWithIndex:", ^{ it(@"runs block for each item, passing the item and its index as arguments", ^{ __block NSInteger i=0; - [$arr($integer(1), $integer(2), $integer(3)) $eachWithIndex:^(id obj, NSUInteger j) { + [$arr($integer(1), $integer(2), $integer(3)) eachWithIndex:^(id obj, NSUInteger j) { i += [obj integerValue] + j; }]; assertThatInteger(i, equalToInteger(9)); @@ -56,7 +56,7 @@ describe(@"-$eachWithStop:", ^{ it(@"runs block for each item, passing the item as an argument, until stop is set to YES", ^{ __block NSInteger i=0; - [$arr($integer(1), $integer(2), $integer(3)) $eachWithStop:^(id obj, BOOL *stop) { + [$arr($integer(1), $integer(2), $integer(3)) eachWithStop:^(id obj, BOOL *stop) { i += [obj integerValue]; if([obj integerValue] == 2) { *stop = YES; @@ -69,7 +69,7 @@ describe(@"-$eachWithIndexAndStop:", ^{ it(@"runs block for each item, passing the item and its index as arguments, until stop is set to YES", ^{ __block NSInteger i=0; - [$arr($integer(1), $integer(2), $integer(3)) $eachWithIndexAndStop:^(id obj, NSUInteger j, BOOL *stop) { + [$arr($integer(1), $integer(2), $integer(3)) eachWithIndexAndStop:^(id obj, NSUInteger j, BOOL *stop) { i += [obj integerValue] + j; if([obj integerValue] == 2) { *stop = YES; @@ -81,7 +81,7 @@ describe(@"-$map:", ^{ it(@"runs block for each item, passing the item as an argument, and creates a new array containing the return values of the block", ^{ - array = [$arr($integer(1), $integer(2), $integer(3)) $map:^(id obj) { + array = [$arr($integer(1), $integer(2), $integer(3)) map:^(id obj) { return (id)$integer([obj integerValue] * 2); }]; assertThat(array, equalTo($arr($integer(2), $integer(4), $integer(6)))); @@ -90,7 +90,7 @@ describe(@"-$mapWithIndex:", ^{ it(@"runs block for each item, passing the item and its index as arguments and creates a new array containing the return values of the block", ^{ - array = [$arr($integer(1), $integer(2), $integer(3)) $mapWithIndex:^(id obj, NSUInteger j) { + array = [$arr($integer(1), $integer(2), $integer(3)) mapWithIndex:^(id obj, NSUInteger j) { return (id)$integer([obj integerValue] * 2 + j); }]; assertThat(array, equalTo($arr($integer(2), $integer(5), $integer(8)))); @@ -99,7 +99,7 @@ describe(@"-$reduce:", ^{ it(@"runs a block for each item, passing in a memoized value and the item, reassigning the memoized value from the return value of each iteration, finally returning the last return value", ^{ - NSNumber *result = [$arr($integer(1), $integer(2), $integer(3), $integer(4)) $reduce:^(NSNumber *memo, NSNumber *obj) { + NSNumber *result = [$arr($integer(1), $integer(2), $integer(3), $integer(4)) reduce:^(NSNumber *memo, NSNumber *obj) { return $integer([memo integerValue] + [obj integerValue]); }]; assertThat(result, equalTo($integer(10))); @@ -108,7 +108,7 @@ describe(@"-$reduceStartingAt:with:", ^{ it(@"performs a reduce function with a starting value", ^{ - NSNumber *result = [$arr($integer(10), $integer(2), $integer(3)) $reduceStartingAt:$integer(1) with:^(NSNumber *memo, NSNumber *obj) { + NSNumber *result = [$arr($integer(10), $integer(2), $integer(3)) reduceStartingAt:$integer(1) with:^(NSNumber *memo, NSNumber *obj) { return $integer([memo integerValue] * [obj integerValue]); }]; assertThat(result, equalTo($integer(60))); @@ -117,7 +117,7 @@ describe(@"-$select:", ^{ it(@"creates a subarray from elements where the block returns YES", ^{ - array = [$arr($integer(1), $integer(2), $integer(3), $integer(4)) $select:^BOOL(NSNumber *obj) { + array = [$arr($integer(1), $integer(2), $integer(3), $integer(4)) select:^BOOL(NSNumber *obj) { return ([obj integerValue] % 2) == 0; }]; assertThat(array, equalTo($arr($integer(2), $integer(4)))); @@ -126,7 +126,7 @@ describe(@"-$detect:", ^{ it(@"returns the first value for which the block returns YES", ^{ - NSNumber *result = [$arr($integer(1), $integer(2), $integer(3)) $detect:^BOOL(NSNumber *obj) { + NSNumber *result = [$arr($integer(1), $integer(2), $integer(3)) detect:^BOOL(NSNumber *obj) { return ([obj integerValue] % 2) == 1; }]; assertThat(result, equalTo($integer(1))); @@ -135,13 +135,13 @@ describe(@"-$join", ^{ it(@"joins the strings in the array without a separator, (more concise than componentsJoinedByString)", ^{ - assertThat([$arr(@"a", @"b", @"c") $join], equalTo(@"abc")); + assertThat([$arr(@"a", @"b", @"c") join], equalTo(@"abc")); }); }); describe(@"-$join:", ^{ it(@"joins the strings in the array with the supplied separator, (more concise than componentsJoinedByString)", ^{ - assertThat([$arr(@"a", @"b", @"c") $join:@"-"], equalTo(@"a-b-c")); + assertThat([$arr(@"a", @"b", @"c") join:@"-"], equalTo(@"a-b-c")); }); }); }); @@ -173,45 +173,45 @@ describe(@"-$push:", ^{ it(@"adds an object", ^{ - [marray $push:@"obj"]; + [marray push:@"obj"]; assertThat([marray lastObject], equalTo(@"obj")); }); it(@"returns self", ^{ - assertThat([marray $push:@"obj"], equalTo(marray)); + assertThat([marray push:@"obj"], equalTo(marray)); }); }); describe(@"-$pop", ^{ it(@"removes last object", ^{ - [marray $pop]; + [marray pop]; assertThat([marray lastObject], equalTo(@"bar")); }); it(@"returns last object", ^{ - assertThat([marray $pop], equalTo(@"baz")); + assertThat([marray pop], equalTo(@"baz")); }); }); describe(@"-$unshift:", ^{ it(@"adds first object", ^{ - [marray $unshift:@"obj"]; + [marray unshift:@"obj"]; assertThat([marray objectAtIndex:0], equalTo(@"obj")); }); it(@"returns self", ^{ - assertThat([marray $unshift:@"obj"], equalTo(marray)); + assertThat([marray unshift:@"obj"], equalTo(marray)); }); }); describe(@"-$shift", ^{ it(@"removes first object", ^{ - [marray $shift]; + [marray shift]; assertThat([marray objectAtIndex:0], equalTo(@"bar")); }); it(@"returns first object", ^{ - assertThat([marray $shift], equalTo(@"foo")); + assertThat([marray shift], equalTo(@"foo")); }); }); }); diff --git a/ConciseKitSpecs/Spec/NSDictionaryConciseKitSpec.m b/ConciseKitSpecs/Spec/NSDictionaryConciseKitSpec.m index 9f660cc..48a43fa 100644 --- a/ConciseKitSpecs/Spec/NSDictionaryConciseKitSpec.m +++ b/ConciseKitSpecs/Spec/NSDictionaryConciseKitSpec.m @@ -19,64 +19,64 @@ describe(@"-$for:", ^{ it(@"returns the object for the given key", ^{ - assertThat([dict $for:@"foo"], equalTo(@"bar")); + assertThat([dict for:@"foo"], equalTo(@"bar")); }); }); describe(@"-$keys:", ^{ it(@"returns the keys for the dict", ^{ - assertThat([[dict $keys] $join], equalTo(@"foohello")); + assertThat([[dict keys] join], equalTo(@"foohello")); }); }); describe(@"-$values:", ^{ it(@"returns the values for the dict", ^{ - assertThat([[dict $values] $join], equalTo(@"barworld")); + assertThat([[dict values] join], equalTo(@"barworld")); }); }); describe(@"-$each:", ^{ it(@"runs block for each key value pair, passing the key and value as arguments", ^{ NSMutableArray *result = $marrnew; - [dict $each:^(id key, id value) { - [result $push:[key $append:value]]; + [dict each:^(id key, id value) { + [result push:[key append:value]]; }]; - assertThat([result $first], equalTo(@"foobar")); - assertThat([result $last], equalTo(@"helloworld")); + assertThat([result first], equalTo(@"foobar")); + assertThat([result last], equalTo(@"helloworld")); }); }); describe(@"-$eachWithStop:", ^{ it(@"runs block for each key value pair, passing the key and value as an argument, until stop is set to YES", ^{ NSMutableArray *result = $marrnew; - [dict $eachWithStop:^(id key, id value, BOOL *stop) { - [result $push:[key $append:value]]; + [dict eachWithStop:^(id key, id value, BOOL *stop) { + [result push:[key append:value]]; if($eql(key, @"foo")) { *stop = YES; } }]; assertThatInteger([result count], equalToInteger(1)); - assertThat([result $last], equalTo(@"foobar")); + assertThat([result last], equalTo(@"foobar")); }); }); describe(@"-$eachKey:", ^{ it(@"runs block for each key, passing the key as an argument", ^{ NSMutableArray *result = $marrnew; - [dict $eachKey:^(id key) { - [result $push:key]; + [dict eachKey:^(id key) { + [result push:key]; }]; - assertThat([result $join], equalTo(@"foohello")); + assertThat([result join], equalTo(@"foohello")); }); }); describe(@"-$eachValue:", ^{ it(@"runs block for each value, passing the value as an argument", ^{ NSMutableArray *result = $marrnew; - [dict $eachValue:^(id value) { - [result $push:value]; + [dict eachValue:^(id value) { + [result push:value]; }]; - assertThat([result $join], equalTo(@"barworld")); + assertThat([result join], equalTo(@"barworld")); }); }); }); @@ -110,12 +110,12 @@ describe(@"-$obj:for:", ^{ it(@"sets an object for a key", ^{ - [mdict $obj:@"qux" for:@"baz"]; + [mdict obj:@"qux" for:@"baz"]; assertThat([mdict objectForKey:@"baz"], equalTo(@"qux")); }); it(@"returns self", ^{ - assertThat([mdict $obj:@"qux" for:@"baz"], equalTo(mdict)); + assertThat([mdict obj:@"qux" for:@"baz"], equalTo(mdict)); }); }); }); diff --git a/ConciseKitSpecs/Spec/NSStringConciseKitSpec.m b/ConciseKitSpecs/Spec/NSStringConciseKitSpec.m index 9f4dd65..c43445d 100644 --- a/ConciseKitSpecs/Spec/NSStringConciseKitSpec.m +++ b/ConciseKitSpecs/Spec/NSStringConciseKitSpec.m @@ -17,25 +17,25 @@ describe(@"-$append", ^{ it(@"appends another string", ^{ - assertThat([string $append:@"bar"], equalTo(@"foobar")); + assertThat([string append:@"bar"], equalTo(@"foobar")); }); }); describe(@"-$prepend", ^{ it(@"prepends another string", ^{ - assertThat([string $prepend:@"bar"], equalTo(@"barfoo")); + assertThat([string prepend:@"bar"], equalTo(@"barfoo")); }); }); describe(@"-$split:", ^{ it(@"divides string into an array of strings by a given string", ^{ - assertThat([@"f,o,o" $split:@","], equalTo($arr(@"f",@"o",@"o"))); + assertThat([@"f,o,o" split:@","], equalTo($arr(@"f",@"o",@"o"))); }); }); describe(@"-$split", ^{ it(@"divides string into an array of strings using whitespace", ^{ - assertThat([@"ab cd ef" $split], equalTo($arr(@"ab",@"cd",@"ef"))); + assertThat([@"ab cd ef" split], equalTo($arr(@"ab",@"cd",@"ef"))); }); }); }); @@ -67,27 +67,27 @@ describe(@"-$append_", ^{ it(@"appends another string and returns self", ^{ - assertThat([mstring $append_:@"bar"], equalTo(@"foobar")); + assertThat([mstring append_:@"bar"], equalTo(@"foobar")); }); }); describe(@"-$prepend_", ^{ it(@"prepends another string and returns self", ^{ - assertThat([mstring $prepend_:@"bar"], equalTo(@"barfoo")); + assertThat([mstring prepend_:@"bar"], equalTo(@"barfoo")); assertThat(mstring, equalTo(@"barfoo")); }); }); describe(@"-$insert:at:", ^{ it(@"inserts a string at given position and returns self", ^{ - assertThat([mstring $insert:@"bar" at:1], equalTo(@"fbaroo")); + assertThat([mstring insert:@"bar" at:1], equalTo(@"fbaroo")); assertThat(mstring, equalTo(@"fbaroo")); }); }); describe(@"-$set:", ^{ it(@"sets the mutable string to be given string", ^{ - assertThat([mstring $set:@"bar"], equalTo(@"bar")); + assertThat([mstring set:@"bar"], equalTo(@"bar")); assertThat(mstring, equalTo(@"bar")); }); }); diff --git a/README.md b/README.md index 70e0020..7d91a5f 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ A set of Objective-C additions and macros that lets you to write code more quick Use [CocoaPods](https://github.com/CocoaPods/CocoaPods) -```ruby -dependency 'ConciseKit', '~> 0.1.2' +```pod +pod "ConciseKit", :git => "https://github.com/basitali/ConciseKit.git" ``` or @@ -37,6 +37,7 @@ or [$ documentPath] => path to user's document directory [$ appPath] => path to app directory [$ resourcePath] => path to app's resources directory + [$ libraryPath] => path to app's library directory ### waitUntil @@ -79,6 +80,15 @@ Useful when writing tests for asynchronous tasks. Default timeout is 10 seconds, $eql(foo, bar) => [foo isEqual:bar] $safe(obj) => (obj == [NSNull null] ? nil : obj) +### System Versioning Preprocessor Macros + if (SYSTEM_VERSION_LESS_THAN(@"4.0")) { + ... + } + + if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) { + ... + } + ### NSArray shorthands $arr(foo, bar) => [NSArray arrayWithObjects:foo, bar, nil] diff --git a/src/CKMacros.h b/src/CKMacros.h index 75b06c2..2f9980a 100644 --- a/src/CKMacros.h +++ b/src/CKMacros.h @@ -47,3 +47,13 @@ #define $size(val) [NSValue valueWithSize:(val)] #define $safe(obj) ((NSNull *)(obj) == [NSNull null] ? nil : (obj)) + +/* + * System Versioning Preprocessor Macros + */ + +#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) +#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) +#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) +#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) +#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) \ No newline at end of file diff --git a/src/ConciseKit.h b/src/ConciseKit.h index 0f4238f..011c610 100644 --- a/src/ConciseKit.h +++ b/src/ConciseKit.h @@ -6,6 +6,7 @@ @interface ConciseKit : NSObject {} + (NSString *)homePath; ++ (NSString *)libraryPath; + (NSString *)desktopPath; + (NSString *)documentPath; + (NSString *)appPath; diff --git a/src/ConciseKit.m b/src/ConciseKit.m index 4678552..73ebc06 100644 --- a/src/ConciseKit.m +++ b/src/ConciseKit.m @@ -7,6 +7,10 @@ + (NSString *)homePath { return NSHomeDirectory(); } ++ (NSString *)libraryPath { + return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; +} + + (NSString *)desktopPath { return [NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) objectAtIndex:0]; } diff --git a/src/NSArray+ConciseKit.h b/src/NSArray+ConciseKit.h index 5672317..27a3e01 100644 --- a/src/NSArray+ConciseKit.h +++ b/src/NSArray+ConciseKit.h @@ -2,29 +2,29 @@ @interface NSArray (ConciseKit) -- (id)$first; -- (id)$last; -- (id)$at:(NSUInteger)index; -- (NSArray *)$each:(void (^)(id obj))block; -- (NSArray *)$eachWithIndex:(void (^)(id obj, NSUInteger idx))block; -- (NSArray *)$eachWithStop:(void (^)(id obj, BOOL *stop))block; -- (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)$reduce:(id (^)(id memo, id obj))block; -- (id)$reduceStartingAt:(id)starting with:(id (^)(id memo, id obj))block; -- (NSArray *)$select:(BOOL(^)(id obj))block; -- (id)$detect:(BOOL(^)(id obj))block; -- (NSString *)$join; -- (NSString *)$join:(NSString *)separator; +- (id)first; +- (id)last; +- (id)at:(NSUInteger)index; +- (NSArray *)each:(void (^)(id obj))block; +- (NSArray *)eachWithIndex:(void (^)(id obj, NSUInteger idx))block; +- (NSArray *)eachWithStop:(void (^)(id obj, BOOL *stop))block; +- (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)reduce:(id (^)(id memo, id obj))block; +- (id)reduceStartingAt:(id)starting with:(id (^)(id memo, id obj))block; +- (NSArray *)select:(BOOL(^)(id obj))block; +- (id)detect:(BOOL(^)(id obj))block; +- (NSString *)join; +- (NSString *)join:(NSString *)separator; @end @interface NSMutableArray (ConciseKit) -- (NSMutableArray *)$push:(id)anObject; -- (id)$pop; -- (NSMutableArray *)$unshift:(id)anObject; -- (id)$shift; +- (NSMutableArray *)push:(id)anObject; +- (id)pop; +- (NSMutableArray *)unshift:(id)anObject; +- (id)shift; @end \ No newline at end of file diff --git a/src/NSArray+ConciseKit.m b/src/NSArray+ConciseKit.m index 0cfc67d..e9a96f7 100644 --- a/src/NSArray+ConciseKit.m +++ b/src/NSArray+ConciseKit.m @@ -2,47 +2,47 @@ @implementation NSArray (ConciseKit) -- (id)$first { +- (id)first { return [self objectAtIndex:0]; } -- (id)$last { +- (id)last { return [self lastObject]; } -- (id)$at:(NSUInteger)index { +- (id)at:(NSUInteger)index { return [self objectAtIndex:index]; } -- (NSArray *)$each:(void (^)(id obj))block { +- (NSArray *)each:(void (^)(id obj))block { [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { block(obj); }]; return self; } -- (NSArray *)$eachWithIndex:(void (^)(id obj, NSUInteger idx))block { +- (NSArray *)eachWithIndex:(void (^)(id obj, NSUInteger idx))block { [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { block(obj, idx); }]; return self; } -- (NSArray *)$eachWithStop:(void (^)(id obj, BOOL *stop))block { +- (NSArray *)eachWithStop:(void (^)(id obj, BOOL *stop))block { [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { block(obj, stop); }]; return self; } -- (NSArray *)$eachWithIndexAndStop:(void (^)(id obj, NSUInteger idx, BOOL *stop))block { +- (NSArray *)eachWithIndexAndStop:(void (^)(id obj, NSUInteger idx, BOOL *stop))block { [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { block(obj, idx, stop); }]; return self; } -- (NSArray *)$map:(id (^)(id obj))block { +- (NSArray *)map:(id (^)(id obj))block { __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]]; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [array addObject:block(obj)]; @@ -50,7 +50,7 @@ @implementation NSArray (ConciseKit) return array; } -- (NSArray *)$mapWithIndex:(id (^)(id obj, NSUInteger idx))block { +- (NSArray *)mapWithIndex:(id (^)(id obj, NSUInteger idx))block { __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]]; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [array addObject:block(obj, idx)]; @@ -58,7 +58,7 @@ @implementation NSArray (ConciseKit) return array; } -- (id)$reduce:(id (^)(id memo, id obj))block { +- (id)reduce:(id (^)(id memo, id obj))block { __block id ret = nil; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (idx == 0) { @@ -70,7 +70,7 @@ @implementation NSArray (ConciseKit) return ret; } -- (id)$reduceStartingAt:(id)starting with:(id (^)(id memo, id obj))block { +- (id)reduceStartingAt:(id)starting with:(id (^)(id memo, id obj))block { __block id ret = starting; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { ret = block(ret, obj); @@ -78,7 +78,7 @@ @implementation NSArray (ConciseKit) return ret; } -- (NSArray *)$select:(BOOL(^)(id obj))block { +- (NSArray *)select:(BOOL(^)(id obj))block { __block NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]]; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (block(obj)) { @@ -88,7 +88,7 @@ @implementation NSArray (ConciseKit) return [NSArray arrayWithArray:array]; } -- (id)$detect:(BOOL(^)(id obj))block { +- (id)detect:(BOOL(^)(id obj))block { __block id ret = nil; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (block(obj)) { @@ -99,11 +99,11 @@ @implementation NSArray (ConciseKit) return ret; } -- (NSString *)$join { +- (NSString *)join { return [self componentsJoinedByString:@""]; } -- (NSString *)$join:(NSString *)separator { +- (NSString *)join:(NSString *)separator { return [self componentsJoinedByString:separator]; } @@ -120,23 +120,23 @@ @implementation NSMutableArray (ConciseKit) #define IF_ARC(with, without) without #endif -- (NSMutableArray *)$push:(id)anObject { +- (NSMutableArray *)push:(id)anObject { [self addObject:anObject]; return self; } -- (id)$pop; { +- (id)pop; { IF_ARC(id lastObject = [self lastObject];, id lastObject = [[[self lastObject] retain] autorelease];) [self removeLastObject]; return lastObject; } -- (NSMutableArray *)$unshift:(id)anObject { +- (NSMutableArray *)unshift:(id)anObject { [self insertObject:anObject atIndex:0]; return self; } -- (id)$shift; { +- (id)shift; { IF_ARC(id firstObject = [self objectAtIndex:0];, id firstObject = [[[self objectAtIndex:0] retain] autorelease];) [self removeObjectAtIndex:0]; diff --git a/src/NSDictionary+ConciseKit.h b/src/NSDictionary+ConciseKit.h index 3397882..ea9c327 100644 --- a/src/NSDictionary+ConciseKit.h +++ b/src/NSDictionary+ConciseKit.h @@ -2,18 +2,20 @@ @interface NSDictionary (ConciseKit) -- (id)$for:(id)aKey; -- (NSArray *)$keys; -- (NSArray *)$values; -- (NSDictionary *)$each:(void (^)(id key, id value))block; -- (NSDictionary *)$eachWithStop:(void (^)(id key, id value, BOOL *stop))block; -- (NSDictionary *)$eachKey:(void (^)(id key))block; -- (NSDictionary *)$eachValue:(void (^)(id value))block; +- (id)for:(id)aKey; +- (NSArray *)keys; +- (NSArray *)values; +- (BOOL)hasKey:(id)key; + +- (NSDictionary *)each:(void (^)(id key, id value))block; +- (NSDictionary *)eachWithStop:(void (^)(id key, id value, BOOL *stop))block; +- (NSDictionary *)eachKey:(void (^)(id key))block; +- (NSDictionary *)eachValue:(void (^)(id value))block; @end @interface NSMutableDictionary (ConciseKit) -- (NSMutableDictionary *)$obj:(id)anObject for:(id)aKey; +- (NSMutableDictionary *)obj:(id)anObject for:(id)aKey; @end \ No newline at end of file diff --git a/src/NSDictionary+ConciseKit.m b/src/NSDictionary+ConciseKit.m index 49df3f4..bff68da 100644 --- a/src/NSDictionary+ConciseKit.m +++ b/src/NSDictionary+ConciseKit.m @@ -2,40 +2,44 @@ @implementation NSDictionary (ConciseKit) -- (id)$for:(id)aKey { +- (id)for:(id)aKey { return [self objectForKey:aKey]; } -- (NSArray *)$keys { +- (NSArray *)keys { return [self allKeys]; } -- (NSArray *)$values { +- (NSArray *)values { return [self allValues]; } -- (NSDictionary *)$each:(void (^)(id key, id value))block { +- (BOOL)hasKey:(id)key { + return [[self keys] containsObject:key]; +} + +- (NSDictionary *)each:(void (^)(id key, id value))block { [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { block(key, value); }]; return self; } -- (NSDictionary *)$eachWithStop:(void (^)(id key, id value, BOOL *stop))block { +- (NSDictionary *)eachWithStop:(void (^)(id key, id value, BOOL *stop))block { [self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { block(key, value, stop); }]; return self; } -- (NSDictionary *)$eachKey:(void (^)(id key))block { +- (NSDictionary *)eachKey:(void (^)(id key))block { [[self allKeys] enumerateObjectsUsingBlock:^(id key, NSUInteger idx, BOOL *stop) { block(key); }]; return self; } -- (NSDictionary *)$eachValue:(void (^)(id value))block { +- (NSDictionary *)eachValue:(void (^)(id value))block { [[self allValues] enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) { block(value); }]; @@ -46,7 +50,7 @@ @implementation NSDictionary (ConciseKit) @implementation NSMutableDictionary (ConciseKit) -- (NSMutableDictionary *)$obj:(id)anObject for:(id)aKey { +- (NSMutableDictionary *)obj:(id)anObject for:(id)aKey { [self setObject:anObject forKey:aKey]; return self; } diff --git a/src/NSString+ConciseKit.h b/src/NSString+ConciseKit.h index 9648b8c..35d631f 100644 --- a/src/NSString+ConciseKit.h +++ b/src/NSString+ConciseKit.h @@ -2,18 +2,22 @@ @interface NSString (ConciseKit) -- (NSString *)$append:(NSString *)aString; -- (NSString *)$prepend:(NSString *)aString; -- (NSArray *)$split:(NSString *)aString; -- (NSArray *)$split; +- (NSString *)appendPath:(NSString *)aString; +- (NSString *)append:(NSString *)aString; +- (NSString *)prepend:(NSString *)aString; +- (NSArray *)split:(NSString *)aString; +- (NSArray *)split; +- (NSString *)trim; +- (BOOL)equals:(NSString*)string; +- (BOOL)notEquals:(NSString*)string; @end @interface NSMutableString (ConciseKit) -- (NSMutableString *)$append_:(NSString *)aString; -- (NSMutableString *)$prepend_:(NSString *)aString; -- (NSMutableString *)$insert:(NSString *)aString at:(NSUInteger)anIndex; -- (NSMutableString *)$set:(NSString *)aString; +- (NSMutableString *)append_:(NSString *)aString; +- (NSMutableString *)prepend_:(NSString *)aString; +- (NSMutableString *)insert:(NSString *)aString at:(NSUInteger)anIndex; +- (NSMutableString *)set:(NSString *)aString; @end \ No newline at end of file diff --git a/src/NSString+ConciseKit.m b/src/NSString+ConciseKit.m index 4083227..36a3754 100644 --- a/src/NSString+ConciseKit.m +++ b/src/NSString+ConciseKit.m @@ -2,42 +2,58 @@ @implementation NSString (ConciseKit) -- (NSString *)$append:(NSString *)aString { +- (NSString *)appendPath:(NSString *)aString { + return [self stringByAppendingPathComponent:aString]; +} + +- (NSString *)append:(NSString *)aString { return [self stringByAppendingString:aString]; } -- (NSString *)$prepend:(NSString *)aString { +- (NSString *)prepend:(NSString *)aString { return [NSString stringWithFormat:@"%@%@", aString, self]; } -- (NSArray *)$split:(NSString *)aString { +- (NSArray *)split:(NSString *)aString { return [self componentsSeparatedByString:aString]; } -- (NSArray *)$split { +- (NSArray *)split { return [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } +- (NSString *)trim { + return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; +} + +- (BOOL)equals:(NSString*)string { + return [self isEqualToString:string]; +} + +- (BOOL)notEquals:(NSString*)string { + return ![self equals:string]; +} + @end @implementation NSMutableString (ConciseKit) -- (NSMutableString *)$append_:(NSString *)aString { +- (NSMutableString *)append_:(NSString *)aString { [self appendString:aString]; return self; } -- (NSMutableString *)$prepend_:(NSString *)aString { +- (NSMutableString *)prepend_:(NSString *)aString { [self insertString:aString atIndex:0]; return self; } -- (NSMutableString *)$insert:(NSString *)aString at:(NSUInteger)anIndex { +- (NSMutableString *)insert:(NSString *)aString at:(NSUInteger)anIndex { [self insertString:aString atIndex:anIndex]; return self; } -- (NSMutableString *)$set:(NSString *)aString { +- (NSMutableString *)set:(NSString *)aString { [self setString:aString]; return self; }