npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phonenumbers_js

v0.0.13

Published

Lightweight TypeScript/JavaScript client code for PhoneNumbers validation library.

Downloads

21

Readme

Javascript API for Phone Number Classification

This module contains basic classes required to build a specialized Javascript phone number classifier. If you are using this module directly then you may need to create your own API (alternatively you may be able to find an existing classifier implementation which suits your needs).

The PhoneNumbers library allows you to build fast, efficient client code to classify and validate phone numbers. By letting clients decide what data they need, and control how it is processed, it is possible to parse, validate and format phone numbers, for some or all regions in the world, in a fraction of the code/data required by Libphonenumber.

Assuming a simple use case which requires parsing, validation and formatting for "normal" phone numbers, we can compare the required metadata size between Libphonenumber and this library

| Library | Protocol Buffer Metadata Size | JSON Metadata Size | |-----------------|-------------------------------|--------------------| | Libphonenumber | 225kB (built in) | 250kB (built in) | | PhoneNumbers | 39KB (23.5kB zipped) | 69kB (26kB zipped) |

And for a completely minimal representation, with the maximally simplified metadata, and no parsing or formatting support, the metadata size (zipped) can be as small as 5Kb.

See https://github.com/hagbard/phone-numbers/blob/main/examples/src/main/resources/README.md for more details.

Deciding on Your Features

To create your own client API, you must decide what features you application needs from a phone number classifier.

A classifier can have the following functionality:

  1. Validating phone numbers (without determining a "number type").
    • This basic functionality is present for any classifier.
  2. Classifying phone numbers with respect to some attribute.
    • E.g. "number type", "tariff", "region" etc.
    • This is optional functionality, and creating a classifier without unwanted classifiers will greatly reduce the metadata requirements.
  3. Parsing phone numbers from user entered text.
    • E.g. Parsing a number such as "(079) 555 1234" in region CH (Switzerland).
    • This is optional functionality, but enabled by default since it uses only a small amount of metadata and is commonly useful.
    • Parsing E.164 phone numbers is always available via the PhoneNumbers class.
  4. Formatting phone numbers in either "national" or "international" format.
    • E.g. Formatting an E.164 number such as "+41795551234" as "(079) 555 1234" for national dialling.
  5. Including example numbers for each region alongside parsing data.
    • This is disabled by default since example number data can be a non-trivial fraction of the total data size when using highly simplified data.

Defining Your Metadata

Once you have determined the API you want to expose, you need to write a metadata configuration file from which the metadata is generated.

Metadata is generated using code in the offline-tools artifact, and requires a simple configuration file. The configuration file should:

  1. Enable any optional "base" metadata needed by you API.
    • E.g. Enabling the REGION classifier, so you can determine the CLDR region code for phone numbers.
  2. Enable parsing or formatting data needed by your API.
  3. Define any custom classifier types for your API.
    • E.g. Defining custom types as combinations of existing base types.
    • This is how you can mimic the types present in Libphonenumber.
  4. Specify the degree to which metadata should be simplified.
    • This increases false positive matches for numbers but can greatly reduce metadata size.

Building a Custom Classifier

Once this is decided, it is easy to make your own subclass of AbstractPhoneNumberClassifier which exposes only the API you need to your business logic. You can use helper methods to easily create you classifier, parser and formatter APIs to return to your business logic, and a fully functional classifier class can easily be written in one page of code.

In JavaScript you also need to pass the JSON metadata file to the parent class during construction. This initializes all the features you will need in your subclass.

See It For Yourself

Fully working examples of classifiers are available in the tests subdirectory as well as the javascript-simple artifact. This shows that a useful, application specific, phone number classifier can be created in a short time and with minimal effort:

See these TypeScript classes for examples of classifiers:

  • javascript/tests/example-classifier.ts
  • javascript-simple/src/index.ts

And the associated metadata configuration files:

  • javascript/tests/lpn_dfa_compact.textproto
  • javascript-simple/src/simple_compact.textproto

And an example of a classifier used in JavaScript code:

  • javascript-simple/src/main.cjs

This is all it takes to prepare your own, smaller, faster, phone number classifier.