Skip to content

Instantly share code, notes, and snippets.

@TucoBZ
Last active September 27, 2017 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TucoBZ/32cdf925a2ccb4b6b6bbde1f33acea97 to your computer and use it in GitHub Desktop.
Save TucoBZ/32cdf925a2ccb4b6b6bbde1f33acea97 to your computer and use it in GitHub Desktop.
[Swift] - Minify Email
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 = "glauber@gmail.com"
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