Convert Figma logo to code with AI

nyaruka logophonenumbers

GoLang port of Google's libphonenumber library

1,196
145
1,196
6

Top Related Projects

Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers.

Python port of Google's libphonenumber

PHP version of Google's phone number handling library

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript

A JavaScript plugin for entering and validating international telephone numbers. Now includes React and Vue components.

Quick Overview

nyaruka/phonenumbers is a Go port of Google's libphonenumber library. It provides functionality for parsing, formatting, and validating international phone numbers. This library is particularly useful for applications that need to handle phone numbers from various countries and formats.

Pros

  • Comprehensive support for international phone numbers
  • Regular updates to keep pace with changes in global phone number systems
  • High performance and efficient memory usage
  • Well-documented API with clear examples

Cons

  • May have a steeper learning curve compared to simpler phone number libraries
  • Requires periodic updates to maintain accuracy with changing phone number formats
  • Large library size due to extensive country-specific data
  • Some advanced features may be overkill for simple use cases

Code Examples

Parsing a phone number:

number, err := phonenumbers.Parse("+1 (202) 555-0123", "US")
if err != nil {
    log.Fatal(err)
}
fmt.Println(phonenumbers.Format(number, phonenumbers.INTERNATIONAL))

Validating a phone number:

number, err := phonenumbers.Parse("+44 20 7031 3000", "GB")
if err != nil {
    log.Fatal(err)
}
fmt.Println(phonenumbers.IsValidNumber(number))

Formatting a phone number:

number, err := phonenumbers.Parse("+81 3-1234-5678", "JP")
if err != nil {
    log.Fatal(err)
}
fmt.Println(phonenumbers.Format(number, phonenumbers.NATIONAL))

Getting Started

To use nyaruka/phonenumbers in your Go project:

  1. Install the library:

    go get github.com/nyaruka/phonenumbers
    
  2. Import the library in your Go code:

    import "github.com/nyaruka/phonenumbers"
    
  3. Start using the library functions, for example:

    number, err := phonenumbers.Parse("+1 650-253-0000", "US")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(phonenumbers.IsValidNumber(number))
    

Competitor Comparisons

Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers.

Pros of libphonenumber

  • More comprehensive and actively maintained by Google
  • Supports a wider range of phone number formats and countries
  • Offers additional features like carrier lookup and time zone detection

Cons of libphonenumber

  • Larger library size, which may impact performance in some applications
  • More complex API, potentially requiring a steeper learning curve
  • Higher memory usage due to extensive metadata

Code Comparison

libphonenumber:

from phonenumbers import parse, format_number, PhoneNumberFormat

number = parse("+1 650-253-0000", None)
formatted = format_number(number, PhoneNumberFormat.INTERNATIONAL)

phonenumbers:

import phonenumbers

number = phonenumbers.parse("+1 650-253-0000", None)
formatted = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)

Both libraries offer similar basic functionality for parsing and formatting phone numbers. However, libphonenumber provides more advanced features and broader support for international numbers, while phonenumbers aims to be a lighter-weight alternative with a focus on core functionality.

Python port of Google's libphonenumber

Pros of python-phonenumbers

  • More comprehensive and actively maintained
  • Supports a wider range of phone number formats and countries
  • Better documentation and examples

Cons of python-phonenumbers

  • Larger package size, which may impact performance
  • More complex API, potentially steeper learning curve

Code Comparison

python-phonenumbers:

import phonenumbers

number = phonenumbers.parse("+1 650 253 0000", None)
print(phonenumbers.is_valid_number(number))
print(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL))

phonenumbers:

from phonenumbers import parse, is_valid_number, format_number, PhoneNumberFormat

number = parse("+1 650 253 0000", None)
print(is_valid_number(number))
print(format_number(number, PhoneNumberFormat.INTERNATIONAL))

The code usage is very similar between the two libraries, with python-phonenumbers offering a slightly more intuitive import structure. Both libraries provide similar functionality for parsing, validating, and formatting phone numbers. However, python-phonenumbers generally offers more advanced features and better support for edge cases and international numbers.

PHP version of Google's phone number handling library

Pros of libphonenumber-for-php

  • More comprehensive and feature-rich, offering a wider range of functionalities
  • Better maintained with more frequent updates and contributions
  • Closer to the original Google's libphonenumber implementation

Cons of libphonenumber-for-php

  • Larger codebase and dependencies, potentially leading to a heavier footprint
  • May be more complex to use and integrate for simpler use cases
  • Requires more system resources due to its comprehensive nature

Code Comparison

libphonenumber-for-php:

$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$phoneNumber = $phoneUtil->parse("+1-202-555-0123", "US");
$isValid = $phoneUtil->isValidNumber($phoneNumber);

phonenumbers:

$phone = \Nyaruka\PhoneNumbers\PhoneNumber::parse("+1-202-555-0123");
$isValid = $phone->isValid();

Both libraries provide similar functionality for parsing and validating phone numbers. However, libphonenumber-for-php offers a more extensive API with additional methods and options, while phonenumbers provides a simpler, more straightforward approach for basic use cases.

The choice between these libraries depends on the specific requirements of your project, considering factors such as feature set, performance, and ease of use.

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript

Pros of libphonenumber-js

  • Lightweight and optimized for browser usage
  • Supports tree-shaking for reduced bundle size
  • Provides a simpler API for basic phone number operations

Cons of libphonenumber-js

  • Less comprehensive coverage of phone number formats and metadata
  • May not be as up-to-date with the latest phone number changes
  • Limited support for certain advanced features available in phonenumbers

Code Comparison

phonenumbers:

import phonenumbers

number = phonenumbers.parse("+12125552368", None)
print(phonenumbers.is_valid_number(number))
print(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL))

libphonenumber-js:

import { parsePhoneNumber } from 'libphonenumber-js'

const number = parsePhoneNumber('+12125552368')
console.log(number.isValid())
console.log(number.format('INTERNATIONAL'))

Both libraries provide similar functionality for parsing and formatting phone numbers, but with different syntax and method names. phonenumbers offers a more extensive set of features, while libphonenumber-js focuses on simplicity and browser optimization.

A JavaScript plugin for entering and validating international telephone numbers. Now includes React and Vue components.

Pros of intl-tel-input

  • User-friendly interface with country flag dropdown and automatic formatting
  • Lightweight and easy to integrate into web applications
  • Extensive customization options for appearance and behavior

Cons of intl-tel-input

  • Limited to client-side validation and formatting
  • Requires additional server-side validation for complete phone number verification
  • May not be suitable for non-web-based applications

Code Comparison

intl-tel-input:

$("#phone").intlTelInput({
  utilsScript: "path/to/utils.js",
  preferredCountries: ["us", "gb"],
  separateDialCode: true
});

phonenumbers:

import phonenumbers

number = phonenumbers.parse("+12125552368", None)
print(phonenumbers.is_valid_number(number))
print(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL))

intl-tel-input focuses on providing a user-friendly input interface for phone numbers, while phonenumbers offers more comprehensive parsing, validation, and formatting capabilities. intl-tel-input is ideal for web-based applications requiring an intuitive user interface, whereas phonenumbers is better suited for backend processing and cross-platform applications needing robust phone number handling.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

☎️ phonenumbers

Build Status codecov GoDoc

golang port of Google's libphonenumber forked from libphonenumber from ttacon/libphonenumber. This library is used daily in production for parsing and validation of numbers across the world, so is well maintained. Please open an issue if you encounter any problems, we'll do our best to address them.

[!IMPORTANT] The aim of this project is strictly to be a port and match as closely as possible the functionality in libphonenumber. Please don't submit feature requests for functionality that doesn't exist in libphonenumber.

[!IMPORTANT] We use the metadata from libphonenumber so if you encounter unexpected parsing results, please first verify if the problem affects libphonenumber and report there if so. You can use their online demo to quickly check parsing results.

Version Numbers

As we don't want to bump our major semantic version number in step with the upstream library, we use independent version numbers than the Google libphonenumber repo. The release notes will mention what version of the metadata a release was built against.

Usage

// parse our phone number
num, err := phonenumbers.Parse("6502530000", "US")

// format it using national format
formattedNum := phonenumbers.Format(num, phonenumbers.NATIONAL)

Updating Metadata

The buildmetadata command will fetch the latest XML file from the official Google repo and rebuild the go source files containing all the territory metadata, timezone and region maps.

It will rebuild the following files:

  • gen/metadata_bin.go - protocol buffer definitions for all the various formats across countries etc..
  • gen/shortnumber_metadata_bin.go - protocol buffer definitions for ShortNumberMetadata.xml
  • gen/countrycode_to_region_bin.go - information needed to map a contry code to a region
  • gen/prefix_to_carrier_bin.go - information needed to map a phone number prefix to a carrier
  • gen/prefix_to_geocoding_bin.go - information needed to map a phone number prefix to a city or region
  • gen/prefix_to_timezone_bin.go - information needed to map a phone number prefix to a city or region
% go install github.com/nyaruka/phonenumbers/cmd/buildmetadata
% $GOPATH/bin/buildmetadata