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.2 to 0.4.3

build/get number type.js

117

build/parse.js

@@ -25,4 +25,2 @@ 'use strict';

exports.find_country_code = find_country_code;
exports.get_number_type = get_number_type;
exports.is_of_type = is_of_type;
exports.is_national_prefix_required = is_national_prefix_required;

@@ -36,8 +34,8 @@

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

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

var PLUS_CHARS = exports.PLUS_CHARS = '+\uFF0B';
// Digits accepted in phone numbers
// (ascii, fullwidth, arabic-indic, and eastern arabic digits).
var VALID_DIGITS = exports.VALID_DIGITS = '0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9';

@@ -242,3 +244,3 @@

if (!country_phone_code && !number) {
if (!number) {
return {};

@@ -578,3 +580,3 @@ }

// fixed-line/mobile/etc regular expressions.
else if (get_number_type(national_phone_number, country_code, metadata)) {
else if ((0, _getNumberType2.default)({ phone: national_phone_number, country: country_code }, metadata)) {
return country_code;

@@ -599,101 +601,2 @@ }

// Finds out national phone number type (fixed line, mobile, etc)
function get_number_type(national_number, country_code, metadata) {
var country_metadata = metadata.countries[country_code];
// Is this national number even valid for this country
if (!is_of_type(national_number, (0, _metadata.get_national_number_pattern)(country_metadata))) {
return;
}
if (is_of_type(national_number, (0, _metadata.get_type_mobile)(country_metadata))) {
// Because duplicate regular expressions are removed
// to reduce metadata size, if there's no "fixed line" pattern
// then it means it was removed due to being a duplicate of some other pattern.
//
// Also, many times fixed line phone number regular expressions
// are the same as mobile phone number regular expressions,
// so in these cases there's no differentiation between them.
//
// (no such country in the metadata, therefore no unit test for this `if`)
/* istanbul ignore if */
if (!(0, _metadata.get_type_fixed_line)(country_metadata) || (0, _metadata.get_type_fixed_line)(country_metadata) === (0, _metadata.get_type_mobile)(country_metadata)) {
return 'FIXED_LINE_OR_MOBILE';
}
return 'MOBILE';
}
// Is it fixed line number
if (is_of_type(national_number, (0, _metadata.get_type_fixed_line)(country_metadata))) {
// Because duplicate regular expressions are removed
// to reduce metadata size, if there's no "mobile" pattern
// then it means it was removed due to being a duplicate of some other pattern.
//
// Also, many times fixed line phone number regular expressions
// are the same as mobile phone number regular expressions,
// so in these cases there's no differentiation between them.
//
if (!(0, _metadata.get_type_mobile)(country_metadata) || (0, _metadata.get_type_mobile)(country_metadata) === (0, _metadata.get_type_fixed_line)(country_metadata)) {
return 'FIXED_LINE_OR_MOBILE';
}
return 'FIXED_LINE';
}
if (is_of_type(national_number, (0, _metadata.get_type_toll_free)(country_metadata))) {
return 'TOLL_FREE';
}
if (is_of_type(national_number, (0, _metadata.get_type_premium_rate)(country_metadata))) {
return 'PREMIUM_RATE';
}
if (is_of_type(national_number, (0, _metadata.get_type_personal_number)(country_metadata))) {
return 'PERSONAL_NUMBER';
}
/* istanbul ignore if */
if (is_of_type(national_number, (0, _metadata.get_type_voice_mail)(country_metadata))) {
return 'VOICEMAIL';
}
/* istanbul ignore if */
if (is_of_type(national_number, (0, _metadata.get_type_uan)(country_metadata))) {
return 'UAN';
}
/* istanbul ignore if */
if (is_of_type(national_number, (0, _metadata.get_type_pager)(country_metadata))) {
return 'PAGER';
}
/* istanbul ignore if */
if (is_of_type(national_number, (0, _metadata.get_type_voip)(country_metadata))) {
return 'VOIP';
}
/* istanbul ignore if */
if (is_of_type(national_number, (0, _metadata.get_type_shared_cost)(country_metadata))) {
return 'SHARED_COST';
}
}
function is_of_type(national_number, type) {
// // Check if any possible number lengths are present;
// // if so, we use them to avoid checking
// // the validation pattern if they don't match.
// // If they are absent, this means they match
// // the general description, which we have
// // already checked before a specific number type.
// if (get_possible_lengths(type) &&
// get_possible_lengths(type).indexOf(national_number.length) === -1)
// {
// return false
// }
// get_type_pattern(type) === type
return (0, _common.matches_entirely)(national_number, type);
}
function is_national_prefix_required(national_number, country_metadata) {

@@ -700,0 +603,0 @@ var format = (0, _format.choose_format_for_number)((0, _metadata.get_formats)(country_metadata), national_number);

@@ -12,2 +12,6 @@ 'use strict';

var _getNumberType = require('./get number type');
var _getNumberType2 = _interopRequireDefault(_getNumberType);
var _metadata = require('./metadata');

@@ -31,3 +35,3 @@

function is_valid(first_argument, second_argument, third_argument) {
var _sort_out_arguments = sort_out_arguments(first_argument, second_argument, third_argument),
var _sort_out_arguments = (0, _getNumberType.sort_out_arguments)(first_argument, second_argument, third_argument),
input = _sort_out_arguments.input,

@@ -54,3 +58,3 @@ metadata = _sort_out_arguments.metadata;

if ((0, _metadata.get_types)(country_metadata)) {
if (!(0, _parse.get_number_type)(input.phone, input.country, metadata)) {
if (!(0, _getNumberType2.default)(input, metadata)) {
return false;

@@ -62,47 +66,2 @@ }

}
// Sort out arguments
function sort_out_arguments(first_argument, second_argument, third_argument) {
var input = void 0;
var metadata = void 0;
if (typeof first_argument === 'string') {
// If country code is supplied
if (typeof second_argument === 'string') {
metadata = third_argument;
// `parse` extracts phone numbers from raw text,
// therefore it will cut off all "garbage" characters,
// while this `validate` function needs to verify
// that the phone number contains no "garbage"
// therefore the explicit `is_viable_phone_number` check.
if ((0, _parse.is_viable_phone_number)(first_argument)) {
input = (0, _parse2.default)(first_argument, second_argument, metadata);
}
}
// Just an international phone number is supplied
else {
metadata = second_argument;
// `parse` extracts phone numbers from raw text,
// therefore it will cut off all "garbage" characters,
// while this `validate` function needs to verify
// that the phone number contains no "garbage"
// therefore the explicit `is_viable_phone_number` check.
if ((0, _parse.is_viable_phone_number)(first_argument)) {
input = (0, _parse2.default)(first_argument, metadata);
}
}
} else {
// The `first_argument` must be a valid phone number
// as a whole, not just a part of it which gets parsed here.
if (first_argument && first_argument.phone && (0, _parse.is_viable_phone_number)(first_argument.phone)) {
input = first_argument;
}
metadata = second_argument;
}
return { input: input, metadata: metadata };
}
//# sourceMappingURL=validate.js.map

@@ -7,3 +7,3 @@ 'use strict'

exports.format = require('./build/format').default
exports.get_number_type = require('./build/parse').get_number_type
exports.get_number_type = require('./build/get number type').default
exports.is_valid_number = require('./build/validate').default

@@ -10,0 +10,0 @@ exports.as_you_type = require('./build/as you type').default

@@ -10,6 +10,8 @@ import _getIterator from 'babel-runtime/core-js/get-iterator';

import { get_phone_code, get_national_number_pattern, get_national_prefix_for_parsing, get_national_prefix_transform_rule, get_leading_digits, get_metadata_by_country_phone_code, get_formats, get_type_fixed_line, get_type_mobile, get_type_toll_free, get_type_premium_rate, get_type_personal_number, get_type_voice_mail, get_type_uan, get_type_pager, get_type_voip, get_type_shared_cost, get_format_national_prefix_is_mandatory_when_formatting } from './metadata';
import { get_phone_code, get_national_number_pattern, get_national_prefix_for_parsing, get_national_prefix_transform_rule, get_leading_digits, get_metadata_by_country_phone_code, get_formats, get_format_national_prefix_is_mandatory_when_formatting } from './metadata';
import { choose_format_for_number } from './format';
import get_number_type from './get number type';
export var PLUS_CHARS = '+\uFF0B';

@@ -212,3 +214,3 @@

if (!country_phone_code && !number) {
if (!number) {
return {};

@@ -548,3 +550,3 @@ }

// fixed-line/mobile/etc regular expressions.
else if (get_number_type(national_phone_number, country_code, metadata)) {
else if (get_number_type({ phone: national_phone_number, country: country_code }, metadata)) {
return country_code;

@@ -569,101 +571,2 @@ }

// Finds out national phone number type (fixed line, mobile, etc)
export function get_number_type(national_number, country_code, metadata) {
var country_metadata = metadata.countries[country_code];
// Is this national number even valid for this country
if (!is_of_type(national_number, get_national_number_pattern(country_metadata))) {
return;
}
if (is_of_type(national_number, get_type_mobile(country_metadata))) {
// Because duplicate regular expressions are removed
// to reduce metadata size, if there's no "fixed line" pattern
// then it means it was removed due to being a duplicate of some other pattern.
//
// Also, many times fixed line phone number regular expressions
// are the same as mobile phone number regular expressions,
// so in these cases there's no differentiation between them.
//
// (no such country in the metadata, therefore no unit test for this `if`)
/* istanbul ignore if */
if (!get_type_fixed_line(country_metadata) || get_type_fixed_line(country_metadata) === get_type_mobile(country_metadata)) {
return 'FIXED_LINE_OR_MOBILE';
}
return 'MOBILE';
}
// Is it fixed line number
if (is_of_type(national_number, get_type_fixed_line(country_metadata))) {
// Because duplicate regular expressions are removed
// to reduce metadata size, if there's no "mobile" pattern
// then it means it was removed due to being a duplicate of some other pattern.
//
// Also, many times fixed line phone number regular expressions
// are the same as mobile phone number regular expressions,
// so in these cases there's no differentiation between them.
//
if (!get_type_mobile(country_metadata) || get_type_mobile(country_metadata) === get_type_fixed_line(country_metadata)) {
return 'FIXED_LINE_OR_MOBILE';
}
return 'FIXED_LINE';
}
if (is_of_type(national_number, get_type_toll_free(country_metadata))) {
return 'TOLL_FREE';
}
if (is_of_type(national_number, get_type_premium_rate(country_metadata))) {
return 'PREMIUM_RATE';
}
if (is_of_type(national_number, get_type_personal_number(country_metadata))) {
return 'PERSONAL_NUMBER';
}
/* istanbul ignore if */
if (is_of_type(national_number, get_type_voice_mail(country_metadata))) {
return 'VOICEMAIL';
}
/* istanbul ignore if */
if (is_of_type(national_number, get_type_uan(country_metadata))) {
return 'UAN';
}
/* istanbul ignore if */
if (is_of_type(national_number, get_type_pager(country_metadata))) {
return 'PAGER';
}
/* istanbul ignore if */
if (is_of_type(national_number, get_type_voip(country_metadata))) {
return 'VOIP';
}
/* istanbul ignore if */
if (is_of_type(national_number, get_type_shared_cost(country_metadata))) {
return 'SHARED_COST';
}
}
export function is_of_type(national_number, type) {
// // Check if any possible number lengths are present;
// // if so, we use them to avoid checking
// // the validation pattern if they don't match.
// // If they are absent, this means they match
// // the general description, which we have
// // already checked before a specific number type.
// if (get_possible_lengths(type) &&
// get_possible_lengths(type).indexOf(national_number.length) === -1)
// {
// return false
// }
// get_type_pattern(type) === type
return matches_entirely(national_number, type);
}
export function is_national_prefix_required(national_number, country_metadata) {

@@ -670,0 +573,0 @@ var format = choose_format_for_number(get_formats(country_metadata), national_number);

@@ -1,2 +0,3 @@

import parse, { get_number_type, is_viable_phone_number } from './parse';
import parse, { is_viable_phone_number } from './parse';
import get_number_type, { sort_out_arguments } from './get number type';

@@ -41,3 +42,3 @@ import { get_types } from './metadata';

if (get_types(country_metadata)) {
if (!get_number_type(input.phone, input.country, metadata)) {
if (!get_number_type(input, metadata)) {
return false;

@@ -49,47 +50,2 @@ }

}
// Sort out arguments
function sort_out_arguments(first_argument, second_argument, third_argument) {
var input = void 0;
var metadata = void 0;
if (typeof first_argument === 'string') {
// If country code is supplied
if (typeof second_argument === 'string') {
metadata = third_argument;
// `parse` extracts phone numbers from raw text,
// therefore it will cut off all "garbage" characters,
// while this `validate` function needs to verify
// that the phone number contains no "garbage"
// therefore the explicit `is_viable_phone_number` check.
if (is_viable_phone_number(first_argument)) {
input = parse(first_argument, second_argument, metadata);
}
}
// Just an international phone number is supplied
else {
metadata = second_argument;
// `parse` extracts phone numbers from raw text,
// therefore it will cut off all "garbage" characters,
// while this `validate` function needs to verify
// that the phone number contains no "garbage"
// therefore the explicit `is_viable_phone_number` check.
if (is_viable_phone_number(first_argument)) {
input = parse(first_argument, metadata);
}
}
} else {
// The `first_argument` must be a valid phone number
// as a whole, not just a part of it which gets parsed here.
if (first_argument && first_argument.phone && is_viable_phone_number(first_argument.phone)) {
input = first_argument;
}
metadata = second_argument;
}
return { input: input, metadata: metadata };
}
//# sourceMappingURL=validate.js.map
import metadata from './metadata.min.json'
import parseCustom, { get_number_type as getNumberTypeCustom } from './es6/parse'
import parseCustom from './es6/parse'
import getNumberTypeCustom from './es6/get number type'
import formatCustom from './es6/format'

@@ -69,5 +70,4 @@ import isValidNumberCustom from './es6/validate'

export { default as isValidNumberCustom } from './es6/validate'
export { default as getNumberTypeCustom } from './es6/get number type'
export { get_number_type as getNumberTypeCustom } from './es6/parse'
export

@@ -74,0 +74,0 @@ {

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

@@ -31,2 +31,3 @@ "main": "index.common.js",

"scripts": {
"libphonenumber-metadata": "libphonenumber-generate-metadata metadata.min.json --countries ID --extended",
"metadata:pull-request": "babel-node runnable/metadata-pull-request",

@@ -33,0 +34,0 @@ "metadata:branch": "babel-node runnable/metadata-branch",

@@ -106,6 +106,11 @@ # libphonenumber-js

### getNumberType(national_number, country_code)
### getNumberType(parsed_number)
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.
The arguments can be
* either the result of the `parse()` function call: `{ country, phone }`
* or a pair of arguments `(phone, country_code)` in which case it is passed to the `parse()` function right away and the resulting `{ country, phone }` object is used
```js

@@ -124,3 +129,3 @@ getNumberType('8005553535', 'RU') === 'MOBILE'

* 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
* or a pair of arguments `(phone, country_code)` in which case it is passed to the `parse()` function right away and the resulting `{ country, phone }` object is used

@@ -251,5 +256,5 @@ ```js

export const parse = (...args) => parseCustom(...args, metadata);
export const format = (...args) => formatCustom(...args, metadata);
export const isValidNumber = (...args) => isValidNumberCustom(...args, metadata);
export const parse = (...args) => parseCustom(...args, metadata)
export const format = (...args) => formatCustom(...args, metadata)
export const isValidNumber = (...args) => isValidNumberCustom(...args, metadata)

@@ -296,2 +301,11 @@ export class asYouType extends asYouTypeCustom {

ES6 "tree-shaking" is a non-trivial thing and at the moment of writing it's not guaranteed that a given ES6-aware bundler will actually be intelligent enough to tree-shake unused code, so there's always another option for those cases (if they arise): using `libphonenumber-js/custom` [Common.js](https://auth0.com/blog/javascript-module-systems-showdown/) export.
```js
import { parse as parseCustom } from 'libphonenumber-js/custom'
import metadata from './metadata.min.json'
export const parse = (...args) => parseCustom(...args, metadata)
```
<!-- ## To do -->

@@ -298,0 +312,0 @@

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

Sorry, the diff of this file is not supported yet

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