diff --git a/Source/Bogus.Tests/ExtensionTests/ExtensionTest.cs b/Source/Bogus.Tests/ExtensionTests/ExtensionTest.cs index 8b8cfbe1..60d5ddc4 100644 --- a/Source/Bogus.Tests/ExtensionTests/ExtensionTest.cs +++ b/Source/Bogus.Tests/ExtensionTests/ExtensionTest.cs @@ -17,6 +17,24 @@ public void can_create_sortcode() f.SortCode().Should().Be("61-86-06"); f.SortCode(false).Should().Be("064391"); } + + [Fact] + public void can_create_nino() + { + var f = new Finance(); + var nino = f.Nino(false); + var regex = @"^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z][0-9]{2}[0-9]{2}[0-9]{2}[ABCD]"; + nino.Should().MatchRegex(regex); + } + + [Fact] + public void can_create_separated_nino() + { + var f = new Finance(); + var nino = f.Nino(); + var regex = @"^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z]\s[0-9]{2}\s[0-9]{2}\s[0-9]{2}\s[ABCD]"; + nino.Should().MatchRegex(regex); + } [Fact] public void can_create_codice_fiscale() @@ -235,4 +253,4 @@ public void codice_fiscale_can_be_computed_from_finance_class() codiceFiscale.Should().StartWith("RSSMRA90D23"); } } -} \ No newline at end of file +} diff --git a/Source/Bogus/Extensions/UnitedKingdom/ExtensionsForUnitedKingdom.cs b/Source/Bogus/Extensions/UnitedKingdom/ExtensionsForUnitedKingdom.cs index 1f1d6d3c..c6480590 100644 --- a/Source/Bogus/Extensions/UnitedKingdom/ExtensionsForUnitedKingdom.cs +++ b/Source/Bogus/Extensions/UnitedKingdom/ExtensionsForUnitedKingdom.cs @@ -1,4 +1,5 @@ -using Bogus.DataSets; +using System.Linq; +using Bogus.DataSets; namespace Bogus.Extensions.UnitedKingdom { @@ -22,5 +23,41 @@ public static string SortCode(this Finance finance, bool includeSeparator = true return finance.Random.ReplaceNumbers(withoutSeparator); } + + /// + /// National Insurance Number + /// + public static string Nino(this Finance finance, bool includeSeparator = true) + { + const string valid1stPrefixChars = "ABCEGHJKLMNOPRSTWXYZ"; + //const string valid2ndPrefixChars = "ABCEGHJKLMN PRSTWXYZ"; + const string validSuffixChars = "ABCD"; + + var prefix = finance.Random.String2(2, chars: valid1stPrefixChars); + + if (prefix.EndsWith("O")) + { //second character in prefix can't end with an 'O' + //Remap O to an X. + prefix = prefix.Replace('O', 'X'); + } + + //check for invalid prefixes + if (prefix == "GB" || prefix == "BG" || prefix == "NK" || + prefix == "KN" || prefix == "TN" || prefix == "NT" || + prefix == "ZZ") + { + //if the prefix is any of the invalid prefixes, + //Remap an invalid prefix to a well known valid one. + prefix = "CE"; + } + + var suffix = finance.Random.String2(1, validSuffixChars); + + if (includeSeparator) + { + return finance.Random.ReplaceNumbers($"{prefix} ## ## ## {suffix}"); + } + return finance.Random.ReplaceNumbers($"{prefix}######{suffix}"); + } } -} \ No newline at end of file +}