reik.schatz
05/17/2019, 9:57 AM*
?Jonathan Mew
05/17/2019, 10:08 AMfun anonymise(s: String, n: Int): String {
val replaceLength = kotlin.math.min(s.length, n)
val replaceChars = String(CharArray(replaceLength, {'*'} ))
return "$replaceChars${s.substring(replaceLength)}"
}
@Test
fun `anonymise`() {
anonymise("blah", 3).shouldBe("***h")
anonymise("blah", 4).shouldBe("****")
anonymise("blah", 5).shouldBe("****")
}
Jonathan Mew
05/17/2019, 10:08 AMreik.schatz
05/17/2019, 10:11 AMDaniel Garibaldi
05/17/2019, 10:32 AMDaniel Garibaldi
05/17/2019, 10:32 AMDaniel Garibaldi
05/17/2019, 10:32 AMfun String.anonymise(numberOfCharactersToHide: Int = this.length - 1) =
replaceRange(0,
kotlin.math.min(this.length -1, numberOfCharactersToHide),
"*")
Daniel Garibaldi
05/17/2019, 10:33 AMJonathan Mew
05/17/2019, 10:46 AMreplaceRange
, totally the right tool for the job!
kotlinlang is such a great slack channelJonathan Mew
05/17/2019, 10:53 AMJonathan Mew
05/17/2019, 10:58 AMfun Char.times(n: Int) = String(CharArray(n, {this} ))
fun String.anonymise(numberOfCharactersToHide: Int = this.length - 1): String {
val resolvedCharacterToHide = kotlin.math.min(this.length, numberOfCharactersToHide)
return replaceRange(
0,
resolvedCharacterToHide,
'*'.times(resolvedCharacterToHide)
)
}
@Test
fun `anonymise`() {
val s = "blah"
s.anonymise(3).shouldBe("***h")
s.anonymise(4).shouldBe("****")
s.anonymise(5).shouldBe("****")
}
antonis
05/17/2019, 2:11 PM