GithubHelp home page GithubHelp logo

amrfaissal / phone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yunshang/phone

1.0 1.0 0.0 47 KB

Go library for phone number parsing, validation, and formatting

Go 100.00%
go phone phone-number golang-library

phone's Introduction

phone

Go library for phone number parsing, validation, and formatting.

Install

go get -u github.com/amrfaissal/phone

Automatic country and area code detection

Phone does its best to automatically detect the country and area code while parsing.

To do this, phone uses data stored in YAML format under country.go.

Each country code can have a regular expression named area_code that describes what the area code for that particular country looks like.

If an area_code regular expression isn't specified, a default value which is considered correct for the US will be used.

If your country has phone numbers longer that 8 digits - excluding country and area code - you can specify that within the country's configuration in country.go file.

Validation

Validating is very relaxed, basically it strips out everything that's not a number or '+' character:

phone.IsValid("random 091/512-5486 random")

Formatting

Formating is done via the #format method. The method accepts a Symbol or a String.

When given a string, it interpolates the string with the following fields:

  • %c - country_code (385)
  • %a - area_code (91)
  • %A - area_code with leading zero (091)
  • %n - number (5125486)
  • %f - first @@n1_length characters of number (configured through country n1_length), default is 3 (512)
  • %l - last characters of number (5486)
  • %x - the extension number
parsedPhone = phone.Parse("+385915125486")
parsedPhone.String() // => "+385915125486"
parsedPhone.Format("%A/%f-%l") // => "091/512-5486"
parsedPhone.Format("+ %c (%a) %n") // => "+ 385 (91) 5125486"

When given a symbol it is used as a lookup for the format in the namedFormats map.

parsedPhone.Format("europe") // => "+385 (0) 91 512 5486"
parsedPhone.Format("us") // => "(234) 123-4567"
parsedPhone.Format("default_with_extension") // => "+3851234567x143"

Finding countries by their ISO code

If you don't have the country code, but you know from other sources what country a phone is from, you can retrieve the country using the country ISO code (such as 'de', 'es', 'us', ...).

Phone Initialization

To initialize a new phone struct with the number, area code, country code and extension number:

args := []string{"5125486", "91", "385"}
phone.New(args)
args := []string{"5125486", "91", "385", "143"}
phone.New(args)

Parsing

Create a new phone object by parsing from a string. The library does it's best to detect the country and area codes:

phone.Parse("+385915125486")
phone.Parse("00385915125486")

If the country or area code isn't given in the string, you must set it, otherwise it doesn't work:

args := []string{"091/512-5486", "385"}
phone.New(args)

If you feel that it's tedious, set the default country code once:

phone.SetDefaultCountryCode("385")
args := []string{"091/512-5486"}
phone.New(args)

Same goes for the area code:

args := []string{"5125486", "91"}
phone.New(args)

OR

phone.SetDefaultCountryCode("385")
phone.SetDefaultAreaCode("47")
args := []string{"451-588"}
phone.New(args)

Adding and maintaining countries

From time to time, the specifics about your countries information may change. You can add or update your countries configuration by editing country.go file.

These are the supported attributes for countries configuration:

  • country_code: Required. A string representing your country's international dialling code. e.g. "123"
  • national_dialing_prefix: Required. A string representing your default dialling prefix for national calls. e.g. "0"
  • char_3_code: Required. A string representing a country's ISO code. e.g. "US"
  • name: Required. The name of the country. e.g. "Denmark"
  • international_dialing_prefix: Required. The dialling prefix a country typically uses when making international calls. e.g. "0"
  • area_code: Optional. A regular expression detailing valid area codes. Default: "\d{3}" i.e. any 3 digits.
  • max_num_length: Optional. The maximum length of a phone number after country and area codes have been removed. Default: 8

License

MIT License

phone's People

Contributors

yunshang avatar amrfaissal avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.