Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for company registration number #103

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions samples/ConsoleSample.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace ConsoleSample
{
class Program
{
private static readonly string[] RawPersonalIdentityNumberSamples =
private static readonly string[] RawIndividualIdentityNumberSamples =
{
"990913+9801",
"120211+9986",
"990807-2391",
"180101-2392",

"180101.2392",
"199008672397",
"ABC",
};

Expand All @@ -23,9 +23,9 @@ static void Main(string[] args)
Console.WriteLine("Sample showing possible uses of SwedishPersonalIdentityNumber.");
WriteSpace();

foreach (var sample in RawPersonalIdentityNumberSamples)
foreach (var sample in RawIndividualIdentityNumberSamples)
{
WritePersonalIdentityNumberInfo(sample);
WriteIndividualIdentityNumberInfo(sample);
WriteSpace();
}

Expand All @@ -44,51 +44,61 @@ static void Main(string[] args)
Console.WriteLine("Here is a personal identity number that can be used for testing:");
Console.WriteLine("----------------------");
var randomPin = SwedishPersonalIdentityNumberTestData.GetRandom();
WritePersonalIdentityNumberInfo(randomPin);
WriteIndividualIdentityNumberInfo(IndividualIdentityNumber.FromSwedishPersonalIdentityNumber(randomPin));

WriteSpace();

Console.WriteLine("What is your (Swedish) Personal Identity Number?");
var userRawPersonalIdentityNumber = Console.ReadLine();
WritePersonalIdentityNumberInfo(userRawPersonalIdentityNumber);
WriteIndividualIdentityNumberInfo(userRawPersonalIdentityNumber);
WriteSpace();

Console.ReadLine();
}

private static void WritePersonalIdentityNumberInfo(string rawPersonalIdentityNumber)
private static void WriteIndividualIdentityNumberInfo(string rawIndividualIdentityNumber)
{
WriteHeader($"Input: {rawPersonalIdentityNumber}");
if (SwedishPersonalIdentityNumber.TryParse(rawPersonalIdentityNumber, out var personalIdentityNumber))
WriteHeader($"Input: {rawIndividualIdentityNumber}");
if (IndividualIdentityNumber.TryParse(rawIndividualIdentityNumber, out var identityNumber))
{
WritePersonalIdentityNumberInfo(personalIdentityNumber);
WriteIndividualIdentityNumberInfo(identityNumber);
}
else
{
Console.Error.WriteLine("Unable to parse the input as a SwedishPersonalIdentityNumber.");
Console.Error.WriteLine("Unable to parse the input as a IndividualIdentityNumber.");
}
}

private static void WritePersonalIdentityNumberInfo(SwedishPersonalIdentityNumber personalIdentityNumber)
private static void WriteIndividualIdentityNumberInfo(IndividualIdentityNumber identityNumber)
{
Console.WriteLine("SwedishPersonalIdentityNumber");
WriteKeyValueInfo(" .ToString()", personalIdentityNumber.ToString());
WriteKeyValueInfo(" .To10DigitString()", personalIdentityNumber.To10DigitString());
WriteKeyValueInfo(" .To12DigitString()", personalIdentityNumber.To12DigitString());
if (identityNumber.IsSwedishPersonalIdentityNumber)
{
Console.WriteLine("SwedishPersonalIdentityNumber");
}
else if (identityNumber.IsSwedishCoordinationNumber)
{
Console.WriteLine("SwedishCoordinationNumber");
}
WriteKeyValueInfo(" .ToString()", identityNumber.ToString());
WriteKeyValueInfo(" .To10DigitString()", identityNumber.To10DigitString());
WriteKeyValueInfo(" .To12DigitString()", identityNumber.To12DigitString());

WriteKeyValueInfo(" .Year", personalIdentityNumber.Year.ToString());
WriteKeyValueInfo(" .Month", personalIdentityNumber.Month.ToString());
WriteKeyValueInfo(" .Day", personalIdentityNumber.Day.ToString());
WriteKeyValueInfo(" .BirthNumber", personalIdentityNumber.BirthNumber.ToString());
WriteKeyValueInfo(" .Checksum", personalIdentityNumber.Checksum.ToString());
WriteKeyValueInfo(" .Year", identityNumber.Year.ToString());
WriteKeyValueInfo(" .Month", identityNumber.Month.ToString());
WriteKeyValueInfo(" .Day", identityNumber.Day.ToString());
WriteKeyValueInfo(" .BirthNumber", identityNumber.BirthNumber.ToString());
WriteKeyValueInfo(" .Checksum", identityNumber.Checksum.ToString());

WriteKeyValueInfo(" .GetDateOfBirthHint()", personalIdentityNumber.GetDateOfBirthHint().ToShortDateString());
WriteKeyValueInfo(" .GetAgeHint()", personalIdentityNumber.GetAgeHint().ToString());
WriteKeyValueInfo(" .GetDateOfBirthHint()", identityNumber.GetDateOfBirthHint().ToShortDateString());
WriteKeyValueInfo(" .GetAgeHint()", identityNumber.GetAgeHint().ToString());

WriteKeyValueInfo(" .GetGenderHint()", personalIdentityNumber.GetGenderHint().ToString());
WriteKeyValueInfo(" .GetGenderHint()", identityNumber.GetGenderHint().ToString());

// IsTestNumber is an extension method from the package ActiveLogin.Identity.Swedish.TestData
WriteKeyValueInfo(" .IsTestNumber()", personalIdentityNumber.IsTestNumber().ToString());
if (identityNumber.IsSwedishPersonalIdentityNumber)
{
// IsTestNumber is an extension method from the package ActiveLogin.Identity.Swedish.TestData
WriteKeyValueInfo(" .IsTestNumber()", identityNumber.SwedishPersonalIdentityNumber.IsTestNumber().ToString());
}
}

private static void WriteHeader(string header)
Expand Down
88 changes: 49 additions & 39 deletions samples/ConsoleSample.FSharp/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,81 @@ open System
open ActiveLogin.Identity.Swedish.FSharp
open ActiveLogin.Identity.Swedish.FSharp.TestData

let sampleStrings = [ "990913+9801"; "120211+9986"; "990807-2391"; "180101-2392"; "180101.2392"; "ABC" ]
let sampleStrings = [ "990913+9801"; "120211+9986"; "990807-2391"; "180101-2392"; "180101.2392" ]
let sampleCoordinationNumbers = [ "199008672397" ]
let sampleInvalidNumbers = [ "ABC" ]

let parseAndPrintPersonalIdentityNumber str =
let parseAndPrintIndividualIdentityNumber str =
let printHeader str =
str
|> printfn "Input: %s\n----------------------"

let print10DigitString pin =
pin
|> SwedishPersonalIdentityNumber.to10DigitString
|> printfn "SwedishPersonalIdentityNumber.to10DigitString: %A"
let print10DigitString num =
num
|> IndividualIdentityNumber.to10DigitString
|> printfn "IdentityNumber.to10DigitString: %A"

let print12DigitString pin =
pin
|> SwedishPersonalIdentityNumber.to12DigitString
|> printfn "SwedishPersonalIdentityNumber.to12DigitString: %s"
let print12DigitString num =
num
|> IndividualIdentityNumber.to12DigitString
|> printfn "IdentityNumber.to12DigitString: %s"

let printDateOfBirthHint pin =
let date = pin |> SwedishPersonalIdentityNumber.Hints.getDateOfBirthHint
date.ToShortDateString() |> printfn "SwedishPersonalIdentityNumber.Hints.getDateOfBirthHint: %s"
let printDateOfBirthHint num =
let date = num |> IndividualIdentityNumber.Hints.getDateOfBirthHint
date.ToShortDateString() |> printfn "IdentityNumber.Hints.getDateOfBirthHint: %s"

let printAgeHint pin =
pin
|> SwedishPersonalIdentityNumber.Hints.getAgeHintOnDate DateTime.UtcNow
let printAgeHint num =
num
|> IndividualIdentityNumber.Hints.getAgeHintOnDate DateTime.UtcNow
|> Option.defaultValue 0
|> printfn "SwedishPersonalIdentityNumber.Hints.getAgeHintOnDate: %i"
|> printfn "IdentityNumber.Hints.getAgeHintOnDate: %i"

let printGenderHint pin =
let gender = pin |> SwedishPersonalIdentityNumber.Hints.getGenderHint
gender.ToString() |> printfn "SwedishPersonalIdentityNumber.Hints.getGenderHint: %s"
let gender = pin |> IndividualIdentityNumber.Hints.getGenderHint
gender.ToString() |> printfn "IdentityNumber.Hints.getGenderHint: %s"

let printIsTestNumber pin =
let printIsTestNumber num =
// isTestNumber is an extension from the package ActiveLogin.Identity.Swedish.FSharp.TestData
pin
|> SwedishPersonalIdentityNumber.isTestNumber
|> printfn "SwedishPersonalIdentityNumber.isTestNumber: %b"
match num with
| Personal pin ->
pin
|> SwedishPersonalIdentityNumber.isTestNumber
|> printfn "SwedishPersonalIdentityNumber.isTestNumber: %b"
| Coordination _ -> printfn "Testnumber check is not implemented for coordination numbers"

printHeader str
match SwedishPersonalIdentityNumber.parse str with
| Ok pin ->
printfn "SwedishPersonalIdentityNumber:"
printfn "%A" pin
print10DigitString pin
print12DigitString pin
printDateOfBirthHint pin
printAgeHint pin
printGenderHint pin
printIsTestNumber pin
| Error e -> printfn "%A: Unable to parse the input as a SwedishPersonalIdentityNumber." e
match IndividualIdentityNumber.parse str with
| Ok num ->
match num with
| Personal pin ->
printfn "SwedishPersonalIdentityNumber:"
printfn "%A" pin
| Coordination cNum ->
printfn "SwedishCoordinationNumber:"
printfn "%A" cNum
print10DigitString num
print12DigitString num
printDateOfBirthHint num
printAgeHint num
printGenderHint num
printIsTestNumber num
| Error e -> printfn "%A: Unable to parse the input as an IndividualIdentityNumber." e
printf "\n\n"

[<EntryPoint>]
let main argv =
printfn "Sample showing possible uses of SwedishPersonalIdentityNumber."
printf "\n\n"

sampleStrings |> List.iter parseAndPrintPersonalIdentityNumber
sampleStrings @ sampleCoordinationNumbers @ sampleInvalidNumbers |> List.iter parseAndPrintIndividualIdentityNumber

printfn "Here is a valid 10 digit string that can be used for testing:\n----------------------"
printfn "Here is a valid 10 digit string personal identity number that can be used for testing:\n----------------------"
SwedishPersonalIdentityNumberTestData.getRandom()
|> SwedishPersonalIdentityNumber.to10DigitString
|> printfn "%A"
printf "\n\n"

printfn "Here is a valid 12 digit string that can be used for testing:\n----------------------"
printfn "Here is a valid 12 digit personal identity number string that can be used for testing:\n----------------------"
SwedishPersonalIdentityNumberTestData.getRandom()
|> SwedishPersonalIdentityNumber.to12DigitString
|> printfn "%s"
Expand All @@ -82,8 +92,8 @@ let main argv =
|> printfn "Is it a test number? %b!"
printf "\n\n"

printfn "What is your (Swedish) Personal Identity Number?"
printfn "What is your (Swedish) Identity Number?"
let userInput = Console.ReadLine()
parseAndPrintPersonalIdentityNumber userInput
parseAndPrintIndividualIdentityNumber userInput
Console.ReadLine() |> ignore
0 // return an integer exit code
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<None Include="StrongNameKey.snk" />
<Compile Include="AllPins.fs" />
<Compile Include="AllCoordNums.fs" />
<Compile Include="TestData.fs" />
<Compile Include="TestDataCSharp.fs" />
</ItemGroup>
Expand Down
Loading