Phone numbers in code
One file per language that reads a number the way people type it, validates it against the national numbering plans, names its line type, writes it as E.164 and says what to dial from another country, with its tests. Nothing beyond the language itself, except the regex crate in Rust. On each page you pick the plans the file holds.
| Language | File | Needs | Tests |
|---|---|---|---|
| JavaScript and TypeScript | phone.mjs | Node 20 or later, or any current browser; no dependency | node:test |
| Python | phone.py | Python 3.9 or later; standard library only | unittest (pytest runs it too) |
| Java | Phone.java | Java 17 or later; standard library only | JUnit 5 |
| Go (Golang) | phone.go | Go 1.21 or later; standard library only | testing |
| PHP | Phone.php | PHP 8.1 or later; no dependency | PHPUnit |
| Ruby | phone.rb | Ruby 3.0 or later; no dependency | minitest (ships with Ruby) |
| C# | Phone.cs | .NET 6 or later; base library only | xUnit |
| Rust | src/phone.rs | Rust 1.80 or later; the regex crate | cargo test |
Around the file
- E.164, the format to store phone numbers inWhat E.164 is, why phone numbers are stored in it and how, how long numbers are in each plan, why stripping punctuation is not enough, and the shape check ^\+[1-9][0-9]{1,14}$ in eight languages, with why it is not validation.
- Mobile, landline or VoIP: line types at signupWhat a numbering plan says about a number's line type, which plans set VoIP ranges apart, why +1 numbers cannot be told mobile from landline by their pattern, and a signup check in eight languages.
- SMS pumping and toll fraud: what the numbering plans can stopHow SMS pumping and revenue-share fraud work, the limits a numbering plan lets you set before sending a code or placing a call (mobile ranges, your countries, a counter per block of numbers), and the code in eight languages.
One combined pattern for one country, in nine languages: the regex builder. How a number is dialled between two countries: the dialling helper.