Skip to content

Commit

Permalink
Add UK National Insurance Number (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mortware authored and bchavez committed Jul 5, 2018
1 parent dcfda64 commit 57f4257
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
20 changes: 19 additions & 1 deletion Source/Bogus.Tests/ExtensionTests/ExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -235,4 +253,4 @@ public void codice_fiscale_can_be_computed_from_finance_class()
codiceFiscale.Should().StartWith("RSSMRA90D23");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus.DataSets;
using System.Linq;
using Bogus.DataSets;

namespace Bogus.Extensions.UnitedKingdom
{
Expand All @@ -22,5 +23,41 @@ public static string SortCode(this Finance finance, bool includeSeparator = true

return finance.Random.ReplaceNumbers(withoutSeparator);
}

/// <summary>
/// National Insurance Number
/// </summary>
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}");
}
}
}
}

0 comments on commit 57f4257

Please sign in to comment.