Sha256: c9df4ddde14fe9b48c4787363bbf3c28eeebaec31b10ab60365604b549cf1358

Contents?: true

Size: 1.8 KB

Versions: 163

Compression:

Stored size: 1.8 KB

Contents

package phonenumber

import (
	"fmt"
	"strings"
	"unicode"
)

const testVersion = 1

// Number takes in a potential phone number string and returns the number
// without any formatting if it's valid.
//
// * If the phone number is less than 10 digits assume that it is bad number
// * If the phone number is 10 digits assume that it is good
// * If the phone number is 11 digits and the first number is 1, trim the 1 and use the first 10 digits
// * If the phone number is 11 digits and the first number is not 1, then it is a bad number
// * If the phone number is more than 11 digits assume that it is a bad number
func Number(s string) (string, error) {
	//remove any characters that aren't digits
	clean := strings.Map(
		func(r rune) rune {
			if unicode.IsDigit(r) {
				return r
			}
			return -1
		}, s)

	numDigits := len(clean)
	switch {
	// bad number if less than 10 digits
	case numDigits < 10:
		return "", fmt.Errorf("Phone number %q contains too few digits", s)
	// good number if exactly 10 digits
	case numDigits == 10:
		return clean, nil
	// good number if 11 digits and 1st digit is a 1.  Return last 10 digits.
	case numDigits == 11 && clean[0] == '1':
		return clean[1:], nil
	}
	// all other numbers are a bad
	return "", fmt.Errorf("Phone number %q contains too many digits", s)
}

// AreaCode takes in a phone number string and returns the area code (first three digits)
func AreaCode(s string) (string, error) {
	num, err := Number(s)
	if err != nil {
		return "", err
	}
	return num[:3], nil
}

// Format takes in a phone number string and returns it as a well formatted
// human readable string
func Format(s string) (string, error) {
	f, err := Number(s)
	if err != nil {
		return "", err
	}
	// should follow the sample format of: (123) 456-7890
	return fmt.Sprintf("(%s) %s-%s", f[:3], f[3:6], f[6:]), nil
}

Version data entries

163 entries across 163 versions & 1 rubygems

Version Path
trackler-2.1.0.8 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.7 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.6 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.5 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.4 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.3 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.2 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.1 tracks/go/exercises/phone-number/example.go
trackler-2.1.0.0 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.55 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.54 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.53 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.52 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.51 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.50 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.49 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.48 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.47 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.46 tracks/go/exercises/phone-number/example.go
trackler-2.0.8.45 tracks/go/exercises/phone-number/example.go