Last active
September 27, 2017 19:13
-
-
Save TucoBZ/32cdf925a2ccb4b6b6bbde1f33acea97 to your computer and use it in GitHub Desktop.
[Swift] - Minify Email
This file contains 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
import Foundation | |
extension String { | |
/// Return a mimified email from this string, like glau***@gmail.com, the mimified length is half as the email length | |
var emailMinified : String? { | |
get { | |
var email = self | |
if let atIndex = email.components(separatedBy: "@").first?.endIndex { | |
let middleIndex = atIndex.encodedOffset/2 | |
let offset = atIndex.encodedOffset - middleIndex | |
let start = email.index(email.startIndex, offsetBy: offset) | |
let mimify = String(repeating: "*", count: atIndex.encodedOffset - start.encodedOffset) | |
email.replaceSubrange(start..<atIndex, with: mimify) | |
return email | |
} | |
return nil | |
} | |
} | |
} | |
let newEmail = "[email protected]" | |
print(newEmail.emailMinified ?? "can't be minified") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment