Skip to content

Commit

Permalink
feat(card): improve Discover support (#60)
Browse files Browse the repository at this point in the history
* feat(card): improve Discover support
* style: indent

Fix #44
  • Loading branch information
aloisdg authored Oct 5, 2021
1 parent 9d8b383 commit e78c70c
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 96 deletions.
154 changes: 82 additions & 72 deletions src/Dedge.Cardizer/Dedge.Cardizer.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Dedge
namespace Dedge

open System
open System.Threading
Expand All @@ -25,7 +25,7 @@ type From12To19 =
| Eightteen = 18
| Nineteen = 19

type From16To19 =
type From16To19 =
| Random = 0
| Sixteen = 16
| Seventeen = 17
Expand Down Expand Up @@ -81,7 +81,8 @@ type Cardizer =
/// <param name="high">The (inclusive) high value of the range</param>
/// <returns>Random integer enumerate as sequence within a given range</returns>
static member private NextSeqInRange low high =
Cardizer.NextInRange low high |> Cardizer.NumberToSeq
Cardizer.NextInRange low high
|> Cardizer.NumberToSeq

static member private GetNumber n =
let n2 = n * 2
Expand All @@ -92,11 +93,16 @@ type Cardizer =
static member private NextUniquePersonalIdentifiers n =
{ 1 .. n } |> Seq.map (fun _ -> Cardizer.next 10)

static member private ReverseSum (digits: seq<int>): int =
static member private ReverseSum(digits: seq<int>) : int =
digits
|> Seq.rev
|> Seq.mapi (fun i n -> i % 2 = 0, n)
|> Seq.sumBy (fun (isEven, n) -> if isEven then Cardizer.GetNumber n else n)
|> Seq.sumBy
(fun (isEven, n) ->
if isEven then
Cardizer.GetNumber n
else
n)
|> Cardizer.CheckDigit

static member private AppendSum digits =
Expand Down Expand Up @@ -136,7 +142,7 @@ type Cardizer =
match visaLengthOption with
| VisaLengthOptions.Random -> if Cardizer.next 2 = 0 then 13 else 16
| _ -> int visaLengthOption


Cardizer.GenerateCard [ 4 ] length

Expand Down Expand Up @@ -235,7 +241,7 @@ type Cardizer =
/// </example>
static member NextAmex() =
let second = if Cardizer.next 2 = 0 then 4 else 7
Cardizer.GenerateCard [3; second] 15
Cardizer.GenerateCard [ 3; second ] 15

/// <summary>Returns a random Discover number that is of the given available length.</summary>
/// <param name="discoverLengthOption">Credit card's length (default is randomized between 16 and 19)</param>
Expand All @@ -257,7 +263,19 @@ type Cardizer =
| From16To19.Random -> Cardizer.NextInRange 16 19
| _ -> int discoverLengthOption

Cardizer.GenerateCard [ 6; 0; 1; 1 ] length
let roll = Cardizer.next 4

let prefix =
if roll = 0 then
[ 6; 0; 1; 1 ]
elif roll = 1 then
Cardizer.NextSeqInRange 622126 622925
elif roll = 2 then
Cardizer.NextSeqInRange 644 649
else
[ 6; 5 ]

Cardizer.GenerateCard prefix length

/// <summary>Returns a random MasterCard number.</summary>
/// <returns>Random MasterCard number</returns>
Expand All @@ -272,9 +290,10 @@ type Cardizer =
/// </example>
static member NextMasterCard() =
let prefixes =
if Cardizer.next 2 = 0
then Cardizer.NextSeqInRange 51 55
else Cardizer.NextSeqInRange 2221 2720
if Cardizer.next 2 = 0 then
Cardizer.NextSeqInRange 51 55
else
Cardizer.NextSeqInRange 2221 2720

Cardizer.GenerateCard prefixes 16

Expand All @@ -290,8 +309,7 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextUatp () =
Cardizer.GenerateCard [1] 15
static member NextUatp() = Cardizer.GenerateCard [ 1 ] 15

/// <summary>Returns a random RuPay number.</summary>
/// <returns>Random RuPay number</returns>
Expand All @@ -304,31 +322,26 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextRuPay([<Optional; DefaultParameterValue(true)>] acceptCoBranded: bool) =
static member NextRuPay([<Optional; DefaultParameterValue(true)>] acceptCoBranded: bool) =
let prefixRuPay =
[ [ 6; 0 ]
[ 6; 5 ]
[ 6; 5 ]
[ 8; 1 ]
[ 8; 2 ]
[ 5; 0; 8 ]
]
[ 5; 0; 8 ] ]

let prefixRuPayAndJcbCobranded =
[ [ 3; 5; 3 ]
[ 3; 5; 6 ]
]
let prefixRuPayAndJcbCobranded = [ [ 3; 5; 3 ]; [ 3; 5; 6 ] ]

if acceptCoBranded
then
if acceptCoBranded then
let merge =
[ prefixRuPay
prefixRuPayAndJcbCobranded ].[Cardizer.next 2]
if merge.Length = 2
then
Cardizer.GenerateCard merge.[Cardizer.next 2] 16

if merge.Length = 2 then
Cardizer.GenerateCard merge.[Cardizer.next 2] 16
else
Cardizer.GenerateCard merge.[Cardizer.next 5] 16
else
Cardizer.GenerateCard merge.[Cardizer.next 5] 16
else
Cardizer.GenerateCard prefixRuPay.[Cardizer.next 5] 16

/// <summary>Returns a random DinersClubInternational number.</summary>
Expand All @@ -342,12 +355,15 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextDinersClubInternational([<Optional; DefaultParameterValue(DinersClubInternationalLengthOptions.Random)>] dinersLengthOption) =
static member NextDinersClubInternational
([<Optional; DefaultParameterValue(DinersClubInternationalLengthOptions.Random)>] dinersLengthOption)
=
let length =
match dinersLengthOption with
| DinersClubInternationalLengthOptions.Random -> Cardizer.NextInRange 14 19
| _ -> int dinersLengthOption
Cardizer.GenerateCard [ 3; 6 ] length

Cardizer.GenerateCard [ 3; 6 ] length

/// <summary>Returns a random DinersClubUsAndCanada number.</summary>
/// <returns>Random DinersClubUsAndCanada number</returns>
Expand All @@ -360,8 +376,7 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextDinersClubUsAndCanada () =
Cardizer.GenerateCard [ 5; 4 ] 16
static member NextDinersClubUsAndCanada() = Cardizer.GenerateCard [ 5; 4 ] 16

/// <summary>Returns a random DinersClubInternational or DinersClubUsAndCanada number.</summary>
/// <returns>Random DinersClubInternational or DinersClubUsAndCanada number</returns>
Expand All @@ -375,9 +390,10 @@ type Cardizer =
/// </code>
/// </example>
static member NextDinersClub() =
if Cardizer.next 2 = 0
then Cardizer.NextDinersClubUsAndCanada()
else Cardizer.NextDinersClubInternational()
if Cardizer.next 2 = 0 then
Cardizer.NextDinersClubUsAndCanada()
else
Cardizer.NextDinersClubInternational()

/// <summary>Returns a random Maestro number.</summary>
/// <returns>Random Maestro number</returns>
Expand All @@ -397,19 +413,18 @@ type Cardizer =
| _ -> int maestroLengthOption

let prefix =
[
[ 5; 0; 1; 8 ]
[ 5; 0; 2; 0 ]
[ 5; 0; 3; 8 ]
[ 5; 8; 9; 3 ]
[ 6; 3; 0; 4 ]
[ 6; 7; 5; 9 ]
[ 6; 7; 6; 1 ]
[ 6; 7; 6; 2 ]
[ 6; 7; 6; 3 ]].[Cardizer.next 9]
[ [ 5; 0; 1; 8 ]
[ 5; 0; 2; 0 ]
[ 5; 0; 3; 8 ]
[ 5; 8; 9; 3 ]
[ 6; 3; 0; 4 ]
[ 6; 7; 5; 9 ]
[ 6; 7; 6; 1 ]
[ 6; 7; 6; 2 ]
[ 6; 7; 6; 3 ] ].[Cardizer.next 9]

Cardizer.GenerateCard prefix length

/// <summary>Returns a random Dankort number.</summary>
/// <returns>Random Dankort number</returns>
/// <example>
Expand All @@ -421,17 +436,17 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextDankort([<Optional; DefaultParameterValue(true)>] acceptCoBranded: bool) =
static member NextDankort([<Optional; DefaultParameterValue(true)>] acceptCoBranded: bool) =
let prefixDankort = [ 5; 0; 1; 9 ]
let prefixVisaCobranded = [ 4; 5; 7; 1 ]

let prefix =
if acceptCoBranded
then
[ prefixDankort
prefixVisaCobranded ].[Cardizer.next 2]
else prefixDankort

Cardizer.GenerateCard prefix 16
if acceptCoBranded then
[ prefixDankort; prefixVisaCobranded ].[Cardizer.next 2]
else
prefixDankort

Cardizer.GenerateCard prefix 16

/// <summary>Returns a random InterPayment number.</summary>
/// <returns>Random InterPayment number</returns>
Expand Down Expand Up @@ -482,8 +497,7 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextTunion() =
Cardizer.GenerateCard [ 3; 1 ] 19
static member NextTunion() = Cardizer.GenerateCard [ 3; 1 ] 19

/// <summary>Returns a random LankaPay number.</summary>
/// <returns>Random LankaPay number</returns>
Expand All @@ -496,7 +510,7 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextLankaPay () =
static member NextLankaPay() =
let prefix = [ 3; 5; 7; 1; 1; 1 ]
Cardizer.GenerateCard prefix 16

Expand All @@ -518,12 +532,10 @@ type Cardizer =
| _ -> int laserLengthOption

let prefix =
[
[ 6; 3; 0; 4 ]
[ 6; 7; 0; 6 ]
[ 6; 7; 7; 1 ]
[ 6; 7; 0; 9 ]
].[Cardizer.next 4]
[ [ 6; 3; 0; 4 ]
[ 6; 7; 0; 6 ]
[ 6; 7; 7; 1 ]
[ 6; 7; 0; 9 ] ].[Cardizer.next 4]

Cardizer.GenerateCard prefix length

Expand All @@ -538,12 +550,10 @@ type Cardizer =
/// }
/// </code>
/// </example>
static member NextInstaPayment () =
let prefix =
[
[6; 3; 7]
[6; 3; 8]
[6; 3; 9]
].[Cardizer.next 3]

Cardizer.GenerateCard prefix 16
static member NextInstaPayment() =
let prefix =
[ [ 6; 3; 7 ]
[ 6; 3; 8 ]
[ 6; 3; 9 ] ].[Cardizer.next 3]

Cardizer.GenerateCard prefix 16
Loading

0 comments on commit e78c70c

Please sign in to comment.