src/main/scala/phone_number.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
object PhoneNumber {
  val Invalid = List("000", "000", "0000")
  val ValidNumber = """1?(\d{3})(\d{3})(\d{4})""".r
}

class PhoneNumber(rawNumber: String) {
  private val parts = rawNumber.filter(_ isDigit) match {
    case PhoneNumber.ValidNumber(parts @ _*) => parts
    case _ => PhoneNumber.Invalid
  }

  val number = parts.mkString("")
  val areaCode = parts(0)

  override def toString = "(%s) %s-%s".format(parts:_*)
}

@abo64 thinks this looks great

Comments

Storing the separate parts of the phone number to avoid too much string manipulation.

helenst commented 31 March 2016 at 08:57 UTC

You're not logged in right now. Please login via GitHub to comment