Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
libphonenumber-js
Advanced tools
A simpler (and smaller) rewrite of Google Android's famous libphonenumber library
The libphonenumber-js npm package is a JavaScript library that provides a variety of functions for handling and manipulating phone numbers. It is based on Google's libphonenumber library and allows users to parse, format, and validate phone numbers in international formats.
Parsing and formatting phone numbers
This feature allows users to parse a string into a phone number object and format it into an international format.
import { parsePhoneNumberFromString } from 'libphonenumber-js';
const phoneNumber = parsePhoneNumberFromString('+12133734253');
console.log(phoneNumber.formatInternational()); // '+1 213 373 4253'
Validating phone numbers
This feature enables users to check if a given phone number is valid according to international standards.
import { isValidPhoneNumber } from 'libphonenumber-js';
const valid = isValidPhoneNumber('+12133734253');
console.log(valid); // true
Getting phone number information
This feature provides information about the phone number, such as the country it is associated with and the type of number (e.g., mobile, fixed-line).
import { parsePhoneNumberFromString } from 'libphonenumber-js';
const phoneNumber = parsePhoneNumberFromString('+12133734253');
console.log(phoneNumber.country); // 'US'
console.log(phoneNumber.getType()); // 'MOBILE'
This is the original phone number handling library from Google that libphonenumber-js is based on. It is more feature-rich but also larger in size, which might not be ideal for front-end usage due to its impact on bundle size.
Another library for phone number parsing, validation, and formatting. It provides similar functionalities to libphonenumber-js but with a different API design and potentially different bundle size implications.
[![NPM Version][npm-badge]][npm] [![Build Status][travis-badge]][travis] [![Test Coverage][coveralls-badge]][coveralls]
A simpler (and smaller) rewrite of Google Android's famous libphonenumber
library: easy phone number parsing and formatting in javascript.
libphonenumber
is a phone number formatting and parsing library released by Google, originally developed for (and currently used in) Google's Android mobile phone operating system. Implementing a rigorous phone number formatting and parsing library was crucial for the phone OS overall usability (back then, in the early 2000s, it was originally meant to be a phone after all, not just a SnapChat device).
libphonenumber-js
is a simplified pure javascript port of the original libphonenumber
library (written in C++ and Java because those are the programming languages used in Android OS). While libphonenumber
has an official javascript port which is being maintained by Google, it is tightly coupled to Google's closure
javascript utility framework. It still can be compiled into one big bundle which weighs 220 KiloBytes — quite a size for a phone number input component. It can be reduced to a specific set of countries only but that wouldn't be an option for a worldwide international solution.
One part of me was curious about how all this phone matching machinery worked, and another part of me was curious if there's a way to reduce those 220 KiloBytes to something more reasonable while also getting rid of the closure
library and rewriting it all in pure javascript. So, that was my little hackathon for a couple of weeks, and seems that it succeeded. The resulting library does everything a modern web application needs while maintaining a much smaller size of about 70 KiloBytes.
libphonenumber
libphonenumber
metadata size is about 200 KiloBytes1-800-GOT-MILK
as we don't use telephone sets in the XXIst century that much (and we have phonebooks in your mobile phones)format
ted are internationally diallable, because that's the only type of phone numbers users are supposed to be inputting on websites (no one inputs short codes, emergency telephone numbers like 911
, etc.)possibleDigits
data to speed up phone number pre-validation (it just skips to the regular expression check itself)011 ...
in the US (again, just use the +...
notation accepted worldwide for mobile phones)tel:...
URIs (RFC 3966) because it's not relevant for user-facing web experiencenpm install libphonenumber-js --save
import { parse, format, asYouType } from 'libphonenumber-js'
parse('8 (800) 555 35 35', 'RU')
// { country: 'RU', phone: '8005553535' }
format('2133734253', 'US', 'International')
// '+1-213-373-4253'
new asYouType().input('+12133734')
// '+1 213 373 4'
new asYouType('US').input('2133734')
// '(213) 373-4'
options
can be
country:
{
restrict — (a two-letter country code)
the phone number must be in this country
default — (a two-letter country code)
default country to use for phone number parsing and validation
(if no country code could be derived from the phone number)
}
or just a two-letter country code which is gonna be country.restrict
.
Returns { country, phone }
where country
is a two-letter country code, and phone
is a national (significant) number. If the phone number supplied isn't valid for the corresponding country then an empty object is returned.
parse('+1-213-373-4253') === { country: 'US', phone: '2133734253' }
parse('(213) 373-4253', 'US') === { country: 'US', phone: '2133734253' }
Formats an already parsed phone number in one of the following format
s:
International
— e.g. +1 213 373 4253
International_plaintext
— (aka E.164
) e.g. +12133734253
National
— e.g. (213) 373-4253
Can also be called with the first object argument expanded:
format('2133734253', 'US', 'International') === '+1-213-373-4253'
format({ country: 'US', phone: '2133734253' }, 'International') === '+1-213-373-4253'
(aka is_valid_number
)
This function is simply a wrapper for parse
: if parse
returns an empty object then the phone number is not valid.
isValidNumber('+1-213-373-4253') === true
isValidNumber('+1-213-373') === false
isValidNumber('(213) 373-4253', 'US') === true
isValidNumber('(213) 37', 'US') === false
class
asYouType(country_code)(aka as_you_type
)
Creates a formatter for partially entered phone number. The two-letter country_code
is optional and, if specified, restricts the phone number being input to the specified country. The instance of this class has two methods:
input(text)
— takes any text and appends it to the input; returns the formatted phone numberclear()
— clears inputnew asYouType().input('+12133734') === '+1 213 373 4'
new asYouType('US').input('2133734') === '(213) 373-4'
After cloning this repo, ensure dependencies are installed by running:
npm install
This module is written in ES6 and uses Babel for ES5 transpilation. Widely consumable JavaScript can be produced by running:
npm run build
Once npm run build
has run, you may import
or require()
directly from
node.
After developing, the full test suite can be evaluated by running:
npm test
When you're ready to test your new functionality on a real project, you can run
npm pack
It will build
, test
and then create a .tgz
archive which you can then install in your project folder
npm install [module name with version].tar.gz
MIT [npm]: https://www.npmjs.org/package/libphonenumber-js [npm-badge]: https://img.shields.io/npm/v/libphonenumber-js.svg?style=flat-square [travis]: https://travis-ci.org/halt-hammerzeit/libphonenumber-js [travis-badge]: https://img.shields.io/travis/halt-hammerzeit/libphonenumber-js/master.svg?style=flat-square [coveralls]: https://coveralls.io/r/halt-hammerzeit/libphonenumber-js?branch=master [coveralls-badge]: https://img.shields.io/coveralls/halt-hammerzeit/libphonenumber-js/master.svg?style=flat-square
FAQs
A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
The npm package libphonenumber-js receives a total of 2,271,668 weekly downloads. As such, libphonenumber-js popularity was classified as popular.
We found that libphonenumber-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.