Skip to content

Commit

Permalink
Add format option to ignore the separator
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Pfundstein authored and Johannestegner committed May 12, 2022
1 parent 8aeb56d commit 513dc85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Personnummer.Tests/PersonnummerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ public void TestSeparator(PersonnummerData ssn)
Assert.Equal(sep, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true }).Separator);
// Getting the separator from a short formatted none-separated person number is not actually possible if it is intended to be a +.
}

[Theory]
[ClassData(typeof(ValidSsnDataProvider))]
[ClassData(typeof(ValidCnDataProvider))]
public void TestIgnoreSeparatorWhenFormatting(PersonnummerData ssn)
{
var separators = "+-".ToCharArray();

var ps1 = Personnummer.Parse(ssn.LongFormat, new Personnummer.Options { AllowCoordinationNumber = true });
var ps2 = Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true });
var ps3 = Personnummer.Parse(ssn.SeparatedFormat,
new Personnummer.Options { AllowCoordinationNumber = true });

Assert.True(ps1.Format(false, true).IndexOfAny(separators) == -1);
Assert.True(ps2.Format(false, true).IndexOfAny(separators) == -1);
Assert.True(ps3.Format(false, true).IndexOfAny(separators) == -1);

Assert.True(ps1.Format().IndexOfAny(separators) >= 0);
Assert.True(ps2.Format().IndexOfAny(separators) >= 0);
Assert.True(ps3.Format().IndexOfAny(separators) >= 0);
}

[Theory]
[ClassData(typeof(OrgNumberDataProvider))]
Expand Down
5 changes: 3 additions & 2 deletions Personnummer/Personnummer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ public Personnummer(string ssn, Options? options = null)
/// If longFormat is true, it will include the century (YYYYMMDD-/+XXXX)
/// </summary>
/// <param name="longFormat">If century should be included.</param>
/// <param name="ignoreSeparator">Whether the separator should be ignored.</param>
/// <returns>Formatted personal identity number.</returns>
public string Format(bool longFormat = false)
public string Format(bool longFormat = false, bool ignoreSeparator = false)
{
return $"{(longFormat ? FullYear : Year)}{Month}{Day}{Separator}{Numbers}";
return ignoreSeparator ? $"{(longFormat ? FullYear : Year)}{Month}{Day}{Numbers}" : $"{(longFormat ? FullYear : Year)}{Month}{Day}{Separator}{Numbers}";
}

/// <summary>
Expand Down

0 comments on commit 513dc85

Please sign in to comment.