Skip to content

Commit

Permalink
refactor: phone number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Jul 31, 2022
1 parent 29d2593 commit a34e4ef
Show file tree
Hide file tree
Showing 6 changed files with 775 additions and 0 deletions.
59 changes: 59 additions & 0 deletions lib/phonegeocode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Phone Geocode

**This package was copied from [github.com/davegardnerisme/phonegeocode](https://github.com/davegardnerisme/phonegeocode) then modified.**

----

Internationalised phone number geocoding by country for Go.

I built this because I needed to turn phone numbers into countries, and that's
really _all_ I needed. If [libphonenumber](https://code.google.com/p/libphonenumber/)
existed for Go, I would probably just use that. AFAIK it doesn't.

This is based on work in [github.com/relops/prefixes](https://github.com/relops/prefixes),
however it has a different implementation, using a [Trie](http://en.wikipedia.org/wiki/Trie)
data structure - specifically [github.com/tchap/go-patricia](https://github.com/tchap/go-patricia).

The way it works is that we have a list of prefixes that identify a country, and
we simply match the _most specific prefix_ to find the country code. This deals
with Canada where the country code is `+1` and shared with US.

All the data lives in a CSV and can be used via codegen to create our Trie.

```
go run data/codegen.go > ./data.go && go fmt
```

## Usage

```
// cc = GB, err = nil
cc, err := phonegeocode.Country("+447999111222")
// cc = "", err = phonegeocode.ErrCountryNotFound
cc, err := phonegeocode.Country("+999999999998")
```

## License

The MIT License (MIT)

Copyright (c) 2016 Dave Gardner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
294 changes: 294 additions & 0 deletions lib/phonegeocode/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
package phonegeocode

import (
gotrie "github.com/tchap/go-patricia/patricia"
)

func initPrefixes() *gotrie.Trie {
prefixes := gotrie.NewTrie()

prefixes.Insert(gotrie.Prefix("1403"), "CA")
prefixes.Insert(gotrie.Prefix("1587"), "CA")
prefixes.Insert(gotrie.Prefix("1780"), "CA")
prefixes.Insert(gotrie.Prefix("1825"), "CA")
prefixes.Insert(gotrie.Prefix("1236"), "CA")
prefixes.Insert(gotrie.Prefix("1250"), "CA")
prefixes.Insert(gotrie.Prefix("1604"), "CA")
prefixes.Insert(gotrie.Prefix("1672"), "CA")
prefixes.Insert(gotrie.Prefix("1778"), "CA")
prefixes.Insert(gotrie.Prefix("1204"), "CA")
prefixes.Insert(gotrie.Prefix("1431"), "CA")
prefixes.Insert(gotrie.Prefix("1506"), "CA")
prefixes.Insert(gotrie.Prefix("1709"), "CA")
prefixes.Insert(gotrie.Prefix("1782"), "CA")
prefixes.Insert(gotrie.Prefix("1902"), "CA")
prefixes.Insert(gotrie.Prefix("1548"), "CA")
prefixes.Insert(gotrie.Prefix("1249"), "CA")
prefixes.Insert(gotrie.Prefix("1289"), "CA")
prefixes.Insert(gotrie.Prefix("1343"), "CA")
prefixes.Insert(gotrie.Prefix("1365"), "CA")
prefixes.Insert(gotrie.Prefix("1387"), "CA")
prefixes.Insert(gotrie.Prefix("1416"), "CA")
prefixes.Insert(gotrie.Prefix("1437"), "CA")
prefixes.Insert(gotrie.Prefix("1519"), "CA")
prefixes.Insert(gotrie.Prefix("1613"), "CA")
prefixes.Insert(gotrie.Prefix("1647"), "CA")
prefixes.Insert(gotrie.Prefix("1705"), "CA")
prefixes.Insert(gotrie.Prefix("1742"), "CA")
prefixes.Insert(gotrie.Prefix("1807"), "CA")
prefixes.Insert(gotrie.Prefix("1905"), "CA")
prefixes.Insert(gotrie.Prefix("1782"), "CA")
prefixes.Insert(gotrie.Prefix("1902"), "CA")
prefixes.Insert(gotrie.Prefix("1418"), "CA")
prefixes.Insert(gotrie.Prefix("1438"), "CA")
prefixes.Insert(gotrie.Prefix("1450"), "CA")
prefixes.Insert(gotrie.Prefix("1514"), "CA")
prefixes.Insert(gotrie.Prefix("1579"), "CA")
prefixes.Insert(gotrie.Prefix("1581"), "CA")
prefixes.Insert(gotrie.Prefix("1819"), "CA")
prefixes.Insert(gotrie.Prefix("1873"), "CA")
prefixes.Insert(gotrie.Prefix("1306"), "CA")
prefixes.Insert(gotrie.Prefix("1639"), "CA")
prefixes.Insert(gotrie.Prefix("1867"), "CA")
prefixes.Insert(gotrie.Prefix("1"), "US")
prefixes.Insert(gotrie.Prefix("1242"), "BS")
prefixes.Insert(gotrie.Prefix("1246"), "BB")
prefixes.Insert(gotrie.Prefix("1264"), "AI")
prefixes.Insert(gotrie.Prefix("1268"), "AG")
prefixes.Insert(gotrie.Prefix("1284"), "VG")
prefixes.Insert(gotrie.Prefix("1340"), "VI")
prefixes.Insert(gotrie.Prefix("1345"), "KY")
prefixes.Insert(gotrie.Prefix("1441"), "BM")
prefixes.Insert(gotrie.Prefix("1473"), "GD")
prefixes.Insert(gotrie.Prefix("1649"), "TC")
prefixes.Insert(gotrie.Prefix("1664"), "MS")
prefixes.Insert(gotrie.Prefix("1670"), "MP")
prefixes.Insert(gotrie.Prefix("1671"), "GU")
prefixes.Insert(gotrie.Prefix("1684"), "AS")
prefixes.Insert(gotrie.Prefix("1721"), "SX")
prefixes.Insert(gotrie.Prefix("1758"), "LC")
prefixes.Insert(gotrie.Prefix("1767"), "DM")
prefixes.Insert(gotrie.Prefix("1784"), "VC")
prefixes.Insert(gotrie.Prefix("1787"), "PR")
prefixes.Insert(gotrie.Prefix("1809"), "DO")
prefixes.Insert(gotrie.Prefix("1829"), "DO")
prefixes.Insert(gotrie.Prefix("1849"), "DO")
prefixes.Insert(gotrie.Prefix("1868"), "TT")
prefixes.Insert(gotrie.Prefix("1869"), "KN")
prefixes.Insert(gotrie.Prefix("1876"), "JM")
prefixes.Insert(gotrie.Prefix("1939"), "PR")
prefixes.Insert(gotrie.Prefix("20"), "EG")
prefixes.Insert(gotrie.Prefix("211"), "SS")
prefixes.Insert(gotrie.Prefix("212"), "MA")
prefixes.Insert(gotrie.Prefix("213"), "DZ")
prefixes.Insert(gotrie.Prefix("216"), "TN")
prefixes.Insert(gotrie.Prefix("218"), "LY")
prefixes.Insert(gotrie.Prefix("220"), "GM")
prefixes.Insert(gotrie.Prefix("221"), "SN")
prefixes.Insert(gotrie.Prefix("222"), "MR")
prefixes.Insert(gotrie.Prefix("223"), "ML")
prefixes.Insert(gotrie.Prefix("224"), "GN")
prefixes.Insert(gotrie.Prefix("225"), "CI")
prefixes.Insert(gotrie.Prefix("226"), "BF")
prefixes.Insert(gotrie.Prefix("227"), "NE")
prefixes.Insert(gotrie.Prefix("228"), "TG")
prefixes.Insert(gotrie.Prefix("229"), "BJ")
prefixes.Insert(gotrie.Prefix("230"), "MU")
prefixes.Insert(gotrie.Prefix("231"), "LR")
prefixes.Insert(gotrie.Prefix("232"), "SL")
prefixes.Insert(gotrie.Prefix("233"), "GH")
prefixes.Insert(gotrie.Prefix("234"), "NG")
prefixes.Insert(gotrie.Prefix("235"), "TD")
prefixes.Insert(gotrie.Prefix("236"), "CF")
prefixes.Insert(gotrie.Prefix("237"), "CM")
prefixes.Insert(gotrie.Prefix("238"), "CV")
prefixes.Insert(gotrie.Prefix("239"), "ST")
prefixes.Insert(gotrie.Prefix("240"), "GQ")
prefixes.Insert(gotrie.Prefix("241"), "GA")
prefixes.Insert(gotrie.Prefix("242"), "CG")
prefixes.Insert(gotrie.Prefix("243"), "CD")
prefixes.Insert(gotrie.Prefix("244"), "AO")
prefixes.Insert(gotrie.Prefix("245"), "GW")
prefixes.Insert(gotrie.Prefix("246"), "IO")
prefixes.Insert(gotrie.Prefix("247"), "SH")
prefixes.Insert(gotrie.Prefix("248"), "SC")
prefixes.Insert(gotrie.Prefix("249"), "SD")
prefixes.Insert(gotrie.Prefix("250"), "RW")
prefixes.Insert(gotrie.Prefix("251"), "ET")
prefixes.Insert(gotrie.Prefix("252"), "SO")
prefixes.Insert(gotrie.Prefix("253"), "DJ")
prefixes.Insert(gotrie.Prefix("254"), "KE")
prefixes.Insert(gotrie.Prefix("255"), "TZ")
prefixes.Insert(gotrie.Prefix("256"), "UG")
prefixes.Insert(gotrie.Prefix("257"), "BI")
prefixes.Insert(gotrie.Prefix("258"), "MZ")
prefixes.Insert(gotrie.Prefix("260"), "ZM")
prefixes.Insert(gotrie.Prefix("261"), "MG")
prefixes.Insert(gotrie.Prefix("262"), "RE")
prefixes.Insert(gotrie.Prefix("263"), "ZW")
prefixes.Insert(gotrie.Prefix("264"), "NA")
prefixes.Insert(gotrie.Prefix("265"), "MW")
prefixes.Insert(gotrie.Prefix("266"), "LS")
prefixes.Insert(gotrie.Prefix("267"), "BW")
prefixes.Insert(gotrie.Prefix("268"), "SZ")
prefixes.Insert(gotrie.Prefix("269"), "KM")
prefixes.Insert(gotrie.Prefix("27"), "ZA")
prefixes.Insert(gotrie.Prefix("290"), "SH")
prefixes.Insert(gotrie.Prefix("291"), "ER")
prefixes.Insert(gotrie.Prefix("297"), "AW")
prefixes.Insert(gotrie.Prefix("298"), "FO")
prefixes.Insert(gotrie.Prefix("299"), "GL")
prefixes.Insert(gotrie.Prefix("30"), "GR")
prefixes.Insert(gotrie.Prefix("31"), "NL")
prefixes.Insert(gotrie.Prefix("32"), "BE")
prefixes.Insert(gotrie.Prefix("33"), "FR")
prefixes.Insert(gotrie.Prefix("34"), "ES")
prefixes.Insert(gotrie.Prefix("350"), "GI")
prefixes.Insert(gotrie.Prefix("351"), "PT")
prefixes.Insert(gotrie.Prefix("352"), "LU")
prefixes.Insert(gotrie.Prefix("353"), "IE")
prefixes.Insert(gotrie.Prefix("354"), "IS")
prefixes.Insert(gotrie.Prefix("355"), "AL")
prefixes.Insert(gotrie.Prefix("356"), "MT")
prefixes.Insert(gotrie.Prefix("357"), "CY")
prefixes.Insert(gotrie.Prefix("358"), "FI")
prefixes.Insert(gotrie.Prefix("359"), "BG")
prefixes.Insert(gotrie.Prefix("36"), "HU")
prefixes.Insert(gotrie.Prefix("370"), "LT")
prefixes.Insert(gotrie.Prefix("371"), "LV")
prefixes.Insert(gotrie.Prefix("372"), "EE")
prefixes.Insert(gotrie.Prefix("373"), "MD")
prefixes.Insert(gotrie.Prefix("374"), "AM")
prefixes.Insert(gotrie.Prefix("375"), "BY")
prefixes.Insert(gotrie.Prefix("376"), "AD")
prefixes.Insert(gotrie.Prefix("377"), "MC")
prefixes.Insert(gotrie.Prefix("378"), "SM")
prefixes.Insert(gotrie.Prefix("379"), "VA")
prefixes.Insert(gotrie.Prefix("380"), "UA")
prefixes.Insert(gotrie.Prefix("381"), "RS")
prefixes.Insert(gotrie.Prefix("381"), "XK")
prefixes.Insert(gotrie.Prefix("382"), "ME")
prefixes.Insert(gotrie.Prefix("385"), "HR")
prefixes.Insert(gotrie.Prefix("386"), "SI")
prefixes.Insert(gotrie.Prefix("386"), "XK")
prefixes.Insert(gotrie.Prefix("387"), "BA")
prefixes.Insert(gotrie.Prefix("389"), "MK")
prefixes.Insert(gotrie.Prefix("39"), "IT")
prefixes.Insert(gotrie.Prefix("39066"), "VA")
prefixes.Insert(gotrie.Prefix("40"), "RO")
prefixes.Insert(gotrie.Prefix("41"), "CH")
prefixes.Insert(gotrie.Prefix("420"), "CZ")
prefixes.Insert(gotrie.Prefix("421"), "SK")
prefixes.Insert(gotrie.Prefix("423"), "LI")
prefixes.Insert(gotrie.Prefix("43"), "AT")
prefixes.Insert(gotrie.Prefix("44"), "GB")
prefixes.Insert(gotrie.Prefix("45"), "DK")
prefixes.Insert(gotrie.Prefix("46"), "SE")
prefixes.Insert(gotrie.Prefix("47"), "NO")
prefixes.Insert(gotrie.Prefix("4779"), "SJ")
prefixes.Insert(gotrie.Prefix("48"), "PL")
prefixes.Insert(gotrie.Prefix("49"), "DE")
prefixes.Insert(gotrie.Prefix("500"), "FK")
prefixes.Insert(gotrie.Prefix("501"), "BZ")
prefixes.Insert(gotrie.Prefix("502"), "GT")
prefixes.Insert(gotrie.Prefix("503"), "SV")
prefixes.Insert(gotrie.Prefix("504"), "HN")
prefixes.Insert(gotrie.Prefix("505"), "NI")
prefixes.Insert(gotrie.Prefix("506"), "CR")
prefixes.Insert(gotrie.Prefix("507"), "PA")
prefixes.Insert(gotrie.Prefix("508"), "PM")
prefixes.Insert(gotrie.Prefix("509"), "HT")
prefixes.Insert(gotrie.Prefix("51"), "PE")
prefixes.Insert(gotrie.Prefix("52"), "MX")
prefixes.Insert(gotrie.Prefix("53"), "CU")
prefixes.Insert(gotrie.Prefix("54"), "AR")
prefixes.Insert(gotrie.Prefix("55"), "BR")
prefixes.Insert(gotrie.Prefix("56"), "CL")
prefixes.Insert(gotrie.Prefix("57"), "CO")
prefixes.Insert(gotrie.Prefix("58"), "VE")
prefixes.Insert(gotrie.Prefix("590"), "GP")
prefixes.Insert(gotrie.Prefix("591"), "BO")
prefixes.Insert(gotrie.Prefix("592"), "GY")
prefixes.Insert(gotrie.Prefix("593"), "EC")
prefixes.Insert(gotrie.Prefix("594"), "GF")
prefixes.Insert(gotrie.Prefix("595"), "PY")
prefixes.Insert(gotrie.Prefix("596"), "MQ")
prefixes.Insert(gotrie.Prefix("597"), "SR")
prefixes.Insert(gotrie.Prefix("598"), "UY")
prefixes.Insert(gotrie.Prefix("5997"), "BQ")
prefixes.Insert(gotrie.Prefix("5999"), "CW")
prefixes.Insert(gotrie.Prefix("60"), "MY")
prefixes.Insert(gotrie.Prefix("61"), "AU")
prefixes.Insert(gotrie.Prefix("62"), "ID")
prefixes.Insert(gotrie.Prefix("63"), "PH")
prefixes.Insert(gotrie.Prefix("64"), "NZ")
prefixes.Insert(gotrie.Prefix("65"), "SG")
prefixes.Insert(gotrie.Prefix("66"), "TH")
prefixes.Insert(gotrie.Prefix("670"), "TL")
prefixes.Insert(gotrie.Prefix("672"), "NF")
prefixes.Insert(gotrie.Prefix("673"), "BN")
prefixes.Insert(gotrie.Prefix("674"), "NR")
prefixes.Insert(gotrie.Prefix("675"), "PG")
prefixes.Insert(gotrie.Prefix("676"), "TO")
prefixes.Insert(gotrie.Prefix("677"), "SB")
prefixes.Insert(gotrie.Prefix("678"), "VU")
prefixes.Insert(gotrie.Prefix("679"), "FJ")
prefixes.Insert(gotrie.Prefix("680"), "PW")
prefixes.Insert(gotrie.Prefix("681"), "WF")
prefixes.Insert(gotrie.Prefix("682"), "CK")
prefixes.Insert(gotrie.Prefix("683"), "NU")
prefixes.Insert(gotrie.Prefix("685"), "WS")
prefixes.Insert(gotrie.Prefix("686"), "KI")
prefixes.Insert(gotrie.Prefix("687"), "NC")
prefixes.Insert(gotrie.Prefix("688"), "TV")
prefixes.Insert(gotrie.Prefix("689"), "PF")
prefixes.Insert(gotrie.Prefix("690"), "TK")
prefixes.Insert(gotrie.Prefix("691"), "FM")
prefixes.Insert(gotrie.Prefix("692"), "MH")
prefixes.Insert(gotrie.Prefix("7"), "RU")
prefixes.Insert(gotrie.Prefix("76"), "KZ")
prefixes.Insert(gotrie.Prefix("77"), "KZ")
prefixes.Insert(gotrie.Prefix("81"), "JP")
prefixes.Insert(gotrie.Prefix("82"), "KR")
prefixes.Insert(gotrie.Prefix("84"), "VN")
prefixes.Insert(gotrie.Prefix("850"), "KP")
prefixes.Insert(gotrie.Prefix("852"), "HK")
prefixes.Insert(gotrie.Prefix("853"), "MO")
prefixes.Insert(gotrie.Prefix("855"), "KH")
prefixes.Insert(gotrie.Prefix("856"), "LA")
prefixes.Insert(gotrie.Prefix("86"), "CN")
prefixes.Insert(gotrie.Prefix("880"), "BD")
prefixes.Insert(gotrie.Prefix("886"), "TW")
prefixes.Insert(gotrie.Prefix("90"), "TR")
prefixes.Insert(gotrie.Prefix("91"), "IN")
prefixes.Insert(gotrie.Prefix("92"), "PK")
prefixes.Insert(gotrie.Prefix("93"), "AF")
prefixes.Insert(gotrie.Prefix("94"), "LK")
prefixes.Insert(gotrie.Prefix("95"), "MM")
prefixes.Insert(gotrie.Prefix("960"), "MV")
prefixes.Insert(gotrie.Prefix("961"), "LB")
prefixes.Insert(gotrie.Prefix("962"), "JO")
prefixes.Insert(gotrie.Prefix("963"), "SY")
prefixes.Insert(gotrie.Prefix("964"), "IQ")
prefixes.Insert(gotrie.Prefix("965"), "KW")
prefixes.Insert(gotrie.Prefix("966"), "SA")
prefixes.Insert(gotrie.Prefix("967"), "YE")
prefixes.Insert(gotrie.Prefix("968"), "OM")
prefixes.Insert(gotrie.Prefix("970"), "PS")
prefixes.Insert(gotrie.Prefix("971"), "AE")
prefixes.Insert(gotrie.Prefix("972"), "IL")
prefixes.Insert(gotrie.Prefix("973"), "BH")
prefixes.Insert(gotrie.Prefix("974"), "QA")
prefixes.Insert(gotrie.Prefix("975"), "BT")
prefixes.Insert(gotrie.Prefix("976"), "MN")
prefixes.Insert(gotrie.Prefix("977"), "NP")
prefixes.Insert(gotrie.Prefix("98"), "IR")
prefixes.Insert(gotrie.Prefix("992"), "TJ")
prefixes.Insert(gotrie.Prefix("993"), "TM")
prefixes.Insert(gotrie.Prefix("994"), "AZ")
prefixes.Insert(gotrie.Prefix("995"), "GE")
prefixes.Insert(gotrie.Prefix("996"), "KG")
prefixes.Insert(gotrie.Prefix("998"), "UZ")

return prefixes
}
41 changes: 41 additions & 0 deletions lib/phonegeocode/data/codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"bufio"
"embed"
"encoding/csv"
"fmt"
"io"
"log"
)

//go:embed prefixes.csv
var prefixes embed.FS

func main() {
f, err := prefixes.Open("prefixes.csv")
if err != nil {
log.Fatal(err)
}
in := bufio.NewReader(f)
reader := csv.NewReader(in)

fmt.Printf("package phonegeocode\n\n")
fmt.Printf("import (\n\tgotrie \"github.com/tchap/go-patricia/patricia\"\n)\n\n")

fmt.Printf("func initPrefixes() *gotrie.Trie {\n")
fmt.Printf("\tprefixes := gotrie.NewTrie()\n\n")

for {
row, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}

fmt.Printf(" prefixes.Insert(gotrie.Prefix(\"%s\"), \"%s\")\n", row[0], row[1])
}
fmt.Printf("\n\treturn prefixes\n}\n")
}
Loading

0 comments on commit a34e4ef

Please sign in to comment.