Sha256: 1037ac52b5064b9d30674dce4fc861dea0629475f1c8b47fe6d001dec7994e1c

Contents?: true

Size: 1.14 KB

Versions: 137

Compression:

Stored size: 1.14 KB

Contents

import Foundation

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

137 entries across 137 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.52 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.51 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.50 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.49 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.48 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.47 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.46 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.45 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.44 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.43 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.42 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.41 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.40 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.39 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.38 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.37 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.36 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.34 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.1.0.33 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift