Skip to content

Commit

Permalink
Merge pull request #5 from jellyhive/0.1.2
Browse files Browse the repository at this point in the history
Support for .NET Standard 2.0
  • Loading branch information
jonaswikstrom committed Jun 2, 2023
2 parents a37d70c + 85e7f4d commit 9f5473f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/csharp.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build & Test

on:
push:
pull_request:
branches: [ main ]


jobs:
build:

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/jellyhive/swedishbankaccounts/csharp.yml?branch=main)](https://github.com/jellyhive/swedishbankaccounts/actions)
[![NuGet](http://img.shields.io/nuget/v/SwedishBankAccounts.svg)](https://www.nuget.org/packages/SwedishBankAccounts/)
[![Downloads](https://img.shields.io/nuget/dt/SwedishBankAccounts)](#)

# Swedish Bank Account for C#

Expand Down
23 changes: 11 additions & 12 deletions src/SwedishBankAccounts/BankAccountNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ private BankAccountNumber(string bank, string sortingCode, string accountNumber)
/// <returns>The parsed bank account number</returns>
public static BankAccountNumber Parse(string value)
{
if(!TryParse(value, InitOptions.Strict, out var bankAccountNumber))
{
throw new FormatException("Invalid: " + nameof(value));
}
return bankAccountNumber;
return !TryParse(value, InitOptions.Strict, out var bankAccountNumber)
? throw new FormatException("Invalid: " + nameof(value))
: bankAccountNumber!;
}
/// <summary>
/// Parses a bank account number
Expand All @@ -52,8 +50,9 @@ public static BankAccountNumber Parse(string value)
/// <returns>The parsed bank account number</returns>
public static BankAccountNumber Parse(string value, InitOptions initOptions)
{
if (TryParse(value, initOptions, out var bankAccountNumber)) throw new FormatException("Invalid: " + nameof(value));
return bankAccountNumber;
return TryParse(value, initOptions, out var bankAccountNumber)
? throw new FormatException("Invalid: " + nameof(value))
: bankAccountNumber!;
}

/// <summary>
Expand Down Expand Up @@ -82,15 +81,15 @@ public static bool TryParse(string value, InitOptions? initOptions, out BankAcco
bankAccountNumber = null;
value = Regex.Replace(value, @"[^\d]", "");

if (value.StartsWith('8') && value.Length < 7) return false;
if(!value.StartsWith('8') && value.Length < 6) return false;
if (value.StartsWith("8") && value.Length < 7) return false;
if(!value.StartsWith("8") && value.Length < 6) return false;

var sortingCode = value[..(value.StartsWith('8') ? 5 : 4)];
var accountNumber = value[(value.StartsWith('8') ? 5 : 4)..];
var sortingCode = value.Substring(0, value.StartsWith("8") ? 5 : 4);
var accountNumber = value.Substring(value.StartsWith("8") ? 5 : 4);

if (sortingCode.Length is < 4 or > 4 && !Modulus10.Validate(sortingCode)) return false;

var bank = SwedishBankAccounts.Bank.Banks.SingleOrDefault(s => s.HasSortingCode(sortingCode[..4]));
var bank = SwedishBankAccounts.Bank.Banks.SingleOrDefault(s => s.HasSortingCode(sortingCode.Substring(0, 4)));
if(bank == null) return false;

var valid = bank.BankAccountNumberType.Validate(sortingCode, accountNumber);
Expand Down
2 changes: 1 addition & 1 deletion src/SwedishBankAccounts/BankAccountNumberType1A.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public record BankAccountNumberType1A : BankAccountNumberType
{
public override bool Validate(string sortingCode, string accountNumber) =>
Modulus11.Validate($"{sortingCode[1..]}{accountNumber.PadLeft(7, '0')}");
Modulus11.Validate($"{sortingCode.Substring(1)}{accountNumber.PadLeft(7, '0')}");
}
2 changes: 1 addition & 1 deletion src/SwedishBankAccounts/Modulus10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Modulus10
/// <returns>True if the number is valid according to to the modulus-10 method, otherwise false</returns>
public static bool Validate(string number)
{
var checkDigit = CalculateCheckDigit(number[..^1]);
var checkDigit = CalculateCheckDigit(number.Substring(0, number.Length - 1));
return number.Last() - 48 == checkDigit;
}

Expand Down
2 changes: 1 addition & 1 deletion src/SwedishBankAccounts/Modulus11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class Modulus11
/// <returns>True if the number is valid according to to the modulus-11 method, otherwise false</returns>
public static bool Validate(string number)
{
var checkDigit = CalculateCheckDigit(number[..^1]);
var checkDigit = CalculateCheckDigit(number.Substring(0, number.Length - 1));
return number.Last() - 48 == checkDigit;
}

Expand Down
2 changes: 1 addition & 1 deletion src/SwedishBankAccounts/SwedishBankAccounts.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
Expand Down

0 comments on commit 9f5473f

Please sign in to comment.