Skip to content

Commit

Permalink
Extracted kentekenformatter + tests from other project
Browse files Browse the repository at this point in the history
  • Loading branch information
mac-cain13 committed Sep 5, 2012
1 parent 3fadfe1 commit 825b6bc
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/kenteken-formatter.xcodeproj/xcuserdata/
/kenteken-formatter.xcodeproj/project.xcworkspace/xcuserdata/
44 changes: 44 additions & 0 deletions Classes/NSString+KentekenFormatter.h
@@ -0,0 +1,44 @@
//
// NSString+KentekenFormatter.h
//
// Created by Mathijs Kadijk on 03-09-12.
// Copyright (c) 2012 Wrep. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, WRPKentekenSidecode) {
WRPKentekenSidecodeBijzondereToelating = -3,
WRPKentekenSidecodeInternationaalGerechtshof = -2,
WRPKentekenSidecodeKoninklijkHuis = -1,
WRPKentekenSidecodeUnknown = 0,
WRPKentekenSidecode1 = 1,
WRPKentekenSidecode2 = 2,
WRPKentekenSidecode3 = 3,
WRPKentekenSidecode4 = 4,
WRPKentekenSidecode5 = 5,
WRPKentekenSidecode6 = 6,
WRPKentekenSidecode7 = 7,
WRPKentekenSidecode8 = 8,
WRPKentekenSidecode9 = 9,
WRPKentekenSidecode10 = 10
};

@interface NSString (KentekenFormatter)


/**
Format een string naar een Nederlands kenteken. Sidecode 1 t/m 10 worden ondersteund.
@discussion Corrupte kentekens worden zo goed en zo kwaad als het gaat geformat, dit zodat deze functie ook nuttig is om te gebruiken tijdens het invoeren van de gebruiker. Voor meer informatie over kenteken formaten en sidecodes zie http://nl.wikipedia.org/wiki/Nederlands_kenteken
*/
- (NSString *)kentekenFormat;

/**
Check welke sidecode dit kenteken is.
@discussion Voor meer informatie over kenteken sidecodes zie http://nl.wikipedia.org/wiki/Nederlands_kenteken
*/
- (WRPKentekenSidecode)kentekenSidecode;

@end
121 changes: 121 additions & 0 deletions Classes/NSString+KentekenFormatter.m
@@ -0,0 +1,121 @@
//
// NSString+KentekenFormatter.m
//
// Created by Mathijs Kadijk on 03-09-12.
// Copyright (c) 2012 Wrep. All rights reserved.
//

#import "NSString+KentekenFormatter.h"

@implementation NSString (KentekenFormatter)

- (NSString *)kentekenFormat
{
// If string if empty just return an empty string
if ([self length] == 0)
{
return @"";
}

// Strip non-alphanumeric characters
NSCharacterSet *nonAlphaNumericCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSString *workingString = [[self componentsSeparatedByCharactersInSet:nonAlphaNumericCharacters] componentsJoinedByString:@""];

// Uppercase the string
workingString = [workingString uppercaseString];

// Initialize some variables
char curChar = [workingString characterAtIndex:0];
BOOL lastCharWasNumber = (curChar >= '0' && curChar <= '9');
int charsOfTheSameTypeInARow = 0;

// Loop through all characters
for (int i = 0; i < [workingString length]; i++)
{
curChar = [workingString characterAtIndex:i];
BOOL curCharIsNumber = (curChar >= '0' && curChar <= '9');

if (curCharIsNumber != lastCharWasNumber)
{
// We are switching from letters to numbers or vice versa, add dash and reset counter
workingString = [workingString stringByReplacingCharactersInRange:NSMakeRange(i, 0) withString:@"-"];
charsOfTheSameTypeInARow = 0;
}
else
{
// Count the number of letters/numbers in a row
charsOfTheSameTypeInARow++;

if (charsOfTheSameTypeInARow >= 4)
{
// There are 4 of the same type of characters in a row, add dash between number 2 and 3 and set the counter to the correct amount
workingString = [workingString stringByReplacingCharactersInRange:NSMakeRange(i-1, 0) withString:@"-"];
charsOfTheSameTypeInARow = 1;
}
}

// Save the type of this character for the next iteration
lastCharWasNumber = curCharIsNumber;
}

// Return the result
return workingString;
}

- (WRPKentekenSidecode)kentekenSidecode
{
// Initialize sidecode regexps
NSArray *sidecodeExpressions = @[
@{@"regexp" : @"^ZZ-[0-9]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecodeBijzondereToelating)},
@{@"regexp" : @"^CDJ-[0-9]{3}$", @"sidecode" : @(WRPKentekenSidecodeInternationaalGerechtshof)},
@{@"regexp" : @"^AA-[0-9]{2,3}(-[0-9]{2})?$", @"sidecode" : @(WRPKentekenSidecodeKoninklijkHuis)},
@{@"regexp" : @"^[A-Z]{2}-[0-9]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode1)},
@{@"regexp" : @"^[0-9]{2}-[0-9]{2}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode2)},
@{@"regexp" : @"^[0-9]{2}-[A-Z]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode3)},
@{@"regexp" : @"^[A-Z]{2}-[0-9]{2}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode4)},
@{@"regexp" : @"^[A-Z]{2}-[A-Z]{2}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode5)},
@{@"regexp" : @"^[0-9]{2}-[A-Z]{2}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode6)},
@{@"regexp" : @"^[0-9]{2}-[A-Z]{3}-[0-9]{1}$", @"sidecode" : @(WRPKentekenSidecode7)},
@{@"regexp" : @"^[0-9]{1}-[A-Z]{3}-[0-9]{2}$", @"sidecode" : @(WRPKentekenSidecode8)},
@{@"regexp" : @"^[A-Z]{2}-[0-9]{3}-[A-Z]{1}$", @"sidecode" : @(WRPKentekenSidecode9)},
@{@"regexp" : @"^[A-Z]{1}-[0-9]{3}-[A-Z]{2}$", @"sidecode" : @(WRPKentekenSidecode10)},
];

// Format kenteken
NSString *kentekenString = [self kentekenFormat];

// Walk through all sidecode options
for (NSDictionary *sidecodeInfo in sidecodeExpressions)
{
// Initialize regexp
NSError *error;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:sidecodeInfo[@"regexp"]
options:0
error:&error];

#if DEBUG
// Check for initialization errors
if (error)
{
NSLog(@"NSString+KentekenFormatter could not initialize regular expression: %@", sidecodeInfo[@"regexp"]);
}
#endif

// Calculate if this string matches the regexp
NSRange matchedRange = [regexp rangeOfFirstMatchInString:kentekenString
options:0
range:NSMakeRange(0, kentekenString.length)];

// If we have a match...
if (matchedRange.location != NSNotFound)
{
// Return the correct sidecode
return [sidecodeInfo[@"sidecode"] integerValue];
}
}

// No matched, so sidecode unknown
return WRPKentekenSidecodeUnknown;
}

@end
28 changes: 22 additions & 6 deletions kenteken-formatter.xcodeproj/project.pbxproj
Expand Up @@ -19,7 +19,8 @@
D50D958615F7E0EE00A93E36 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D50D956615F7E0ED00A93E36 /* UIKit.framework */; };
D50D958715F7E0EE00A93E36 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D50D956815F7E0ED00A93E36 /* Foundation.framework */; };
D50D958F15F7E0EE00A93E36 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = D50D958D15F7E0EE00A93E36 /* InfoPlist.strings */; };
D50D959215F7E0EE00A93E36 /* kenteken_formatterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D50D959115F7E0EE00A93E36 /* kenteken_formatterTests.m */; };
D50D959E15F7E12A00A93E36 /* NSString+KentekenFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = D50D959D15F7E12A00A93E36 /* NSString+KentekenFormatter.m */; };
D50D95A115F7E14100A93E36 /* KentekenFormatterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D50D95A015F7E14100A93E36 /* KentekenFormatterTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -50,8 +51,10 @@
D50D958415F7E0EE00A93E36 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
D50D958C15F7E0EE00A93E36 /* kenteken-formatterTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "kenteken-formatterTests-Info.plist"; sourceTree = "<group>"; };
D50D958E15F7E0EE00A93E36 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
D50D959015F7E0EE00A93E36 /* kenteken_formatterTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = kenteken_formatterTests.h; sourceTree = "<group>"; };
D50D959115F7E0EE00A93E36 /* kenteken_formatterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = kenteken_formatterTests.m; sourceTree = "<group>"; };
D50D959C15F7E12A00A93E36 /* NSString+KentekenFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+KentekenFormatter.h"; sourceTree = "<group>"; };
D50D959D15F7E12A00A93E36 /* NSString+KentekenFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+KentekenFormatter.m"; sourceTree = "<group>"; };
D50D959F15F7E14100A93E36 /* KentekenFormatterTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KentekenFormatterTests.h; sourceTree = "<group>"; };
D50D95A015F7E14100A93E36 /* KentekenFormatterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KentekenFormatterTests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -81,6 +84,7 @@
D50D955715F7E0ED00A93E36 = {
isa = PBXGroup;
children = (
D50D959B15F7E10500A93E36 /* Classes */,
D50D956C15F7E0ED00A93E36 /* kenteken-formatter */,
D50D958A15F7E0EE00A93E36 /* kenteken-formatterTests */,
D50D956515F7E0ED00A93E36 /* Frameworks */,
Expand Down Expand Up @@ -135,8 +139,8 @@
D50D958A15F7E0EE00A93E36 /* kenteken-formatterTests */ = {
isa = PBXGroup;
children = (
D50D959015F7E0EE00A93E36 /* kenteken_formatterTests.h */,
D50D959115F7E0EE00A93E36 /* kenteken_formatterTests.m */,
D50D959F15F7E14100A93E36 /* KentekenFormatterTests.h */,
D50D95A015F7E14100A93E36 /* KentekenFormatterTests.m */,
D50D958B15F7E0EE00A93E36 /* Supporting Files */,
);
path = "kenteken-formatterTests";
Expand All @@ -151,6 +155,15 @@
name = "Supporting Files";
sourceTree = "<group>";
};
D50D959B15F7E10500A93E36 /* Classes */ = {
isa = PBXGroup;
children = (
D50D959C15F7E12A00A93E36 /* NSString+KentekenFormatter.h */,
D50D959D15F7E12A00A93E36 /* NSString+KentekenFormatter.m */,
);
path = Classes;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -262,14 +275,15 @@
D50D957315F7E0EE00A93E36 /* main.m in Sources */,
D50D957715F7E0EE00A93E36 /* WRPAppDelegate.m in Sources */,
D50D957D15F7E0EE00A93E36 /* WRPViewController.m in Sources */,
D50D959E15F7E12A00A93E36 /* NSString+KentekenFormatter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D50D957E15F7E0EE00A93E36 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D50D959215F7E0EE00A93E36 /* kenteken_formatterTests.m in Sources */,
D50D95A115F7E14100A93E36 /* KentekenFormatterTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -364,6 +378,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "kenteken-formatter/kenteken-formatter-Prefix.pch";
INFOPLIST_FILE = "kenteken-formatter/kenteken-formatter-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -375,6 +390,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "kenteken-formatter/kenteken-formatter-Prefix.pch";
INFOPLIST_FILE = "kenteken-formatter/kenteken-formatter-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions kenteken-formatterTests/KentekenFormatterTests.h
@@ -0,0 +1,15 @@
//
// KentekenFormatterTests.h
// Schaderegistratie
//
// Created by Mathijs Kadijk on 03-09-12.
// Copyright (c) 2012 Bultman. All rights reserved.
//

#import <SenTestingKit/SenTestingKit.h>

@interface KentekenFormatterTests : SenTestCase {
NSArray *kentekenData;
}

@end

0 comments on commit 825b6bc

Please sign in to comment.