Skip to content

Commit

Permalink
Strip NSString spaces from start and end.
Browse files Browse the repository at this point in the history
  • Loading branch information
devinross committed Jan 26, 2016
1 parent 3573c48 commit 70eacd3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions curry/Categories/Foundation/NSString+TKCategory.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
*/
@property (nonatomic, getter=isEmail, readonly) BOOL email;


/** Returns a `NSString` that removes blank space from the start and end of string.
@return A stripped string.
*/
@property (nonatomic, readonly, copy) NSString *stripWhitespace;

/** Returns a `NSString` that removes blank space and new lines from the start and end of string.
@return A stripped string.
*/
@property (nonatomic, readonly, copy) NSString *stripWhitepspaceAndNewlines;

/** Returns a `NSString` that is URL friendly.
@return A URL encoded string.
*/
Expand Down
10 changes: 10 additions & 0 deletions curry/Categories/Foundation/NSString+TKCategory.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ - (BOOL) isEmail{
return [regExPredicate evaluateWithObject:[self lowercaseString]];
}


- (NSString*) stripWhitespace{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

- (NSString*) stripWhitepspaceAndNewlines{
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}


- (NSString*) URLEncode{
NSMutableCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet].mutableCopy;
[set removeCharactersInString:@";?/:#& =+$,%<>~%"];
Expand Down
2 changes: 2 additions & 0 deletions curryTests/NSStringTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

- (void) testShouldHaveString;

- (void) testShouldHaveStripWhitespace;

- (void) testShouldPassCreditCardValidation;

- (void) testShouldFormatPhoneString;
Expand Down
11 changes: 11 additions & 0 deletions curryTests/NSStringTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ - (void) testShouldCountWhitespaceCorrectly{
XCTAssertEqual(@" \n\t".lengthWithoutWhitespace, 0);
}


- (void) testShouldHaveStripWhitespace{

NSString *resultString = @"This is the final result string.";
NSString *test1 = [NSString stringWithFormat:@" %@ ",resultString];
NSString *test2 = [NSString stringWithFormat:@"\n \t %@ \n \t ",resultString];
XCTAssertEqualObjects(test1.stripWhitespace,resultString);
XCTAssertEqualObjects(test2.stripWhitepspaceAndNewlines,resultString);

}

- (void) testShouldEncodeString{

XCTAssertEqualObjects([@"" URLEncode], @"");
Expand Down

0 comments on commit 70eacd3

Please sign in to comment.