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.180 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.179 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.178 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.177 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.176 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.175 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.174 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.173 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.172 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.171 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.170 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.169 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.167 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.166 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.165 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.164 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.163 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.162 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.161 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift
trackler-2.2.1.160 tracks/swift/exercises/phone-number/Sources/PhoneNumberExample.swift