Skip to content

add more syntaxs #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CKMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 4 additions & 1 deletion src/NSArray+ConciseKit.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import <Foundation/Foundation.h>

#import "CKMacros.h"
@interface NSArray (ConciseKit)

- (id)$first;
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/NSArray+ConciseKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/NSDictionary+ConciseKit.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#import <Foundation/Foundation.h>

#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;
Expand Down
4 changes: 3 additions & 1 deletion src/NSDictionary+ConciseKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
3 changes: 3 additions & 0 deletions src/NSString+ConciseKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions src/NSString+ConciseKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down