Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from ef1rspb/master
Browse files Browse the repository at this point in the history
Add "Mir" card type & Swift 4 migration
  • Loading branch information
maxkramer authored Apr 19, 2018
2 parents e8b6ff1 + 016b83d commit 4b64534
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion SwiftLuhn.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "SwiftLuhn"
s.version = "0.2.1"
s.version = "0.2.2"
s.summary = "SwiftLuhn is a port of the Luhn algorithm, used for validating debit/credit card details."
s.description = <<-DESC
This is a port of the Luhn Algorithm, generally used for validating debit/credit card details, to Swift.
Expand Down
50 changes: 29 additions & 21 deletions SwiftLuhn/Classes/SwiftLuhn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,57 @@ open class SwiftLuhn {
case jcb
case maestro
case rupay
case mir
}

public enum CardError: Error {
case unsupported
case invalid
}

fileprivate class func regularExpression(for cardType: CardType) -> String {
switch cardType {
case .amex:
return "^3[47][0-9]{5,}$";
return "^3[47][0-9]{5,}$"
case .dinersClub:
return "^3(?:0[0-5]|[68][0-9])[0-9]{4,}$";
return "^3(?:0[0-5]|[68][0-9])[0-9]{4,}$"
case .discover:
return "^6(?:011|5[0-9]{2})[0-9]{3,}$";
return "^6(?:011|5[0-9]{2})[0-9]{3,}$"
case .jcb:
return "^(?:2131|1800|35[0-9]{3})[0-9]{3,}$";
return "^(?:2131|1800|35[0-9]{3})[0-9]{3,}$"
case .mastercard:
return "^5[1-5][0-9]{5,}$";
return "^5[1-5][0-9]{5,}$"
case .visa:
return "^4[0-9]{6,}$";
return "^4[0-9]{6,}$"
case .maestro:
return "^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$";
return "^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$"
case .rupay:
return "^6[0-9]{15}$";

return "^6[0-9]{15}$"
case .mir:
return "^220[0-9]{13}$"
}
}

fileprivate class func suggestionRegularExpression(for cardType: CardType) -> String {
switch cardType {
case .amex:
return "^3[47][0-9]+$";
return "^3[47][0-9]+$"
case .dinersClub:
return "^3(?:0[0-5]|[68][0-9])[0-9]+$";
return "^3(?:0[0-5]|[68][0-9])[0-9]+$"
case .discover:
return "^6(?:011|5[0-9]{2})[0-9]+$";
return "^6(?:011|5[0-9]{2})[0-9]+$"
case .jcb:
return "^(?:2131|1800|35[0-9]{3})[0-9]+$";
return "^(?:2131|1800|35[0-9]{3})[0-9]+$"
case .mastercard:
return "^5[1-5][0-9]+$";
return "^5[1-5][0-9]+$"
case .visa:
return "^4[0-9]+$";
return "^4[0-9]+$"
case .maestro:
return "^(5018|5020|5038|6304|6759|6761|6763)[0-9]+$";
return "^(5018|5020|5038|6304|6759|6761|6763)[0-9]+$"
case .rupay:
return "^6[0-9]+$";

return "^6[0-9]+$"
case .mir:
return "^220[0-9]+$"
}
}

Expand Down Expand Up @@ -147,10 +152,11 @@ public extension SwiftLuhn.CardType {
case .jcb:
return "JCB"
case .maestro:
return "Maestro";
return "Maestro"
case .rupay:
return "Rupay";

return "Rupay"
case .mir:
return "Mir"
}
}

Expand All @@ -172,6 +178,8 @@ public extension SwiftLuhn.CardType {
self.init(rawValue: 6)
case "rupay":
self.init(rawValue: 7)
case "mir":
self.init(rawValue: 8)
default:
return nil
}
Expand Down

0 comments on commit 4b64534

Please sign in to comment.