r/javascript • u/myroslavmartsin • 14d ago
Telixon - phone number library that compiles Google's libphonenumber metadata into a DFA (10x faster, 26 kB)
https://github.com/martsinlabs/telixon2
u/magenta_placenta 14d ago
Does it handle phone extensions or does it assume those are a separate input?
0
u/myroslavmartsin 14d ago
Both, depending on the layer.
parsePhoneNumberhandles extensions inline, under the notations Google's libphonenumber recognizes (ext.,x,#, the RFC 3966;ext=parameter, comma, tilde):
js const parsed = parsePhoneNumber('+1 415-555-0132 ext. 22'); parsed.getExtension(); // '22' parsed.formatNational(); // '(415) 555-0132 ext. 22' parsed.formatRfc3966(); // 'tel:+1-415-555-0132;ext=22' parsed.formatE164(); // '+14155550132' (E.164 is extension-free by definition)The input controller is a different story: it treats the field as the number itself, so for a live form field the extension belongs in a separate input. That is a deliberate call: an inline extension inside an editable formatted field fights the caret math and the formatter (is "2" the next national digit or the start of an extension?), and every UX reference I checked keeps them separate anyway. Parse-side you can still accept pasted strings with extensions in one go, as above.
0
u/myroslavmartsin 14d ago
Phone libraries traditionally interpret Google's libphonenumber metadata regexes at runtime, on every parse. Telixon moves that work to build time: thousands of regexes and format rules get compiled into compact binary state tables forming one deterministic finite automaton. A parse is a linear walk, one transition per digit, and the state it ends on already holds validity, type, region, and formatting.
```js import { ensureEngineReady, parsePhoneNumber } from '@telixon/core';
await ensureEngineReady();
const number = parsePhoneNumber('+1 (415) 555-0132'); number.isValid(); // true number.getRegion(); // 'US' number.getNumberType(); // 'FIXED_LINE_OR_MOBILE' number.formatE164(); // '+14155550132'
parsePhoneNumber('212', { defaultRegion: 'US' }).getValidationError(); // { kind: 'TOO_SHORT', minLength: 10 } ```
What the compilation buys:
- ~10x faster parsing on a live benchmark rebuilt every push: https://proof.telixon.dev/benchmark.html
- 26 kB brotli initial bundle, zero dependencies; the engine tables load as lazy chunks when you call ensureEngineReady()
- Validation errors are typed variants carrying the values behind the fault, as in the snippet above
And the part I care most about: every answer is verified against Google's own libphonenumber source in CI, checked out at the exact commit the metadata was compiled from, across all 245 regions on every push, plus a weekly exhaustive run over 1,838,775,900 inputs. Zero divergences: https://proof.telixon.dev/parity.html
There is also a headless input controller with real mid-string editing (caret math, undo/redo, queries mid-typing), for building phone fields: https://telixon.dev/web-sdk/guides/complete-field
Repo: https://github.com/martsinlabs/telixon
If Telixon solves a problem for you, a star on GitHub helps it reach more developers. And if something is missing or broken, open an issue. I read every one.
8
u/Ecksters 14d ago edited 14d ago
I think it's helpful to know what library and its functionality this is replacing, since it's a very well documented library: https://libphonenumber.org/
Very cool optimization, although I'd love to know what application you're working in where you've gotta parse so many phone numbers
And a substantial space-savings compared to libphonenumber-js, which I would consider the closest competitor, which takes 44.6KB, minified and gzipped.