Phone numbers
Phone numbers checked against Google's libphonenumber data, stored in E.164, shown in international or national form.
Version 1.0.0 · 2 functions · 1 crate
Functions
What use phone.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use phone_parse, phone_e164 from phone (Quillo 0.6).
phone_parse(number: str, region: str) → anyor an error{ e164, international, national, country_code, region, valid } for a number, read as from region ("US", "GB", "PH") when it has no +.
phone_e164(number: str, region: str) → stror an errorThe number in E.164 (+14155550123), to store and to send texts to, or an error when it is not a valid number.
Get it
$ quillo install phone # phone.qwer into this folder$ quillo install phone --example # and its example, phone.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/phone.qwer$ curl -O https://quillolang.org/plugins/files/phone.qweThen, in your program, all of it or only what you call:
use phone.qweruse phone_parse, phone_e164 from phone // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
phonenumber = "0.3"Example
// A sign-up form's phone field, stored one way whatever was typed.//// quillo run phone.qwe then open http://localhost:8080/phone?n=(415)+555-2671®ion=USuse "phone.qwer"route GET /phone ? { n: str, region: str = "US" } = phone_parse(n, region)?route GET /store = { ph: phone_e164("0917 123 4567", "PH")?, uk: phone_e164("+44 20 7946 0958", "")?, bad: is_err(phone_e164("123", "US")) }$ quillo run phone.qwe$ curl 'localhost:8080/phone?n=(415)+555-2671®ion=US'$ curl 'localhost:8080/store'Source
//! Phone numbers checked and written one way: E.164 for storing, international and national forms for showing, from Google's libphonenumber data.//! version: 1.0.0//!//! [dependencies]//! phonenumber = "0.3"use phonenumber::{country, Mode};use serde_json::{json, Value};fn parse(number: &str, region: &str) -> Result<phonenumber::PhoneNumber, String> { let id = if region.is_empty() { None } else { Some(region.to_uppercase().parse::<country::Id>().map_err(|_| format!("`{}` is not a region code (US, GB, PH...)", region))?) }; phonenumber::parse(id, number).map_err(|e| e.to_string())}/// `{ e164, international, national, country_code, region, valid }` for a number, read as from `region` ("US", "GB", "PH") when it has no `+`.pub fn phone_parse(number: &str, region: &str) -> Result<Value, String> { let n = parse(number, region)?; Ok(json!({ "e164": n.format().mode(Mode::E164).to_string(), "international": n.format().mode(Mode::International).to_string(), "national": n.format().mode(Mode::National).to_string(), "country_code": n.code().value(), "region": n.country().id().map(|id| format!("{:?}", id)), "valid": phonenumber::is_valid(&n), }))}/// The number in E.164 (`+14155550123`), to store and to send texts to, or an error when it is not a valid number.pub fn phone_e164(number: &str, region: &str) -> Result<String, String> { let n = parse(number, region)?; if !phonenumber::is_valid(&n) { return Err(format!("{} is not a valid phone number", number)); } Ok(n.format().mode(Mode::E164).to_string())}Related
countries.qwer · 1.0.0ISO 3166 countries for forms and addresses: alpha-2, alpha-3 and numeric codes, names in several languages, calling codes, flags, continents, currencies and EU membership.9 views · 7 downloadsCurrencies currencies.qwer · 1.0.0ISO 4217 currencies: names, symbols, minor units and the countries using them, prices formatted, and amounts turned into the cents payment APIs take.6 views · 7 downloadsLanguages languages.qwer · 1.0.0ISO 639 language codes and names in English and in the language itself, two- and three-letter codes read, and the best of your translations picked from Accept-Language.7 views · 9 downloadsCard numbers cards.qwer · 1.0.0Payment card numbers checked before they reach a payment provider: Luhn check digit, brand and length, expiry, and the number formatted or masked for receipts.7 views · 7 downloadsEmail addresses email_address.qwer · 1.0.0Email addresses checked against RFC 5322 and split into local part, domain and display name.7 views · 7 downloadsIBAN and BIC iban.qwer · 1.0.0IBANs checked (country, length, check digits) and printed as banks print them, and BICs checked.9 views · 8 downloadsThread 0 posts
On Phone numbers: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.