Skip to content

VismaiNair/gophonewords

gophonewords

Find numeric phone numbers from words and find vanity numbers from numeric phone numbers! This is a Go library.

gophonewords is a Go library designed to convert between vanity numbers (phone numbers containing letters) and standard numeric phone numbers.

A similar library is also available on pub.dev, for usage in Dart: phonewords package

Understanding gophonewords

The gophonewords library provides a straightforward way to transform letter-based phone numbers (like "1-800-FLOWERS") into their numeric equivalents ("1-800-356-9377").

Current Limitations (as of version 0.1.0)

Please note that gophonewords is still a very young library and has some limitations:

  • It cannot currently break numeric phone numbers into common chunks (e.g., separating "5551234567" into "555-123-4567").
  • It does not convert numeric phone numbers into string-formatted phone numbers (e.g., adding hyphens or parentheses).

Quickstart Guide

How to Add gophonewords to Your Project

  1. Create your Go project. Create a new directory for your project and a Go file where you'll use gophonewords.

  2. Initialize your Go module. Open your terminal in your project's root directory and run go mod init <your_module_name> (e.g., go mod init myapp). This will create a go.mod file.

  3. Import gophonewords. In your Go file, add the import statement:

    import "gophonewords"

    Or, if you have other imports:

    import (
        "fmt" // another package here
        "gophonewords"
    )
  4. Fetch dependencies. Run go mod tidy in your terminal. This command will add gophonewords to your go.mod file and download the package.


Usage Example

The core functionality of the gophonewords package is provided by the NumberFromString() function.

package main

import (
	"fmt"
	"gophonewords" // Assuming gophonewords is in your Go module path
)

func main() {
	// Convert a vanity number to a numeric phone number
	vanityPhone := "1-800-CALL-NOW"
	numericPhone, err := gophonewords.NumberFromString(vanityPhone)
	if err != nil {
		fmt.Printf("Error converting '%s': %v\n", vanityPhone, err)
		return
	}
	fmt.Printf("'%s' converts to '%s'\n", vanityPhone, numericPhone) // Output: "1-800-CALL-NOW" converts to "1-800-225-5669"

	// Example with mixed digits and letters
	mixedPhone := "PH0N3W0RD5"
	convertedMixed, err := gophonewords.NumberFromString(mixedPhone)
	if err != nil {
		fmt.Printf("Error converting '%s': %v\n", mixedPhone, err)
		return
	}
	fmt.Printf("'%s' converts to '%s'\n", mixedPhone, convertedMixed) // Output: "PH0N3W0RD5" converts to "7406390735"

	// Example with only digits (will return the same digits)
	numericOnly := "1234567890"
	convertedNumeric, err := gophonewords.NumberFromString(numericOnly)
	if err != nil {
		fmt.Printf("Error converting '%s': %v\n", numericOnly, err)
		return
	}
	fmt.Printf("'%s' converts to '%s'\n", numericOnly, convertedNumeric) // Output: "1234567890" converts to "1234567890"

	// Example with an empty string
	emptyString := ""
	_, err = gophonewords.NumberFromString(emptyString)
	if err != nil {
		fmt.Printf("Error converting '%s': %v\n", emptyString, err) // Output: Error converting '': phone number cannot be empty
	}
}

License

gophonewords is released under the BSD 3-Clause Revised License. See the LICENSE file for more details.

Contributing

gophonewords openly welcomes contribution. See the file CONTRIBUTING.md for more information.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages