Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
seventhmoon committed Apr 12, 2021
1 parent 045941c commit 4c172ee
Showing 1 changed file with 1 addition and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.androidfung.validator.lib

class HkidValidator {

private val acceptedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
private val hkidRegex = Regex("[A-Z]{1,2}\\d{6}")

Expand All @@ -10,16 +9,12 @@ class HkidValidator {
return if (input.matches(hkidRegex)) {
val id = if (input.length == 7) " $input" else input
val sum = id.mapIndexed { index, c ->
(9 - index) * c.toMultiplier()
(9 - index) * acceptedChars.indexOf(c)
}.sum()

val checksum = (11 - (sum % 11) % 11)
checksum.digitToChar(16)

} else throw IllegalArgumentException()
}

private fun Char.toMultiplier(): Int {
return acceptedChars.indexOf(this, ignoreCase = true)
}
}

0 comments on commit 4c172ee

Please sign in to comment.