Sha256: a95bebceaf2f09431af42e1066c7cb847bfb38ce65f8c2d01919eedcbd9f14bb

Contents?: true

Size: 1.13 KB

Versions: 71

Compression:

Stored size: 1.13 KB

Contents

private extension String {
    subscript (range: CountableClosedRange<Int>) -> String {
        get {
            let start = characters.index(startIndex, offsetBy: range.lowerBound)
            let end = characters.index(start, offsetBy: range.upperBound - range.lowerBound)

            return self[start...end]
        }
    }

    var onlyDigits: String {
        return String(characters.filter { $0.isDigit })
    }
}

private extension Character {
    var isDigit: Bool {
        return "0123456789".characters.contains(self)
    }
}

struct PhoneNumber: CustomStringConvertible {
    let number: String

    init(_ startingNumber: String) {
        let digits = startingNumber.onlyDigits

        switch digits.characters.count {
        case 10:
            number = digits
        case 11 where digits.hasPrefix("1"):
            number = digits[1...10]
        default:
            number = "0000000000"
        }
    }

    var areaCode: String {
        return number[0...2]
    }

    var description: String {
        let prefix = number[3...5]
        let final  = number[6...9]
        return "(\(areaCode)) \(prefix)-\(final)"
    }
}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.0.6.15 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.14 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.13 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.12 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.11 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.10 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.9 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.8 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.7 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.6 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.5 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.4 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.3 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.2 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.1 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.6.0 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.5.18 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.5.17 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.5.16 tracks/swift/exercises/phone-number/PhoneNumberExample.swift
trackler-2.0.5.15 tracks/swift/exercises/phone-number/PhoneNumberExample.swift