Skip to content

Commit

Permalink
Adding gh_present. Fixing test and README.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Sep 4, 2014
1 parent f8c99ac commit cc29227
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Classes/GHNSString+Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
*/
- (BOOL)gh_isPresent;

/*!
Returns string if present, not empty or whitespace.
@" ", @"" returns nil.
*/
+ (NSString *)gh_present:(NSString *)s;

/*!
Create attributed string that truncates in the middle.
Expand Down
5 changes: 5 additions & 0 deletions Classes/GHNSString+Utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ - (BOOL)gh_isPresent {
return ![NSString gh_isBlank:self];
}

+ (NSString *)gh_present:(NSString *)s {
if ([s gh_isPresent]) return s;
return nil;
}

- (BOOL)gh_isEqualIgnoreCase:(NSString *)s {
return [self compare:s options:NSCaseInsensitiveSearch] == NSOrderedSame;
}
Expand Down
2 changes: 1 addition & 1 deletion GHKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "GHKit"
s.version = "1.0.7"
s.version = "1.0.8"
s.summary = "Objective-C categories and utilities"
s.homepage = "https://github.com/gabriel/gh-kit"
s.license = 'MIT'
Expand Down
41 changes: 41 additions & 0 deletions GHKit.xcodeproj/project.xcworkspace/xcshareddata/GHKit.xccheckout
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>15693D2D-7570-4673-8BBF-E3E5B66EA5D3</string>
<key>IDESourceControlProjectName</key>
<string>GHKit</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>FCF1318D7FA6CAB10513AC324A1290B93E7E7F64</key>
<string>github.com:gabriel/gh-kit.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>GHKit.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>FCF1318D7FA6CAB10513AC324A1290B93E7E7F64</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>github.com:gabriel/gh-kit.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>FCF1318D7FA6CAB10513AC324A1290B93E7E7F64</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>FCF1318D7FA6CAB10513AC324A1290B93E7E7F64</string>
<key>IDESourceControlWCCName</key>
<string>gh-kit</string>
</dict>
</array>
</dict>
</plist>
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,26 @@ date = [date gh_addDays:-1];

***Strings:***

`GHNSString+Utils.h`: Stripping, reversing, counting, UUID and more.
`GHNSString+Utils.h`: Stripping, reversing, counting and more.

```objc
[NSString gh_isBlank:@" "]; // YES
[NSString gh_isBlank:nil]; // YES
[@" some text " gh_strip]; // @"some text"
[@" " gh_isPresent]; // NO
[@"abc" gh_isPresent]; // YES
[NSString gh_present:@" "]; // nil
[NSString gh_present:@"some text"]; // @"some text"

[@"abc" gh_reverse]; // @"cba"

[@"ababababcde" gh_count:@"ab"]; // 4 (@"ab" appears 4 times)

[NSString gh_localizedStringForTimeInterval:30]; // "half a minute"
[NSString gh_abbreviatedStringForTimeInterval:30]; // @"30s"

[@"WWW.test.com" gh_startsWith:@"www." options:NSCaseInsensitiveSearch]; // YES
[@"foo:bar" gh_lastSplitWithString:@":" options:NSCaseInsensitiveSearch]; // @"bar"
```
***URLs:***
Expand Down
6 changes: 4 additions & 2 deletions Tests/NSDictionaryUtilsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ - (void)testCompact {
}

- (void)testJSON {
NSString *JSONString = [@{@"key1": @(2), @"key2": @(3.1), @"key3": @YES} gh_toJSONString:nil];
GHAssertEqualStrings(@"{\"key2\":3.1,\"key1\":2,\"key3\":true}", JSONString, nil);
NSDictionary *dict = @{@"key1": @(2), @"key2": @(3.1), @"key3": @YES};
NSString *JSONString = [dict gh_toJSONString:nil];
NSDictionary *dict2 = [NSJSONSerialization JSONObjectWithData:[JSONString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
GHAssertEqualObjects(dict, dict2, nil);
}

@end
10 changes: 10 additions & 0 deletions Tests/NSStringUtilsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ - (void)testStartsWith {
GHAssertFalse([@"www.test.com" gh_startsWith:@"" options:0], nil);

GHAssertTrue([@"www.test.com" gh_startsWith:@"WWW." options:NSCaseInsensitiveSearch], nil);
GHAssertTrue([@"WWW.test.com" gh_startsWith:@"www." options:NSCaseInsensitiveSearch], nil);
GHAssertTrue([@"www.test.com" gh_startsWith:@"WWW.test.com" options:NSCaseInsensitiveSearch], nil);
GHAssertFalse([@"www.test.com" gh_startsWith:@"" options:NSCaseInsensitiveSearch], nil);
}
Expand All @@ -90,6 +91,15 @@ - (void)testCount {
GHAssertTrue([@" " gh_count:@"\n"] == 0, nil);
}

- (void)testPresent {
GHAssertNil([NSString gh_present:@" "], nil);
GHAssertEqualStrings([NSString gh_present:@"s"], @"s", nil);

GHAssertTrue([@"s" gh_isPresent], nil);
GHAssertFalse([@" " gh_isPresent], nil);
GHAssertFalse([@"" gh_isPresent], nil);
}

- (void)testSubStringSegmentsWithin {

NSString *test1 = @"This <START>is a<END> test.";
Expand Down

0 comments on commit cc29227

Please sign in to comment.