From fda668dcaf7792b3e58b053176ff00b4c4c03d8e Mon Sep 17 00:00:00 2001 From: Dmitrijs Minajevs Date: Thu, 7 Oct 2021 16:35:00 +0300 Subject: [PATCH] feat(card): add support for Switch (#63) * feat(card): add support for Switch * fix(Switch): fix switch card length options * fix(Switch): fix Switch test inline data --- src/Dedge.Cardizer/Dedge.Cardizer.fs | 38 ++++++++++++++++++++++++++++ src/Tests/Tests.fs | 24 ++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/Dedge.Cardizer/Dedge.Cardizer.fs b/src/Dedge.Cardizer/Dedge.Cardizer.fs index 6ab06e9..e1135a2 100644 --- a/src/Dedge.Cardizer/Dedge.Cardizer.fs +++ b/src/Dedge.Cardizer/Dedge.Cardizer.fs @@ -32,6 +32,12 @@ type From16To19 = | Eightteen = 18 | Nineteen = 19 +type From16To19Skip17 = + | Random = 0 + | Sixteen = 16 + | Eighteen = 18 + | Nineteen = 19 + type DinersClubInternationalLengthOptions = | Random = 0 | Fourteen = 14 @@ -558,6 +564,38 @@ type Cardizer = Cardizer.GenerateCard prefix 16 + /// Returns a random Switch number. + /// Random Switch number + /// + /// This sample shows how to call the method. + /// + /// void PrintSwitch() + /// { + /// Console.WriteLine(Cardizer.NextSwitch()); + /// Console.WriteLine(Cardizer.NextSwitch()); // randomized between 16, 18 or 19 + /// Console.WriteLine(Cardizer.NextSwitch(From16To19Skip17.Random)); // randomized between 16, 18 or 19 + /// Console.WriteLine(Cardizer.NextSwitch(From16To19Skip17.Sixteen)); + /// } + /// + /// + static member NextSwitch([] switchLengthOption) = + let length = + match switchLengthOption with + | From16To19Skip17.Random -> [ 16; 18; 19 ].[Cardizer.next 3] + | _ -> int switchLengthOption + + let prefix = + [ [ 4; 9; 0; 3 ] + [ 4; 9; 0; 5 ] + [ 4; 9; 1; 1 ] + [ 4; 9; 3; 6 ] + [ 5; 6; 4; 1; 8; 2 ] + [ 6; 3; 3; 1; 1; 0 ] + [ 6; 3; 3; 3 ] + [ 6; 7; 5; 9 ] ].[Cardizer.next 8] + + Cardizer.GenerateCard prefix length + /// Returns a random Visa Electron number. /// Random Visa Electron number /// diff --git a/src/Tests/Tests.fs b/src/Tests/Tests.fs index fd937f2..d6afd4d 100644 --- a/src/Tests/Tests.fs +++ b/src/Tests/Tests.fs @@ -308,6 +308,30 @@ let ``Should generate valid InstaPayment`` () = card |> should haveLength 16 card |> luhn |> should be LuhnCheck +[] +[] +[] +[] +let ``Should generate valid Switch`` length expectedLength = + let card = Cardizer.NextSwitch length + + let shortPrefix = card.Substring(0, 4) |> int + let longPrefix = card.Substring(0, 6) |> int + + let prefixInRange = + shortPrefix = 4903 + || shortPrefix = 4905 + || shortPrefix = 4911 + || shortPrefix = 4936 + || shortPrefix = 6333 + || shortPrefix = 6759 + || longPrefix = 564182 + || longPrefix = 633110 + + prefixInRange |> should be True + card |> should haveLength expectedLength + card |> luhn |> should be LuhnCheck + [] let ``Should generate valid Visa Electron`` () = let card = Cardizer.NextVisaElectron()