Last active
October 24, 2018 02:40
-
-
Save guzhenhuaGitHub/7b8b522ac15b50b14ea676be67206c3e to your computer and use it in GitHub Desktop.
Swift密码规则封装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum PasswordRule { | |
enum CharacterClass { | |
case upper, lower, digits, special, asciiPrintable, unicode | |
case custom(Set<Character>) | |
} | |
case required(CharacterClass) | |
case allowed(CharacterClass) | |
case maxConsecutive(UInt) | |
case minLength(UInt) | |
case maxLength(UInt) | |
} | |
extension PasswordRule: CustomStringConvertible { | |
var description: String { | |
switch self { | |
case .required(let characterClass): | |
return "required: \(characterClass)" | |
case .allowed(let characterClass): | |
return "allowed: \(characterClass)" | |
case .maxConsecutive(let length): | |
return "max-consecutive: \(length)" | |
case .minLength(let length): | |
return "minlength: \(length)" | |
case .maxLength(let length): | |
return "maxlength: \(length)" | |
} | |
} | |
} | |
extension PasswordRule.CharacterClass: CustomStringConvertible { | |
var description: String { | |
switch self { | |
case .upper: return "upper" | |
case .lower: return "lower" | |
case .digits: return "digits" | |
case .special: return "special" | |
case .asciiPrintable: return "ascii-printable" | |
case .unicode: return "unicode" | |
case .custom(let characters): | |
return "[" + String(characters) + "]" | |
} | |
} | |
} | |
extension Array where Element == PasswordRule { | |
var descriptor: String { | |
return self | |
.map { "\($0);" } | |
.joined(separator: " ") | |
} | |
} | |
extension UITextInputPasswordRules { | |
convenience init(rules: [PasswordRule]) { | |
self.init(descriptor: rules.descriptor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment