I have an extension to mask the first and last characters. When I try to mask last characters it works but if I try to mask the first characters it doesn't mask as I want to.
For example;
extension String {
func maskedChar(_ charCount: Int, lastCharacters: Bool = true) -> String {
if self.count <= charCount {
return self
}
let mask = String(repeating: "*", count: charCount)
return lastCharacters ? self.prefix(self.count - charCount) + mask : self.suffix(self.count - charCount) + mask
}
}
let sample = "123456"
print(sample.maskedChar(3))
print(sample.maskedChar(3, lastCharacters: false))
// 123***
// 456***
What I want to see is basically ***456.
And also, is there any way to shorten the code? The return line is too long to see it.