Sha256: 4094300189d451bc5aaf4f778f0977ccc8e168679d0112caaaf10e174f2aa54c

Contents?: true

Size: 1.15 KB

Versions: 188

Compression:

Stored size: 1.15 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 String(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

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.138 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.137 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.136 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.135 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.134 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.133 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.132 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.131 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.130 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.129 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.128 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.127 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.126 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.125 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.124 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.123 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.122 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.121 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.120 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift