Socket
Socket
Sign inDemoInstall

libphonenumber-js

Package Overview
Dependencies
Maintainers
1
Versions
392
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libphonenumber-js - npm Package Compare versions

Comparing version 0.4.12 to 0.4.13

metadata.full.json

13

bin/update-metadata.js

@@ -34,3 +34,3 @@ #!/usr/bin/env node

// Include all regular expressions
let extended = false
var extended = false
if (command_line_arguments.extended)

@@ -42,2 +42,11 @@ {

// Included phone number types
var included_phone_number_types
if (command_line_arguments.types)
{
included_phone_number_types = command_line_arguments.types.split(',')
console.log('Included phone number types:', included_phone_number_types)
included_phone_number_types = new Set(included_phone_number_types)
}
// Download the latest `PhoneNumberMetadata.xml`

@@ -49,3 +58,3 @@ // from Google's `libphonenumber` github repository.

// Generate and compress metadata
return generate(xml, included_countries, extended)
return generate(xml, included_countries, extended, included_phone_number_types)
})

@@ -52,0 +61,0 @@ .then(function(output)

37

build/tools/generate.js

@@ -15,3 +15,3 @@ 'use strict';

exports.default = function (input, included_countries, extended) {
exports.default = function (input, included_countries, extended, included_phone_number_types) {
return _bluebird2.default.promisify(_xml2js.parseString)(input).then(function (xml) {

@@ -350,7 +350,7 @@ // https://github.com/googlei18n/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml

// between a country phone code and a country code
if (!extended) {
if (country_codes.length === 1) {
delete countries[country_codes[0]].types;
continue;
}
var all_types_required = country_codes.length > 1;
if (!extended && !included_phone_number_types && !all_types_required) {
delete countries[country_codes[0]].types;
continue;
}

@@ -368,3 +368,5 @@

// to resolve country phone code ambiguity.
if (!extended) {
// So retaining all phone number type regular expressions
// is not required in this case.
if (!extended && !included_phone_number_types) {
if (countries[country_code].leading_digits) {

@@ -381,2 +383,4 @@ delete countries[country_code].types;

// to reduce metadata size (by 5 KiloBytes).
// Or retain regular expressions just for the
// specified phone number types (if configured).
var _iteratorNormalCompletion6 = true;

@@ -394,9 +398,18 @@ var _didIteratorError6 = false;

// Retain regular expressions just for the
// specified phone number types (if configured).
if (included_phone_number_types) {
if (!all_types_required && !included_phone_number_types.has(type)) {
delete types[type];
}
}
// Remove redundant types
// (other types having the same regular expressions as this one)
phone_number_types.filter(function (key) {
return key !== type && types[key] === types[type];
}).forEach(function (key) {
return delete types[key];
});
else {
phone_number_types.filter(function (key) {
return key !== type && types[key] === types[type];
}).forEach(function (key) {
return delete types[key];
});
}
};

@@ -403,0 +416,0 @@

@@ -72,3 +72,3 @@ import _Object$keys from 'babel-runtime/core-js/object/keys';

//
export default function (input, included_countries, extended) {
export default function (input, included_countries, extended, included_phone_number_types) {
return Promise.promisify(parseString)(input).then(function (xml) {

@@ -407,7 +407,7 @@ // https://github.com/googlei18n/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml

// between a country phone code and a country code
if (!extended) {
if (country_codes.length === 1) {
delete countries[country_codes[0]].types;
continue;
}
var all_types_required = country_codes.length > 1;
if (!extended && !included_phone_number_types && !all_types_required) {
delete countries[country_codes[0]].types;
continue;
}

@@ -425,3 +425,5 @@

// to resolve country phone code ambiguity.
if (!extended) {
// So retaining all phone number type regular expressions
// is not required in this case.
if (!extended && !included_phone_number_types) {
if (countries[country_code].leading_digits) {

@@ -438,2 +440,4 @@ delete countries[country_code].types;

// to reduce metadata size (by 5 KiloBytes).
// Or retain regular expressions just for the
// specified phone number types (if configured).
var _iteratorNormalCompletion6 = true;

@@ -451,9 +455,18 @@ var _didIteratorError6 = false;

// Retain regular expressions just for the
// specified phone number types (if configured).
if (included_phone_number_types) {
if (!all_types_required && !included_phone_number_types.has(type)) {
delete types[type];
}
}
// Remove redundant types
// (other types having the same regular expressions as this one)
phone_number_types.filter(function (key) {
return key !== type && types[key] === types[type];
}).forEach(function (key) {
return delete types[key];
});
else {
phone_number_types.filter(function (key) {
return key !== type && types[key] === types[type];
}).forEach(function (key) {
return delete types[key];
});
}
};

@@ -460,0 +473,0 @@

{
"name": "libphonenumber-js",
"version": "0.4.12",
"version": "0.4.13",
"description": "A simpler (and smaller) rewrite of Google Android's popular libphonenumber library",

@@ -36,3 +36,6 @@ "main": "index.common.js",

"metadata:update": "npm run metadata:branch && npm run metadata:download && babel-node runnable/update && npm run metadata:unbranch",
"metadata:generate": "babel-node runnable/generate ../PhoneNumberMetadata.xml",
"metadata:generate": "npm-run-all metadata:generate:min metadata:generate:full metadata:generate:mobile",
"metadata:generate:min": "babel-node -- runnable/generate ../PhoneNumberMetadata.xml ../metadata.min.json",
"metadata:generate:full": "babel-node -- runnable/generate ../PhoneNumberMetadata.xml ../metadata.full.json --extended",
"metadata:generate:mobile": "babel-node -- runnable/generate ../PhoneNumberMetadata.xml ../metadata.mobile.json --types mobile --debug",
"metadata:download": "babel-node runnable/download https://raw.githubusercontent.com/googlei18n/libphonenumber/master/resources/PhoneNumberMetadata.xml PhoneNumberMetadata.xml",

@@ -39,0 +42,0 @@ "test": "mocha --compilers js:babel-core/register --colors --bail --reporter spec test/ --recursive",

@@ -99,3 +99,3 @@ # libphonenumber-js

`parsed_number` argument should be taken from the result of the `parse()` function call: `{ country, phone }`. `phone` must be a national (significant) number (i.e. no national prefix). `parsed_number` argument can also be expanded into two arguments:
`parsed_number` argument **must** be an already `parse()`d phone number (to strip national prefix from it). That means that first a phone number is `parse()`d and only then is it `format()`ted and there's no other way around it. For example, a phone number is `parse()`d before storing it in a database and then it is `forrmat()`ted each time it is read from the database. The `parsed_number` object argument can also be expanded into two string arguments (for those who prefer this kind of syntax):

@@ -105,2 +105,6 @@ ```js

format('2133734253', 'US', 'International') === '+1 213 373 4253'
// The following won't work becase the phone number argument is invalid
// (has not been parsed previously and therefore contains the `0` national prefix)
format('017212345678', 'DE', 'International_plaintext') !== '+4917212345678'
```

@@ -110,3 +114,3 @@

Determines phone number type (fixed line, mobile, toll free, etc). This function will work if `--extended` metadata is available (see [Metadata](#metadata) section of this document). The regular expressions used to differentiate between various phone number types consume a lot of space (two thirds of the total size of the `--extended` library build) therefore they're not included in the bundle by default.
Determines phone number type (fixed line, mobile, toll free, etc). This function will work if `--extended` (or relevant `--types`) metadata is available (see [Metadata](#metadata) section of this document). The regular expressions used to differentiate between various phone number types consume a lot of space (two thirds of the total size of the `--extended` library build) therefore they're not included in the bundle by default.

@@ -143,7 +147,7 @@ The arguments can be

The difference between using `parse()` and `isValidNumber()` for phone number validation is that `isValidNumber()` also checks the precise regular expressions of possible phone numbers for a country. For example, for Germany `parse('123456', 'DE')` would return `{ country: 'DE', phone: '123456' }` because this phone number matches the general phone number rules for Germany. But, if the metadata is compiled with `--extended` flag (see below) and the precise regular expressions for possible phone numbers are included in the metadata then `isValidNumber()` is gonna use those precise regular expressions for validation and `isValid('123456', 'DE')` will return `false` because the phone number `123456` doesn't actually exist in Germany.
The difference between using `parse()` and `isValidNumber()` for phone number validation is that `isValidNumber()` also checks the precise regular expressions of possible phone numbers for a country. For example, for Germany `parse('123456', 'DE')` would return `{ country: 'DE', phone: '123456' }` because this phone number matches the general phone number rules for Germany. But, if the metadata is compiled with `--extended` (or relevant `--types`) flag (see below) and the precise regular expressions for possible phone numbers are included in the metadata then `isValidNumber()` is gonna use those precise regular expressions for validation and `isValid('123456', 'DE')` will return `false` because the phone number `123456` doesn't actually exist in Germany.
So, the general phone number rules for a country are mainly for phone number formatting: they dictate how different phone numbers (matching those general regular expressions) should be formatted. And `parse()` uses only those general regular expressions (as per the reference Google's `libphonenumber` implementation) to perform basic phone number validation. `isValidNumber()`, on the other hand, is all about validation, so it digs deeper into precise regular expressions (if they're included in metadata) for possible phone numbers in a given country. And that's the difference between them: `parse()` parses phone numbers and loosely validates them while `isValidNumber()` validates phone number precisely (provided the precise regular expressions are included in metadata).
By default those precise regular expressions aren't included in metadata at all because that would cause metadata to grow twice in its size (the complete metadata would be about 200 KiloBytes). If anyone needs to generate custom metadata then it's very easy to do so: just follow the instructions provided in the [Customizing metadata](#customizing-metadata) section of this document (the option to look for is `--extended`).
By default those precise regular expressions aren't included in metadata at all because that would cause metadata to grow twice in its size (the complete metadata would be about 200 KiloBytes). If anyone needs to generate custom metadata then it's very easy to do so: just follow the instructions provided in the [Customizing metadata](#customizing-metadata) section of this document (the option to look for is `--extended`, or relevant `--types`).

@@ -243,4 +247,10 @@ ### `class` asYouType(default_country_code)

If only a specific set of countries is needed in a project, and a developer really wants to reduce the resulting bundle size, say, by 50 KiloBytes, then he can generate custom metadata and pass it as an extra argument to this library's functions. Or, say, if a developer wants to use the complete metadata (which is about 200 KiloBytes) for precise phone number validation then he can also generate such complete metadata set.
This library comes prepackaged with three flavours of metadata
* `metadata.full.json` — contains everything, including all regular expressions for precise phone number validation and getting phone number type, but weighs `130 KiloBytes`.
* `metadata.min.json` — (default) the minimal one, doesn't contain regular expressions for precise phone number validation and getting phone number type, weighs `70 KiloBytes`.
* `metadata.mobile.json` — contains regular expressions for precise **mobile** phone number validation, weighs `100 KiloBytes`.
Furthermore, if only a specific set of countries is needed in a project, and a developer really wants to reduce the resulting bundle size, say, by 50 KiloBytes (even when including all regular expressions for precise phone number validation and getting phone number type), then he can generate such custom metadata and pass it as an extra argument to this library's functions.
First, add metadata generation script to **your project's** `package.json`

@@ -258,4 +268,9 @@

The first argument is the output metadata file path. `--countries` argument is a comma-separated list of the required countries (if `--countries` is omitted then all countries are included). `--extended` argument may be passed to increase the precision of phone number validation but at the same time it will enlarge the resulting metadata size approximately twice.
The arguments are
* The first argument is the output metadata file path.
* `--countries` argument is a comma-separated list of the countries included (if `--countries` is omitted then all countries are included).
* `--extended` argument may be passed to include all regular expressions for precise phone number validation and getting phone number type, which increases the precision of phone number validation but at the same time it will enlarge the resulting metadata size approximately twice.
* `--types ...` argument may be passed instead of `--extended` to only include the precise phone number type regular expressions for a specific set of phone number types (a comma-separated list, e.g. `--types mobile,fixed`).
Then use the generated `metadata.min.json` with the exported "custom" functions.

@@ -262,0 +277,0 @@

import generate from '../source/tools/generate'
import compress from '../source/tools/compress'
import minimist from 'minimist'
import path from 'path'

@@ -8,8 +9,11 @@ import fs from 'fs'

const input = fs.readFileSync(path.join(__dirname, process.argv[2]), 'utf8')
const output_file = process.argv[3]
const command_line_arguments = minimist(process.argv.slice(4))
// Included countries
let included_countries
if (process.argv[3])
if (command_line_arguments.countries)
{
included_countries = process.argv[3].split(',')
included_countries = command_line_arguments.countries.split(',')
console.log('Included countries:', included_countries)

@@ -21,3 +25,3 @@ included_countries = new Set(included_countries)

let extended = false
if (process.argv[4] === 'extended')
if (command_line_arguments.extended)
{

@@ -28,10 +32,23 @@ console.log('Include extra validation regular expressions')

// Included phone number types
let included_phone_number_types
if (command_line_arguments.types)
{
included_phone_number_types = command_line_arguments.types.split(',')
console.log('Included phone number types:', included_phone_number_types)
included_phone_number_types = new Set(included_phone_number_types)
}
// Generate and compress metadata
generate(input, included_countries, extended).then((output) =>
generate(input, included_countries, extended, included_phone_number_types).then((output) =>
{
// Write uncompressed metadata into a file for easier debugging
fs.writeFileSync(path.join(__dirname, '../metadata.json'), JSON.stringify(output, undefined, 3))
if (command_line_arguments.debug)
{
console.log('Output uncompressed JSON for debugging')
fs.writeFileSync(path.join(__dirname, '../metadata.json'), JSON.stringify(output, undefined, 3))
}
// Compress the generated metadata
fs.writeFileSync(path.join(__dirname, '../metadata.min.json'), JSON.stringify(compress(output)))
fs.writeFileSync(path.join(__dirname, output_file), JSON.stringify(compress(output)))
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc