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.0.2 to 0.0.3

build/common.js

2

build/compress.js

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

// When changing this array also change getters in `./metadata.js`
var format_array = [format.pattern, format.format, format.leading_digits, format.national_prefix_formatting_rule, format.national_prefix_optional_when_formatting, format.intlFormat];
var format_array = [format.pattern, format.format, format.leading_digits, format.national_prefix_formatting_rule, format.national_prefix_optional_when_formatting, format.international_format];

@@ -38,0 +38,0 @@ trim_array(format_array);

@@ -1,7 +0,51 @@

"use strict";
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
exports.default = format;
exports.format_national_number = format_national_number;
exports.local_to_international_style = local_to_international_style;
var _common = require('./common');
var _metadata = require('../metadata.min');
var _metadata2 = _interopRequireDefault(_metadata);
var _metadata3 = require('./metadata');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function format(number, format, third_argument) {
// If the first argument object is expanded
if (typeof number === 'string') {
number = { phone: number, country: format };
format = third_argument;
}
var country_metadata = _metadata2.default.countries[number.country];
switch (format) {
case 'International':
var national_number = format_national_number(number.phone, 'International', country_metadata);
return '+' + (0, _metadata3.get_phone_code)(country_metadata) + ' ' + national_number;
case 'International_plaintext':
return '+' + (0, _metadata3.get_phone_code)(country_metadata) + number.phone;
case 'National':
return format_national_number(number.phone, 'National', country_metadata);
}
}
// This was originally set to $1 but there are some countries for which the
// first group is not used in the national pattern (e.g. Argentina) so the $1
// group does not match correctly. Therefore, we use \d, so that the first
// group actually used in the pattern will be matched.
// This is a port of Google Android `libphonenumber`'s

@@ -12,49 +56,78 @@ // `phonenumberutil.js` of 17th November, 2016.

function format(number) {
// if (number.getNationalNumber() == 0 && number.hasRawInput()) {
// // Unparseable numbers that kept their raw input just use that.
// // This is the only case where a number can be formatted as E164 without a
// // leading '+' symbol (but the original number wasn't parseable anyway).
// // TODO: Consider removing the 'if' above so that unparseable strings
// // without raw input format to the empty string instead of "+00"
// var rawInput = number.getRawInputOrDefault();
// if (rawInput.length > 0) {
// return rawInput;
// }
// }
// var countryCallingCode = number.getCountryCodeOrDefault()
// var nationalSignificantNumber = this.getNationalSignificantNumber(number)
// if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.E164)
// {
// // Early exit for E164 case (even if the country calling code is invalid)
// // since no formatting of the national number needs to be applied.
// // Extensions are not formatted.
// return this.prefixNumberWithCountryCallingCode_(
// countryCallingCode, i18n.phonenumbers.PhoneNumberFormat.E164,
// nationalSignificantNumber, '')
// }
// if (!this.hasValidCountryCallingCode_(countryCallingCode))
// {
// return nationalSignificantNumber
// }
// // Note getRegionCodeForCountryCode() is used because formatting information
// // for regions which share a country calling code is contained by only one
// // region for performance reasons. For example, for NANPA regions it will be
// // contained in the metadata for US.
// var regionCode = this.getRegionCodeForCountryCode(countryCallingCode)
var FIRST_GROUP_PATTERN = /(\$\d)/;
// // Metadata cannot be null because the country calling code is valid (which
// // means that the region code cannot be ZZ and must be one of our supported
// // region codes).
// var metadata = get_metadata_for_region_or_calling_code(countryCallingCode, regionCode)
// var formattedExtension = maybeGetFormattedExtension_(number, metadata, numberFormat)
// var formattedNationalNumber = formatNsn_(nationalSignificantNumber, metadata, numberFormat)
// return this.prefixNumberWithCountryCallingCode_(countryCallingCode,
// numberFormat,
// formattedNationalNumber,
// formattedExtension)
function format_national_number(number, format_as, country_metadata) {
var format = choose_format_for_number((0, _metadata3.get_formats)(country_metadata), number);
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
if (!format) {
return number;
}
var formatting_rule = (0, _metadata3.get_format_international_format)(format);
var pattern_to_match = new RegExp((0, _metadata3.get_format_pattern)(format));
var national_prefix_formatting_rule = (0, _metadata3.get_format_national_prefix_formatting_rule)(format, country_metadata);
if (format_as === 'National' && !(0, _metadata3.get_format_national_prefix_is_optional_when_formatting)(format, country_metadata) && national_prefix_formatting_rule) {
return number.replace(pattern_to_match, formatting_rule.replace(FIRST_GROUP_PATTERN, national_prefix_formatting_rule));
}
var formatted_number = number.replace(pattern_to_match, formatting_rule);
if (format_as === 'International') {
return local_to_international_style(formatted_number);
}
return formatted_number;
}
module.exports = exports['default'];
function choose_format_for_number(available_formats, national_number) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(available_formats), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _format = _step.value;
if ((0, _metadata3.get_format_leading_digits)(_format)) {
// The last leading_digits_pattern is used here, as it is the most detailed
var last_leading_digits_pattern = (0, _metadata3.get_format_leading_digits)(_format)[(0, _metadata3.get_format_leading_digits)(_format).length - 1];
if (national_number.search(last_leading_digits_pattern) !== 0) {
return;
}
}
if ((0, _common.matches_entirely)(new RegExp((0, _metadata3.get_format_pattern)(_format)), national_number)) {
return _format;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
// Removes brackets and replaces dashes with spaces.
//
// E.g. "(999) 111-22-33" -> "999 111 22 33"
//
function local_to_international_style(local) {
return local
// Remove brackets
.replace(/[\(\)]/g, '')
// Replace dashes with spaces
.replace(/\-/g, ' ').trim();
}
//# sourceMappingURL=format.js.map

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

var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _getIterator2 = require('babel-runtime/core-js/get-iterator');

@@ -27,3 +31,3 @@

try {
for (var _iterator = (0, _getIterator3.default)(xml.phoneNumberMetadata.territories[0].territory), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _loop = function _loop() {
var territory = _step.value;

@@ -113,3 +117,3 @@

//
national_prefix_formatting_rule: territory.$.nationalPrefixFormattingRule,
national_prefix_formatting_rule: national_prefix_formatting_rule(territory.$.nationalPrefixFormattingRule, territory.$.nationalPrefix),

@@ -128,3 +132,3 @@ // Is it possible that a national (significant)

// Check that national (significant) phone number pattern
// is set for this country
// is set for this country (no "default" value here)
if (!country.national_number_pattern) {

@@ -134,3 +138,4 @@ throw new Error('"generalDesc.nationalNumberPattern" is missing for country ' + country_code + ' metadata');

// Some countries don't have `availableFormats` specified
// Some countries don't have `availableFormats` specified,
// because those formats are inherited from the "main country for region".
if (territory.availableFormats) {

@@ -143,16 +148,36 @@ country.formats = territory.availableFormats[0].numberFormat.map(function (number_format) {

}) : undefined,
national_prefix_formatting_rule: number_format.$.nationalPrefixFormattingRule,
national_prefix_formatting_rule: national_prefix_formatting_rule(number_format.$.nationalPrefixFormattingRule, territory.$.nationalPrefix),
national_prefix_optional_when_formatting: number_format.$.nationalPrefixOptionalWhenFormatting,
format: number_format.format[0],
intlFormat: number_format.intlFormat && number_format.intlFormat[0] !== 'NA' ? number_format.intlFormat : undefined
international_format: number_format.intlFormat && number_format.intlFormat[0] !== 'NA' ? number_format.intlFormat : undefined
};
});
} else {
country.formats = [];
}
// If `national_prefix_for_parsing` is not set explicitly,
// then infer it from `national_prefix` (if any)
if (!country.national_prefix_for_parsing && country.national_prefix) {
country.national_prefix_for_parsing = country.national_prefix;
// Sanity check (using no "default" for this field)
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = (0, _getIterator3.default)(country.formats), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var format = _step3.value;
if (!format.format) {
throw new Error('No phone number format "format" supplied for pattern ' + format.pattern + ' for ' + country_code);
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}

@@ -184,3 +209,11 @@

}
};
for (var _iterator = (0, _getIterator3.default)(xml.phoneNumberMetadata.territories[0].territory), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
_loop();
}
// Some countries don't have `availableFormats` specified,
// because those formats are meant to be copied
// from the "main country for region".
} catch (err) {

@@ -201,2 +234,39 @@ _didIteratorError = true;

var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)((0, _keys2.default)(countries)), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _country_code = _step2.value;
var _country = countries[_country_code];
var main_country_for_region_code = country_phone_code_to_countries[_country.phone_code][0];
var main_country_for_region = countries[main_country_for_region_code];
_country.formats = main_country_for_region.formats;
// Some countries like Saint Helena and Falkland Islands
// ('AC', 'FK', 'KI', 'NU', 'SH', 'TA', ...)
// don't have any phone number formats
// and phone numbers are formatted as a block in those countries.
if (!_country.formats) {
_country.formats = [];
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return { countries: countries, country_phone_code_to_countries: country_phone_code_to_countries };

@@ -214,4 +284,12 @@ });

module.exports = exports['default'];
// Replaces $NP with national prefix and $FG with the first group ($1)
function national_prefix_formatting_rule(rule, national_prefix) {
if (!rule) {
return;
}
// Replace $NP with national prefix and $FG with the first group ($1)
return rule.replace('$NP', national_prefix).replace('$FG', '$1');
}
// Excessive fields from "PhoneNumberMetadata.xml"

@@ -260,2 +338,3 @@ // aren't included to reduce code complexity and size:

//
module.exports = exports['default'];
//# sourceMappingURL=generate.js.map

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

exports.get_format_national_prefix_formatting_rule = get_format_national_prefix_formatting_rule;
exports.get_format_national_prefix_optional_when_formatting = get_format_national_prefix_optional_when_formatting;
exports.get_format_national_prefix_is_optional_when_formatting = get_format_national_prefix_is_optional_when_formatting;
exports.get_format_international_format = get_format_international_format;

@@ -43,3 +43,11 @@ function get_phone_code(country_metadata) {

function get_national_prefix_for_parsing(country_metadata) {
return country_metadata[5];
var national_prefix_for_parsing = country_metadata[5];
// If `national_prefix_for_parsing` is not set explicitly,
// then infer it from `national_prefix` (if any)
if (!national_prefix_for_parsing) {
national_prefix_for_parsing = get_national_prefix(country_metadata);
}
return national_prefix_for_parsing;
}

@@ -71,13 +79,13 @@

function get_format_national_prefix_formatting_rule(format_array) {
return format_array[3];
function get_format_national_prefix_formatting_rule(format_array, country_metadata) {
return format_array[3] || get_national_prefix_formatting_rule(country_metadata);
}
function get_format_national_prefix_optional_when_formatting(format_array) {
return format_array[4];
function get_format_national_prefix_is_optional_when_formatting(format_array, country_metadata) {
return format_array[4] || get_national_prefix_is_optional_when_formatting(country_metadata);
}
function get_format_international_format(format_array) {
return format_array[5];
return format_array[5] || get_format_format(format_array);
}
//# sourceMappingURL=metadata.js.map

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

exports.replace_characters = replace_characters;
exports.matches_entirely = matches_entirely;
exports.extract_possible_number = extract_possible_number;

@@ -27,2 +26,4 @@ exports.extract_country_phone_code = extract_country_phone_code;

var _common = require('./common');
var _metadata = require('../metadata.min');

@@ -36,7 +37,2 @@

// This is a port of Google Android `libphonenumber`'s
// `phonenumberutil.js` of 17th November, 2016.
//
// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js
var default_options = {

@@ -60,2 +56,7 @@ country: {}

// Returns `{ country, number }`
// This is a port of Google Android `libphonenumber`'s
// `phonenumberutil.js` of 17th November, 2016.
//
// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js
function parse(text, options) {

@@ -141,3 +142,3 @@ if (typeof options === 'string') {

if (!matches_entirely(national_number_rule, national_number)) {
if (!(0, _common.matches_entirely)(national_number_rule, national_number)) {
return {};

@@ -269,13 +270,2 @@ }

// Checks whether the entire input sequence can be matched
// against the regular expression.
function matches_entirely(regular_expression, text) {
if (typeof regular_expression === 'string') {
regular_expression = '^(?:' + regular_expression + ')$';
}
var matched_groups = text.match(regular_expression);
return matched_groups && matched_groups[0].length === text.length;
}
// Attempts to extract a possible number from the string passed in.

@@ -388,3 +378,3 @@ function extract_possible_number(text) {

// If the original number was viable, and the resultant number is not, then return.
if (matches_entirely(national_number_rule, number) && !matches_entirely(national_number_rule, national_significant_number)) {
if ((0, _common.matches_entirely)(national_number_rule, number) && !(0, _common.matches_entirely)(national_number_rule, national_significant_number)) {
return number;

@@ -454,4 +444,4 @@ }

return matches_entirely((0, _metadata3.get_national_number_pattern)(country_metadata), national_number);
return (0, _common.matches_entirely)((0, _metadata3.get_national_number_pattern)(country_metadata), national_number);
}
//# sourceMappingURL=parse.js.map

@@ -0,4 +1,9 @@

0.0.3 / 24.11.2016
===================
* Added `format` function.
0.0.1 / 24.11.2016
===================
* Initial release. `parse`function is working.
* Initial release. `parse` function is working.
{
"name": "libphonenumber-js",
"version": "0.0.2",
"version": "0.0.3",
"description": "A simpler (and smaller) rewrite of Google Android's famous libphonenumber library",

@@ -5,0 +5,0 @@ "main": "index.common.js",

@@ -21,2 +21,4 @@ # libphonenumber-js

* Weighs less than 70 KiloBytes while `libphonenumber` bundle weighs about 220 KiloBytes
* When formatting international numbers replaces all braces, dashes, etc with spaces (because that's the logical thing to do, and leaving braces in an international number isn't)
* Doesn't parse alphabetic phone numbers like `1-800-GOT-MILK` as we don't use telephone sets in the XXIst century that much (and we have phonebooks in your mobile phones)

@@ -42,2 +44,4 @@ * Doesn't handle carrier codes: they're only used in Colombia and Brazil, and only when dialing within those countries from a mobile phone to a fixed line number (the locals surely already know those carrier codes by themselves)

parse('8 (800) 555 35 35', 'RU') === { country: 'RU', phone: '8005553535' }
format({ country: 'US', phone: '2133734253' }, 'International') === '+1-213-373-4253'
```

@@ -69,4 +73,23 @@

parse('+1-213-373-4253') === { country: 'US', phone: '2133734253' }
parse('(213) 373-4253', 'US') === { country: 'US', phone: '2133734253' }
```
### format(parsed_number, format)
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:
```js
format('2133734253', 'US', 'International') === '+1-213-373-4253'
format({ country: 'US', phone: '2133734253' }, 'International') === '+1-213-373-4253'
```
## To do
* Maybe exclude `national_prefix` from metadata due to it not being used (superseded by the relevant regular expressions)
## Contributing

@@ -73,0 +96,0 @@

@@ -25,3 +25,3 @@ export default function compress(input)

format.national_prefix_optional_when_formatting,
format.intlFormat
format.international_format
]

@@ -28,0 +28,0 @@

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

export default function format(number, options = {})
import { matches_entirely } from './common'
import metadata from '../metadata.min'
import
{
// if (number.getNationalNumber() == 0 && number.hasRawInput()) {
// // Unparseable numbers that kept their raw input just use that.
// // This is the only case where a number can be formatted as E164 without a
// // leading '+' symbol (but the original number wasn't parseable anyway).
// // TODO: Consider removing the 'if' above so that unparseable strings
// // without raw input format to the empty string instead of "+00"
// var rawInput = number.getRawInputOrDefault();
// if (rawInput.length > 0) {
// return rawInput;
// }
// }
// var countryCallingCode = number.getCountryCodeOrDefault()
// var nationalSignificantNumber = this.getNationalSignificantNumber(number)
// if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.E164)
// {
// // Early exit for E164 case (even if the country calling code is invalid)
// // since no formatting of the national number needs to be applied.
// // Extensions are not formatted.
// return this.prefixNumberWithCountryCallingCode_(
// countryCallingCode, i18n.phonenumbers.PhoneNumberFormat.E164,
// nationalSignificantNumber, '')
// }
// if (!this.hasValidCountryCallingCode_(countryCallingCode))
// {
// return nationalSignificantNumber
// }
// // Note getRegionCodeForCountryCode() is used because formatting information
// // for regions which share a country calling code is contained by only one
// // region for performance reasons. For example, for NANPA regions it will be
// // contained in the metadata for US.
// var regionCode = this.getRegionCodeForCountryCode(countryCallingCode)
get_phone_code,
get_formats,
get_format_pattern,
get_format_format,
get_format_leading_digits,
get_format_national_prefix_formatting_rule,
get_format_national_prefix_is_optional_when_formatting,
get_format_international_format
}
from './metadata'
// // Metadata cannot be null because the country calling code is valid (which
// // means that the region code cannot be ZZ and must be one of our supported
// // region codes).
// var metadata = get_metadata_for_region_or_calling_code(countryCallingCode, regionCode)
// var formattedExtension = maybeGetFormattedExtension_(number, metadata, numberFormat)
// var formattedNationalNumber = formatNsn_(nationalSignificantNumber, metadata, numberFormat)
// return this.prefixNumberWithCountryCallingCode_(countryCallingCode,
// numberFormat,
// formattedNationalNumber,
// formattedExtension)
export default function format(number, format, third_argument)
{
// If the first argument object is expanded
if (typeof number === 'string')
{
number = { phone: number, country: format }
format = third_argument
}
const country_metadata = metadata.countries[number.country]
switch (format)
{
case 'International':
const national_number = format_national_number(number.phone, 'International', country_metadata)
return `+${get_phone_code(country_metadata)} ${national_number}`
case 'International_plaintext':
return `+${get_phone_code(country_metadata)}${number.phone}`
case 'National':
return format_national_number(number.phone, 'National', country_metadata)
}
}
// This was originally set to $1 but there are some countries for which the
// first group is not used in the national pattern (e.g. Argentina) so the $1
// group does not match correctly. Therefore, we use \d, so that the first
// group actually used in the pattern will be matched.
const FIRST_GROUP_PATTERN = /(\$\d)/
export function format_national_number(number, format_as, country_metadata)
{
const format = choose_format_for_number(get_formats(country_metadata), number)
if (!format)
{
return number
}
const formatting_rule = get_format_international_format(format)
const pattern_to_match = new RegExp(get_format_pattern(format))
const national_prefix_formatting_rule = get_format_national_prefix_formatting_rule(format, country_metadata)
if (format_as === 'National' &&
!get_format_national_prefix_is_optional_when_formatting(format, country_metadata) &&
national_prefix_formatting_rule)
{
return number.replace(pattern_to_match,
formatting_rule.replace(FIRST_GROUP_PATTERN, national_prefix_formatting_rule))
}
const formatted_number = number.replace(pattern_to_match, formatting_rule)
if (format_as === 'International')
{
return local_to_international_style(formatted_number)
}
return formatted_number
}
function choose_format_for_number(available_formats, national_number)
{
for (let format of available_formats)
{
if (get_format_leading_digits(format))
{
// The last leading_digits_pattern is used here, as it is the most detailed
const last_leading_digits_pattern = get_format_leading_digits(format)[get_format_leading_digits(format).length - 1]
if (national_number.search(last_leading_digits_pattern) !== 0)
{
return
}
}
if (matches_entirely(new RegExp(get_format_pattern(format)), national_number))
{
return format
}
}
}
// Removes brackets and replaces dashes with spaces.
//
// E.g. "(999) 111-22-33" -> "999 111 22 33"
//
export function local_to_international_style(local)
{
return local
// Remove brackets
.replace(/[\(\)]/g, '')
// Replace dashes with spaces
.replace(/\-/g, ' ')
.trim()
}

@@ -144,3 +144,3 @@ import { parseString } from 'xml2js'

//
national_prefix_formatting_rule: territory.$.nationalPrefixFormattingRule,
national_prefix_formatting_rule: national_prefix_formatting_rule(territory.$.nationalPrefixFormattingRule, territory.$.nationalPrefix),

@@ -181,3 +181,3 @@ // Is it possible that a national (significant)

// Check that national (significant) phone number pattern
// is set for this country
// is set for this country (no "default" value here)
if (!country.national_number_pattern)

@@ -188,3 +188,4 @@ {

// Some countries don't have `availableFormats` specified
// Some countries don't have `availableFormats` specified,
// because those formats are inherited from the "main country for region".
if (territory.availableFormats)

@@ -196,18 +197,16 @@ {

leading_digits: number_format.leadingDigits ? number_format.leadingDigits.map(leading_digits => leading_digits.replace(/\s/g, '')) : undefined,
national_prefix_formatting_rule: number_format.$.nationalPrefixFormattingRule,
national_prefix_formatting_rule: national_prefix_formatting_rule(number_format.$.nationalPrefixFormattingRule, territory.$.nationalPrefix),
national_prefix_optional_when_formatting: number_format.$.nationalPrefixOptionalWhenFormatting,
format: number_format.format[0],
intlFormat: (number_format.intlFormat && number_format.intlFormat[0] !== 'NA') ? number_format.intlFormat : undefined
international_format: (number_format.intlFormat && number_format.intlFormat[0] !== 'NA') ? number_format.intlFormat : undefined
}))
}
else
{
country.formats = []
}
// If `national_prefix_for_parsing` is not set explicitly,
// then infer it from `national_prefix` (if any)
if (!country.national_prefix_for_parsing && country.national_prefix)
{
country.national_prefix_for_parsing = country.national_prefix
// Sanity check (using no "default" for this field)
for (let format of country.formats)
{
if (!format.format)
{
throw new Error(`No phone number format "format" supplied for pattern ${format.pattern} for ${country_code}`)
}
}
}

@@ -245,4 +244,39 @@

// Some countries don't have `availableFormats` specified,
// because those formats are meant to be copied
// from the "main country for region".
for (let country_code of Object.keys(countries))
{
const country = countries[country_code]
const main_country_for_region_code = country_phone_code_to_countries[country.phone_code][0]
const main_country_for_region = countries[main_country_for_region_code]
country.formats = main_country_for_region.formats
// Some countries like Saint Helena and Falkland Islands
// ('AC', 'FK', 'KI', 'NU', 'SH', 'TA', ...)
// don't have any phone number formats
// and phone numbers are formatted as a block in those countries.
if (!country.formats)
{
country.formats = []
}
}
return { countries, country_phone_code_to_countries }
})
}
// Replaces $NP with national prefix and $FG with the first group ($1)
function national_prefix_formatting_rule(rule, national_prefix)
{
if (!rule)
{
return
}
// Replace $NP with national prefix and $FG with the first group ($1)
return rule
.replace('$NP', national_prefix)
.replace('$FG', '$1')
}

@@ -28,3 +28,12 @@ export function get_phone_code(country_metadata)

{
return country_metadata[5]
let national_prefix_for_parsing = country_metadata[5]
// If `national_prefix_for_parsing` is not set explicitly,
// then infer it from `national_prefix` (if any)
if (!national_prefix_for_parsing)
{
national_prefix_for_parsing = get_national_prefix(country_metadata)
}
return national_prefix_for_parsing
}

@@ -62,10 +71,10 @@

export function get_format_national_prefix_formatting_rule(format_array)
export function get_format_national_prefix_formatting_rule(format_array, country_metadata)
{
return format_array[3]
return format_array[3] || get_national_prefix_formatting_rule(country_metadata)
}
export function get_format_national_prefix_optional_when_formatting(format_array)
export function get_format_national_prefix_is_optional_when_formatting(format_array, country_metadata)
{
return format_array[4]
return format_array[4] || get_national_prefix_is_optional_when_formatting(country_metadata)
}

@@ -75,3 +84,3 @@

{
return format_array[5]
return format_array[5] || get_format_format(format_array)
}

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

import { matches_entirely } from './common'
import metadata from '../metadata.min'

@@ -13,15 +14,5 @@

get_national_number_pattern,
get_formats,
get_national_prefix,
get_national_prefix_formatting_rule,
get_national_prefix_for_parsing,
get_national_prefix_transform_rule,
get_national_prefix_is_optional_when_formatting,
get_leading_digits,
get_format_pattern,
get_format_format,
get_format_leading_digits,
get_format_national_prefix_formatting_rule,
get_format_national_prefix_optional_when_formatting,
get_format_international_format
get_leading_digits
}

@@ -256,15 +247,2 @@ from './metadata'

// Checks whether the entire input sequence can be matched
// against the regular expression.
export function matches_entirely(regular_expression, text)
{
if (typeof regular_expression === 'string')
{
regular_expression = '^(?:' + regular_expression + ')$'
}
const matched_groups = text.match(regular_expression)
return matched_groups && matched_groups[0].length === text.length
}
// Attempts to extract a possible number from the string passed in.

@@ -271,0 +249,0 @@ export function extract_possible_number(text)

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 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