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.2.25 to 0.2.26

runnable/metadata-branch.js

4

bin/update-metadata.js
#!/usr/bin/env node
var path = require('path')
var fs = require('fs')
var path = require('path')
var fs = require('fs')
var minimist = require('minimist')

@@ -6,0 +6,0 @@

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

exports.format_national_number = format_national_number;
exports.choose_format_for_number = choose_format_for_number;
exports.local_to_international_style = local_to_international_style;

@@ -140,2 +141,3 @@

// Validate leading digits
if ((0, _metadata.get_format_leading_digits_patterns)(_format).length > 0) {

@@ -145,7 +147,9 @@ // The last leading_digits_pattern is used here, as it is the most detailed

// If leading digits don't match then move on to the next phone number format
if (national_number.search(last_leading_digits_pattern) !== 0) {
return;
continue;
}
}
// Check that the national number matches the phone number format regular expression
if ((0, _common.matches_entirely)(national_number, new RegExp((0, _metadata.get_format_pattern)(_format)))) {

@@ -152,0 +156,0 @@ return _format;

@@ -43,3 +43,3 @@ "use strict";

function get_formats(country_metadata) {
return country_metadata[2];
return country_metadata[2] || [];
}

@@ -46,0 +46,0 @@

@@ -27,2 +27,3 @@ 'use strict';

exports.is_of_type = is_of_type;
exports.is_national_prefix_required = is_national_prefix_required;

@@ -33,4 +34,10 @@ var _common = require('./common');

var _format = require('./format');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var PLUS_CHARS = exports.PLUS_CHARS = '++';
// Digits accepted in phone numbers
// (ascii, fullwidth, arabic-indic, and eastern arabic digits).
// This is a port of Google Android `libphonenumber`'s

@@ -41,6 +48,2 @@ // `phonenumberutil.js` of 17th November, 2016.

var PLUS_CHARS = exports.PLUS_CHARS = '++';
// Digits accepted in phone numbers
// (ascii, fullwidth, arabic-indic, and eastern arabic digits).
var VALID_DIGITS = exports.VALID_DIGITS = '0-90-9٠-٩۰-۹';

@@ -234,3 +237,8 @@

// Whether the phone number is formatted as an international phone number
var is_international = false;
if (country_phone_code) {
is_international = true;
// Check country restriction

@@ -262,2 +270,8 @@ if (options.country.restrict && country_phone_code !== (0, _metadata.get_phone_code)(this.metadata.countries[options.country.restrict])) {

var did_have_national_prefix = national_number !== number;
if (!is_international && !did_have_national_prefix && is_national_prefix_required(national_number, country_metadata)) {
return {};
}
// Sometimes there are several countries

@@ -641,2 +655,10 @@ // corresponding to the same country phone code

}
function is_national_prefix_required(national_number, country_metadata) {
var format = (0, _format.choose_format_for_number)((0, _metadata.get_formats)(country_metadata), national_number);
if (format) {
return (0, _metadata.get_format_national_prefix_is_mandatory_when_formatting)(format, country_metadata);
}
}
//# sourceMappingURL=parse.js.map

@@ -16,4 +16,6 @@ 'use strict';

function is_valid(number, country_code) {
var parsed = _parse2.default.call(this, number, country_code);
function is_valid(parsed, country_code) {
if (typeof parsed === 'string') {
parsed = _parse2.default.call(this, parsed, country_code);
}

@@ -20,0 +22,0 @@ if (!parsed.country) {

@@ -0,1 +1,6 @@

0.2.26 / 02.01.2016
===================
* Added national prefix check for `parse` and `isPhoneValid`
0.2.25 / 30.12.2016

@@ -2,0 +7,0 @@ ===================

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

@@ -30,4 +30,7 @@ "main": "index.common.js",

"scripts": {
"metadata:pull-request": "babel-node runnable/metadata-pull-request",
"metadata:branch": "babel-node runnable/metadata-branch",
"metadata:unbranch": "babel-node runnable/metadata-unbranch",
"metadata:publish": "npm version patch && npm publish && git push",
"metadata:update": "npm run metadata:download && babel-node runnable/update",
"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",

@@ -34,0 +37,0 @@ "metadata:download": "babel-node runnable/download https://raw.githubusercontent.com/googlei18n/libphonenumber/master/resources/PhoneNumberMetadata.xml PhoneNumberMetadata.xml",

@@ -81,4 +81,8 @@ # libphonenumber-js

Returns `{ country, phone }` where `country` is a [country code](https://github.com/halt-hammerzeit/libphonenumber-js#country-code-definition), and `phone` is a national (significant) number. If the phone number supplied isn't valid then an empty object `{}` is returned.
Returns `{ country, phone }` where
* `country` is a [country code](https://github.com/halt-hammerzeit/libphonenumber-js#country-code-definition)
* `phone` is a national (significant) number
If the phone number supplied isn't valid then an empty object `{}` is returned.
```js

@@ -103,3 +107,3 @@ parse('+1-213-373-4253') === { country: 'US', phone: '2133734253' }

### isValidNumber(number, country_code)
### isValidNumber(parsed_number)

@@ -110,9 +114,23 @@ (aka `is_valid_number`)

The arguments can be
* either the result of the `parse()` function call: `{ country, phone }`
* or a pair of arguments `(phone, country_code)` which will then be simply passed to the `parse()` function for parsing
```js
isValidNumber('+1-213-373-4253') === true
isValidNumber('+1-213-373') === false
isValidNumber('(213) 373-4253', 'US') === true
isValidNumber('(213) 37', 'US') === false
isValidNumber({ phone: '2133734253', country: 'US' }) === true
```
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.
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`).
### `class` asYouType(default_country_code)

@@ -147,7 +165,7 @@

## Metadata generation
## Metadata
Metadata is generated from Google's original [`PhoneNumberMetadata.xml`](https://github.com/googlei18n/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml) by transforming XML into JSON and removing unnecessary fields.
How to update metadata:
Currently I have a daily bash script set up monitoring the changes to `PhoneNumberMetadata.xml` in Google's repo and it automatically creates a Pull Request when the metadata is updated. So this project's metadata is supposed to be up-to-date. Still, in case the automatic metadata update script malfunctions some day, anyone can request metadata update via a Pull Request here on GitHub:

@@ -157,7 +175,7 @@ * Fork this repo

* `npm run metadata:update`
* Submit a pull request
* Submit a Pull Request to the main repo from the `update-metadata` branch of your fork
To update metadata first it downloads the new [`PhoneNumberMetadata.xml`](https://github.com/googlei18n/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml) into the project folder replacing the old one. Then it generates JSON metadata out of the XML one. After that it runs the tests and commits the new metadata.
`npm run metadata:update` command creates a new `update-metadata` branch, downloads the new [`PhoneNumberMetadata.xml`](https://github.com/googlei18n/libphonenumber/blob/master/resources/PhoneNumberMetadata.xml) into the project folder replacing the old one, generates JSON metadata out of the XML one, runs the tests, commits the new metadata and pushes the commit to the remote `update-metadata` branch of your fork.
Alternatively, a developer may wish to update metadata instantly, without waiting for pull requests. In this case just perform the steps described in the [Including only a specific set of countries](#including-only-a-specific-set-of-countries) section of this document (just don't pass the `--countries` option).
Alternatively, a developer may wish to update metadata urgently, without waiting for a pull request approval. In this case just perform the steps described in the [Customizing metadata](#customizing-metadata) section of this document (just don't pass the `--countries` option).

@@ -201,7 +219,7 @@ ## React

## Including only a specific set of countries
## Customizing metadata
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.
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.
First, add metadata generation script to the project's `package.json`
First, add metadata generation script to your project's `package.json`

@@ -218,3 +236,3 @@ ```js

The first argument is the output metadata file path. `--countries` argument is a comma-separated list of the required countries. `--extended` argument may be passed to increase the precision of the phone number validation function but at the same time it will enlarge the resulting metadata size approximately twice.
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.

@@ -235,2 +253,6 @@ Then use the generated `metadata.min.json` with the `libphonenumber-js/custom` functions

## To do
* Check that `metadata:update` and `metadata:pull-request` scripts work as intended and [add a daily `launchd` job](http://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs) for `npm run metadata:update && npm run metadata:pull-request`
## Contributing

@@ -237,0 +259,0 @@

@@ -8,30 +8,82 @@ var child_process = require('child_process')

var metadata_changed = exec('git ls-files -m PhoneNumberMetadata.xml')
var metadata_changed = exec('git ls-files --modified PhoneNumberMetadata.xml')
if (metadata_changed)
if (!metadata_changed)
{
console.log()
console.log('========================================')
console.log('= Metadata has changed, updating files =')
console.log('= Metadata is up-to-date. Exiting. =')
console.log('========================================')
console.log()
console.log(exec('npm run metadata:generate'))
// The absense of the `update-metadata` branch will tell the script
// that the metadata is up-to-date and doesn't need updating
console.log(exec('git checkout master'))
console.log(exec('git branch -D update-metadata'))
process.exit(0)
}
console.log()
console.log('========================================')
console.log('= Metadata has changed, updating files =')
console.log('========================================')
console.log()
console.log(exec('npm run metadata:generate'))
console.log()
console.log('========================================')
console.log('= Running tests =')
console.log('========================================')
console.log()
console.log(exec('npm test'))
var modified_files = exec('git ls-files --modified').split(/\s/)
if (modified_files.length > 2)
{
console.log()
console.log('========================================')
console.log('= Running tests =')
console.log('= Error =')
console.log('========================================')
console.log()
console.log('Only `PhoneNumberMetadata.xml` and `metadata.min.json` should be modified.')
console.log()
console.log(modified_files.join('\n'))
console.log(exec('npm test'))
process.exit(1)
}
// http://stackoverflow.com/questions/33610682/git-list-of-staged-files
var staged_files = exec('git diff --name-only --cached').split(/\s/)
if (staged_files.length > 0)
{
console.log()
console.log('========================================')
console.log('= Committing changes =')
console.log('= Error =')
console.log('========================================')
console.log()
console.log('There are some staged files already. Aborting metadata update process.')
console.log()
console.log(staged_files.join('\n'))
console.log(exec('git add PhoneNumberMetadata.xml metadata.min.json'))
console.log(exec('git commit -m "Phone number medatada update"'))
}
process.exit(1)
}
console.log()
console.log('========================================')
console.log('= Committing changes =')
console.log('========================================')
console.log()
console.log(exec('git add PhoneNumberMetadata.xml metadata.min.json'))
console.log(exec('git commit -m "Phone number medatada update"'))
console.log(exec('git push'))
console.log()
console.log('========================================')
console.log('= Finished =')
console.log('========================================')

@@ -150,6 +150,7 @@ // This is a port of Google Android `libphonenumber`'s

function choose_format_for_number(available_formats, national_number)
export function choose_format_for_number(available_formats, national_number)
{
for (let format of available_formats)
{
// Validate leading digits
if (get_format_leading_digits_patterns(format).length > 0)

@@ -160,8 +161,10 @@ {

// If leading digits don't match then move on to the next phone number format
if (national_number.search(last_leading_digits_pattern) !== 0)
{
return
continue
}
}
// Check that the national number matches the phone number format regular expression
if (matches_entirely(national_number, new RegExp(get_format_pattern(format))))

@@ -168,0 +171,0 @@ {

@@ -13,3 +13,3 @@ export function get_phone_code(country_metadata)

{
return country_metadata[2]
return country_metadata[2] || []
}

@@ -16,0 +16,0 @@

@@ -16,2 +16,3 @@ // This is a port of Google Android `libphonenumber`'s

get_metadata_by_country_phone_code,
get_formats,
get_type_fixed_line,

@@ -26,6 +27,13 @@ get_type_mobile,

get_type_voip,
get_type_shared_cost
get_type_shared_cost,
get_format_national_prefix_is_mandatory_when_formatting
}
from './metadata'
import
{
choose_format_for_number
}
from './format'
export const PLUS_CHARS = '+\uFF0B'

@@ -249,4 +257,9 @@

// Whether the phone number is formatted as an international phone number
let is_international = false
if (country_phone_code)
{
is_international = true
// Check country restriction

@@ -283,2 +296,10 @@ if (options.country.restrict &&

const did_have_national_prefix = national_number !== number
if (!is_international && !did_have_national_prefix &&
is_national_prefix_required(national_number, country_metadata))
{
return {}
}
// Sometimes there are several countries

@@ -670,2 +691,12 @@ // corresponding to the same country phone code

return matches_entirely(national_number, type)
}
export function is_national_prefix_required(national_number, country_metadata)
{
const format = choose_format_for_number(get_formats(country_metadata), national_number)
if (format)
{
return get_format_national_prefix_is_mandatory_when_formatting(format, country_metadata)
}
}

@@ -9,5 +9,8 @@ import parse, { get_number_type } from './parse'

export default function is_valid(number, country_code)
export default function is_valid(parsed, country_code)
{
const parsed = parse.call(this, number, country_code)
if (typeof parsed === 'string')
{
parsed = parse.call(this, parsed, country_code)
}

@@ -14,0 +17,0 @@ if (!parsed.country)

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 not supported yet

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

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