Skip to content

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.

LanguageFileNeedsTests
JavaScript and TypeScriptphone.mjsNode 20 or later, or any current browser; no dependencynode:test
Pythonphone.pyPython 3.9 or later; standard library onlyunittest (pytest runs it too)
JavaPhone.javaJava 17 or later; standard library onlyJUnit 5
Go (Golang)phone.goGo 1.21 or later; standard library onlytesting
PHPPhone.phpPHP 8.1 or later; no dependencyPHPUnit
Rubyphone.rbRuby 3.0 or later; no dependencyminitest (ships with Ruby)
C#Phone.cs.NET 6 or later; base library onlyxUnit
Rustsrc/phone.rsRust 1.80 or later; the regex cratecargo test

Around the file

One combined pattern for one country, in nine languages: the regex builder. How a number is dialled between two countries: the dialling helper.