Phone number parsing, validation and formatting for MoonBit: E.164 and RFC 3966, international and national dialing, calling-code and region lookup across 51 numbering plans
moon add sssssurf/dialnumber///|
let pn = @dialnumber.parse("011 44 20 7946 0958", "US")
///|
match pn {
Some(n) => {
@dialnumber.format_e164(n) // "+442079460958"
@dialnumber.format_national(n, "GB") // Some("02079460958")
@dialnumber.is_possible(n) // true
@dialnumber.region_of_number(n) // Some("GB")
}
None => ()
}moon run cmd/main -- parse "+81 90-1234-5678"
moon run cmd/main -- possible "090-1234-5678" JP
moon run cmd/main -- format "090-1234-5678" JP
moon run cmd/main -- types "+86 131 2345 6789"
moon run cmd/main -- region "+442079460958"
moon run cmd/main -- info JP
moon run cmd/main -- cc de
moon run cmd/main -- tel "tel:+1-201-555-0123;ext=42"
moon run cmd/main -- list$ moon run cmd/main -- parse "+81 90-1234-5678"
country_code: 81
national_number: 9012345678
e164: +819012345678
region: JP$ moon run cmd/main -- info JP
region: JP
name: Japan
calling_code: 81
idd: 010
national_prefix: 0
lengths: 8,9,10,11,12,13,14,15,16,17$ moon run cmd/main -- types "+86 131 2345 6789"
types: fixed-line,mobile,shared-cost| Function | Purpose |
|---|---|
| parse(input, region) | read any written form into a PhoneNumber? |
| parse_e164(input) | read only a +-prefixed number |
| parse_national(input, region) | read a number as dialled in a region |
| is_possible(pn) | calling code known and length used by the plan |
| is_possible_in_region(pn, region) | the same, against one named region |
| is_possible_number(input, region) | parse then check, in one call |
| possible_types(pn) | number types the length admits |
| possible_types_in_region(pn, region) | the same, within one region |
| region_of_number(pn) | the region a number resolves to |
| format_e164(pn) | +8613123456789 |
| format_international(pn) | +86 13123456789 |
| format_national(pn, region) | trunk prefix plus national number |
| format_rfc3966(pn) | tel:+8613123456789;ext=42 |
| parse_tel_uri(uri) | read a tel: URI |
| to_tel_uri(pn) | write a tel: URI |
| region_codes() | every region code in the table |
| region_exists(code) | is the code in the table |
| canonical_region_code(code) | the table's spelling of a code |
| region_name(code) | English name |
| calling_code(code) | E.164 calling code |
| idd_prefix(code) | international dial-out prefix |
| national_prefix(code) | trunk prefix, or None |
| possible_lengths(code) | national-number lengths |
| region_by_calling_code(cc) | one region for a calling code |
| regions_by_calling_code(cc) | every region for a calling code |
moon testimpl Show for NumberTypepub(all) struct PhoneNumber {
country_code : Int
national_number : String
extension : String
} derive(Eq)impl Show for PhoneNumberfn calling_code(code : String) -> Int?fn canonical_region_code(code : String) -> String?fn idd_prefix(code : String) -> String?fn is_possible_number(input : String, region : String) -> Boolfn national_prefix(code : String) -> String?fn region_by_calling_code(cc : Int) -> String?fn region_exists(code : String) -> Boolfn region_name(code : String) -> String?Install
Download zipPhone number parsing, validation and formatting for MoonBit: E.164 and RFC 3966, international and national dialing, calling-code and region lookup across 51 numbering plans