Last active
December 29, 2015 12:22
-
-
Save RobertAudi/5926772 to your computer and use it in GitHub Desktop.
NSString category containing an Objective-C port of @henrik's slugalize helper method: https://github.com/henrik/slugalizer?source=c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Robert Audi on 07/04/13. | |
// Updated on 08/21/14 | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSString (RAInflections) | |
- (NSString *)slugalize; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Robert Audi on 07/04/13. | |
// Updated on 08/21/14 | |
// | |
#import "NSString+RAInflections.h" | |
@implementation NSString (RAInflections) | |
/** | |
* Port of the slugalized helper created by @henrik | |
* https://github.com/RobertAudi/slugalizer | |
*/ | |
- (NSString *)slugalize | |
{ | |
NSString *separator = @"-"; | |
NSMutableString *slugalizedString = [NSMutableString string]; | |
NSRange replaceRange = NSMakeRange(0, self.length); | |
// Remove all non ASCII characters | |
NSError *nonASCIICharsRegexError = nil; | |
NSRegularExpression *nonASCIICharsRegex = [NSRegularExpression regularExpressionWithPattern:@"[^\\x00-\\x7F]+" | |
options:0 | |
error:&nonASCIICharsRegexError]; | |
slugalizedString = [[nonASCIICharsRegex stringByReplacingMatchesInString:self | |
options:0 | |
range:replaceRange | |
withTemplate:@""] mutableCopy]; | |
// Turn non-slug characters into separators | |
NSError *nonSlugCharactersError = nil; | |
NSRegularExpression *nonSlugCharactersRegex = [NSRegularExpression regularExpressionWithPattern:@"[^a-z0-9\\-_\\+]+" | |
options:NSRegularExpressionCaseInsensitive | |
error:&nonSlugCharactersError]; | |
slugalizedString = [[nonSlugCharactersRegex stringByReplacingMatchesInString:slugalizedString | |
options:0 | |
range:replaceRange | |
withTemplate:separator] mutableCopy]; | |
// No more than one of the separator in a row | |
NSError *repeatingSeparatorsError = nil; | |
NSRegularExpression *repeatingSeparatorsRegex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"%@{2,}", separator] | |
options:0 | |
error:&repeatingSeparatorsError]; | |
slugalizedString = [[repeatingSeparatorsRegex stringByReplacingMatchesInString:slugalizedString | |
options:0 | |
range:replaceRange | |
withTemplate:separator] mutableCopy]; | |
// Remove leading/trailing separator | |
slugalizedString = [[slugalizedString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:separator]] mutableCopy]; | |
return [slugalizedString lowercaseString]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should do
replaceRange = NSMakeRange(0, slugalizedString.length);
after all stringByReplacingMatchesInString