Last active
January 24, 2018 03:48
-
-
Save prestongarno/5c7fd0fd3f868b51a7ee3002e8bfaf0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
fun parsePhoneNumber(text: String): PhoneNumber { | |
val destructuredRegex = "([0-9]{3})-([0-9]{3})-([0-9]{4})".toRegex() | |
return destructuredRegex.matchEntire(text) | |
?.destructured | |
?.let { (areaCode, prefix, lineNumber) -> | |
PhoneNumber(areaCode.toInt(), prefix.toInt(), lineNumber.toInt()) | |
} | |
?: throw IllegalArgumentException("Bad input '$text'") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment