phonekeypad

phonekeypad converts alphabetic strings and phone number literals to their numeric equivalents on a standard 12-key telephony keypad (ITU E.161 / ITU T.9).

Part of nurago, a collection of independent Go packages for backend services.

import "github.com/tecnickcom/nurago/pkg/phonekeypad"

Package phonekeypad converts alphabetic strings and phone number literals to their numeric equivalents on a standard 12-key telephony keypad (ITU E.161 / ITU T.9).

Keypad Layout

The standard keypad mapping (ITU E.161) used by this package:

+-----+-----+-----+
|  1  |  2  |  3  |
|     | ABC | DEF |
+-----+-----+-----+
|  4  |  5  |  6  |
| GHI | JKL | MNO |
+-----+-----+-----+
|  7  |  8  |  9  |
| PQRS| TUV | WXYZ|
+-----+-----+-----+
|     |  0  |     |
|     |     |     |
+-----+-----+-----+

Quick Start

Translate a vanity phone number to its dialable form:

numStr := phonekeypad.KeypadNumberString("1-800-FLOWERS")
// numStr == "18003569377"

Or get the digit sequence as a slice for programmatic use:

digits := phonekeypad.KeypadNumber("1-800-FLOWERS")
// digits == []int{1, 8, 0, 0, 3, 5, 6, 9, 3, 7, 7}

When To Use

  • Vanity numbers such as 1-800-FLOWERS must be converted to digits.
  • You index numbers for search in their digit form.

Example

phoneNumber := "999-EXAMPLE-1"
numSeq := make([]int, 0, len(phoneNumber))

for _, r := range phoneNumber {
	v, status := phonekeypad.KeypadDigit(r)
	if status {
		numSeq = append(numSeq, v)
	}
}

fmt.Println(numSeq)

// Output:
// [9 9 9 3 9 2 6 7 5 3 1]

Full source is in example_phonekeypad_test.go. More runnable examples are on pkg.go.dev.

Dependencies

This package reaches no external module: it uses only the Go standard library.