Skip to content

Commit

Permalink
Add pk_sanitizedName to be able to use the name in filenames and so on.
Browse files Browse the repository at this point in the history
  • Loading branch information
pk committed Jan 28, 2015
1 parent 7f5ae27 commit b24569d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Source/Categories/UIDevice+PKAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@
*/
- (NSString *)pk_ipAddress;

/**
* Gets device name which is safe to use as filename.
*
* Replaces all non-alphanumeric characters with underscore (_)
*
* @return Sanitized device name
*/
- (NSString *)pk_sanitizedName;

@end
8 changes: 8 additions & 0 deletions Source/Categories/UIDevice+PKAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,13 @@ - (unsigned long long)pk_availableDiskSpace {
return totalFreeSpace;
}


#pragma mark - Device name

- (NSString *)pk_sanitizedName {
NSCharacterSet *set = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
return [[[self name] componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@"_"];
}

@end

13 changes: 11 additions & 2 deletions Specs/Categories/UIDevice+PKAdditionsSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
// Created by Pavel Kunc on 04/11/2013.
//

#import "UIDevice+PKAdditions.h"

#define EXP_SHORTHAND
#import "Expecta.h"
#import <Specta/Specta.h>
#import <OCMock/OCMock.h>
#import "UIDevice+PKAdditions.h"

SpecBegin(UIDevice_PKAdditions)
Expand Down Expand Up @@ -55,5 +54,15 @@

});

describe (@"pk_sanitizedName", ^{

it (@"returns device name with non alphanumeric characters replaced with _", ^{
id mock = OCMPartialMock([UIDevice currentDevice]);
OCMStub([mock name]).andReturn(@"Pavel's iPad Mini 1G");
expect([mock pk_sanitizedName]).equal(@"Pavel_s_iPad_Mini_1G");
});

});

SpecEnd

0 comments on commit b24569d

Please sign in to comment.