areacode

package
v1.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2025 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t Territory, ts ...Territory) bool

Contains returns true if the territory is in the list of territories.

func Notes added in v1.6.1

func Notes() map[NAN]string

Notes returns a map of North American Numbering Plan area codes notes that provide additional information, such as when an area code was split or created and the regions it serves.

Types

type Abbreviation

type Abbreviation string

Abbreviation represents a two-letter abbreviation for a territory in the North American Numbering Plan.

func Abbreviations

func Abbreviations() []Abbreviation

Abbreviations returns a list of all two-letter abbreviations for territories in the North American Numbering Plan sorted in ascending order.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	codes := areacode.Abbreviations()
	fmt.Println(codes[0])
}
Output:

AB

func (Abbreviation) HTML

func (a Abbreviation) HTML() template.HTML
Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	fmt.Println(areacode.Abbreviation("AB").HTML())
}
Output:

<span>AB (Alberta) - 403</span><br>

type NAN

type NAN int

NAN represents a North American Numbering Plan area code.

func AreaCodes

func AreaCodes() []NAN

AreaCodes returns a list of all NANP area codes sorted in ascending order.

func (NAN) HTML

func (c NAN) HTML() template.HTML

HTML returns the NANP code as an HTML span element.

Example:

212 - New York (NY)
Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	fmt.Println(areacode.NAN(403).HTML())
}
Output:

<span>403 - Alberta (AB) + Yukon (YT)</span><br>

func (NAN) Valid

func (c NAN) Valid() bool

Valid returns true if the NANP code is a valid area code.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	fmt.Println(areacode.NAN(212).Valid())
	fmt.Println(areacode.NAN(999999).Valid())
}
Output:

true
false

type Result

type Result struct {
	AreaCode NAN
	Terr     []Territory
}

Result represents the result of a query, which can be an area code or a list of territories.

func Queries

func Queries(s ...string) []Result

Queries returns a list of results for multiple queries from a form input.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	q := areacode.Queries("az", "arizona", "ut", "602")
	for _, result := range q {
		s := result.AreaCode.HTML()
		if s != "" {
			fmt.Println(s)
		}
		for _, t := range result.Terr {
			fmt.Println(t.HTML())
		}
	}
}
Output:

<span>Arizona (AZ)  - 602</span><br>
<span>Arizona (AZ)  - 602</span><br>
<span>Utah (UT)  - 801</span><br>
<span>602 - Arizona (AZ)</span><br>

func Query

func Query(a any) Result

Query returns the result of a query from a form input.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	fmt.Println(areacode.Query("az").Terr)
	fmt.Println(areacode.Query("arizona").Terr[0].HTML())
	fmt.Println(areacode.Query("ut").Terr[0].HTML())
	fmt.Println(areacode.Query("602").AreaCode.HTML())
}
Output:

[{Arizona AZ [602]}]
<span>Arizona (AZ)  - 602</span><br>
<span>Utah (UT)  - 801</span><br>
<span>602 - Arizona (AZ)</span><br>

type Territory

type Territory struct {
	Name         string       // Name of the state, province, or territory.
	Abbreviation Abbreviation // Two-letter abbreviation.
	AreaCodes    []NAN        // Three-digit NAN code used for telephone area codes.
}

Territory represents a territory in the North American Numbering Plan.

func Lookup

func Lookup(a any) []Territory

Lookup returns a list of territories that match the given input. The input can be a string, integer, or NAN. If the input is a string, it will match against territory names and abbreviations. If the input is an integer, it will match against NANP codes.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.Lookup("texas")
	fmt.Println(t[0])

	t = areacode.Lookup("tx")
	fmt.Println(t[0])

	t = areacode.Lookup(214)
	fmt.Println(t[0])
}
Output:

{Texas TX [210 214 409 512 713 806 817 903 915]}
{Texas TX [210 214 409 512 713 806 817 903 915]}
{Texas TX [210 214 409 512 713 806 817 903 915]}

func Lookups

func Lookups(a ...any) []Territory

Lookup returns a list of territories that match the given inputs.

See Lookup for more information.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.Lookups(817, "iowa", 202)
	for _, v := range t {
		fmt.Println(v)
	}
}
Output:

{Texas TX [210 214 409 512 713 806 817 903 915]}
{Iowa IA [319 515 712]}
{District of Columbia DC [202]}

func Territories

func Territories() []Territory

Territories returns a list of all territories in the North American Numbering Plan sorted by name in ascending order.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.Territories()
	name := t[0].Name
	alpha := t[0].Abbreviation
	area := t[0].AreaCodes
	fmt.Printf("%s %s %d\n", name, alpha, area)
	fmt.Println(len(t), "territories")
}
Output:

Alabama AL [205]
66 territories

func TerritoryByAbbr

func TerritoryByAbbr(abbr Abbreviation) Territory

TerritoryByAbbr returns the territory with the given two-letter abbreviation.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.TerritoryByAbbr("CT")
	fmt.Println(t.Name, t.Abbreviation, t.AreaCodes)
}
Output:

Connecticut CT [203]

func TerritoryByCode

func TerritoryByCode(code NAN) []Territory

TerritoryByCode returns the territories for the given North American Numbering code.

Generally, this will return a single territory, but it is possible for a NAN code to be used in multiple territories, such as provinces in Canada.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.TerritoryByCode(212)
	fmt.Println(t[0].Name, t[0].Abbreviation, t[0].AreaCodes)

	t = areacode.TerritoryByCode(902)
	for _, v := range t {
		fmt.Println(v.Name, v.Abbreviation, v.AreaCodes)
	}
}
Output:

New York NY [212 315 516 518 607 716 718 914 917]
Nova Scotia NS [902]
Prince Edward Island PE [902]

func TerritoryByName

func TerritoryByName(name string) Territory

TerritoryByName returns the territory with the given name. The name can be a US state, Canadian province, or other territory.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.TerritoryByName("ontario")
	fmt.Println(t.AreaCodes)
}
Output:

[416 519 613 705 807 905]

func TerritoryContains

func TerritoryContains(s string) []Territory

TerritoryContains returns a list of territories with names that contain the given string.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.TerritoryContains("south")
	for _, v := range t {
		fmt.Println(v)
	}
}
Output:

{South Carolina SC [803]}
{South Dakota SD [605]}

func (Territory) HTML

func (t Territory) HTML() template.HTML
Example
package main

import (
	"fmt"

	"github.com/Defacto2/server/handler/areacode"
)

func main() {
	t := areacode.TerritoryByCode(710)
	fmt.Println(t[0].HTML())
}
Output:

<span>United States Government - 710</span><br>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL