Skip to content

Commit 513dc85

Browse files
Maximilian PfundsteinJohannestegner
authored andcommitted
Add format option to ignore the separator
1 parent 8aeb56d commit 513dc85

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

Personnummer.Tests/PersonnummerTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,27 @@ public void TestSeparator(PersonnummerData ssn)
268268
Assert.Equal(sep, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true }).Separator);
269269
// Getting the separator from a short formatted none-separated person number is not actually possible if it is intended to be a +.
270270
}
271+
272+
[Theory]
273+
[ClassData(typeof(ValidSsnDataProvider))]
274+
[ClassData(typeof(ValidCnDataProvider))]
275+
public void TestIgnoreSeparatorWhenFormatting(PersonnummerData ssn)
276+
{
277+
var separators = "+-".ToCharArray();
278+
279+
var ps1 = Personnummer.Parse(ssn.LongFormat, new Personnummer.Options { AllowCoordinationNumber = true });
280+
var ps2 = Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true });
281+
var ps3 = Personnummer.Parse(ssn.SeparatedFormat,
282+
new Personnummer.Options { AllowCoordinationNumber = true });
283+
284+
Assert.True(ps1.Format(false, true).IndexOfAny(separators) == -1);
285+
Assert.True(ps2.Format(false, true).IndexOfAny(separators) == -1);
286+
Assert.True(ps3.Format(false, true).IndexOfAny(separators) == -1);
287+
288+
Assert.True(ps1.Format().IndexOfAny(separators) >= 0);
289+
Assert.True(ps2.Format().IndexOfAny(separators) >= 0);
290+
Assert.True(ps3.Format().IndexOfAny(separators) >= 0);
291+
}
271292

272293
[Theory]
273294
[ClassData(typeof(OrgNumberDataProvider))]

Personnummer/Personnummer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ public Personnummer(string ssn, Options? options = null)
126126
/// If longFormat is true, it will include the century (YYYYMMDD-/+XXXX)
127127
/// </summary>
128128
/// <param name="longFormat">If century should be included.</param>
129+
/// <param name="ignoreSeparator">Whether the separator should be ignored.</param>
129130
/// <returns>Formatted personal identity number.</returns>
130-
public string Format(bool longFormat = false)
131+
public string Format(bool longFormat = false, bool ignoreSeparator = false)
131132
{
132-
return $"{(longFormat ? FullYear : Year)}{Month}{Day}{Separator}{Numbers}";
133+
return ignoreSeparator ? $"{(longFormat ? FullYear : Year)}{Month}{Day}{Numbers}" : $"{(longFormat ? FullYear : Year)}{Month}{Day}{Separator}{Numbers}";
133134
}
134135

135136
/// <summary>

0 commit comments

Comments
 (0)