Sha256: af46ccd87fe9ffb282c9fc77b65fd6c52d8f93f9e049db4e812c4529195a6d46
Contents?: true
Size: 1.07 KB
Versions: 73
Compression:
Stored size: 1.07 KB
Contents
public class PhoneNumber { private final String number; public PhoneNumber(String phoneNumber) { number = normalize(extractDigits(phoneNumber)); } private String extractDigits(String dirtyNumber) { return dirtyNumber.replaceAll("[^\\d]", ""); } private String normalize(String number) { if(number.length() == 11 && number.startsWith("1")) { number = number.substring(1, number.length()); } final boolean numberIsValid = (number.length() == 10); final String normalizedNumber = numberIsValid ? number : "0000000000"; return normalizedNumber; } public String getNumber() { return number; } public String getAreaCode() { return number.substring(0, 3); } public String getExchangeCode() { return number.substring(3, 6); } public String getSubscriberNumber() { return number.substring(6, 10); } public String pretty() { return "(" + getAreaCode() + ") " + getExchangeCode() + "-" + getSubscriberNumber(); } }
Version data entries
73 entries across 73 versions & 1 rubygems