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.1.15 to 0.1.16

303

build/as you type.js

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

value: function input(text) {
this.valid = false;
// Parse input

@@ -117,48 +119,20 @@

// Feed the parsed input character-by-character
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var input = (0, _parse.parse_phone_number)(extracted_number);
try {
for (var _iterator = (0, _getIterator3.default)((0, _parse.parse_phone_number)(extracted_number)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var character = _step.value;
this.current_output = this.input_character(character);
// If an out of position '+' sign detected
// (or a second '+' sign),
// then just drop it from the input.
if (input[0] === '+') {
if (!this.parsed_input) {
this.parsed_input += '+';
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return this.current_output;
}
}, {
key: 'input_character',
value: function input_character(character) {
if (character === '+') {
// If an out of position '+' sign detected
// (or a second '+' sign),
// then just don't allow it being input.
if (this.parsed_input) {
return this.current_output;
}
input = input.slice(1);
}
// A digit then
else {
this.national_number += character;
}
this.parsed_input += character;
this.parsed_input += input;
// Add digits to the national number
this.national_number += input;
// Try to format the parsed input

@@ -173,21 +147,34 @@

// then it means that this is the country code.
if (this.extract_country_phone_code()) {
// If the possible phone number formats
// haven't been initialized during instance creation,
// then do it.
if (!this.default_country) {
this.initialize_phone_number_formats_for_this_country();
this.determine_the_country();
}
return '+' + this.country_phone_code;
// If no country phone code could be extracted so far,
// then just return the raw phone number.
if (!this.extract_country_phone_code()) {
// Return raw phone number
return this.parsed_input;
}
// Return raw phone number
return this.parsed_input;
// If the possible phone number formats
// haven't been initialized during instance creation,
// then do it now.
if (!this.default_country) {
this.initialize_phone_number_formats_for_this_country();
this.reset_format();
this.determine_the_country();
}
}
if (!this.country) {
this.determine_the_country();
}
// `this.country` could be `undefined`,
// for instance, when there is ambiguity
// in a form of several different countries
// each corresponding to the same country phone code
// (e.g. NANPA: USA, Canada, etc),
// and there's not enough digits entered
// to reliably determine the country
// the phone number belongs to.
// Therefore, in cases of such ambiguity,
// each time something is input,
// try to determine the country
// (if it's not determined yet).
else if (!this.country) {
this.determine_the_country();
}
} else {

@@ -209,9 +196,10 @@ // Some national prefixes are substrings of other national prefixes

// therefore reset all previous formatting data.
this.reset_formatting();
// (and leading digits matching state)
this.matching_formats = this.available_formats;
this.reset_format();
}
}
// Format the next phone number digit
// since the previously chose phone number format
// still holds.
// Format the next phone number digits
// using the previously chosen phone number format.
//

@@ -222,6 +210,9 @@ // This is done here because if `attempt_to_format_complete_phone_number`

//
var national_number_formatted_with_previous_format = this.format_next_national_number_digit(character);
var national_number_formatted_with_previous_format = void 0;
if (this.chosen_format) {
national_number_formatted_with_previous_format = this.format_next_national_number_digits(input);
}
// See if the input digits can be formatted properly already. If not,
// use the results from format_next_national_number_digit(), which does formatting
// use the results from format_next_national_number_digits(), which does formatting
// based on the formatting pattern chosen.

@@ -236,4 +227,2 @@

this.valid = false;
// Check if the previously chosen phone number format still holds

@@ -300,2 +289,3 @@ this.match_formats_by_leading_digits();

this.country_metadata = _metadata2.default.countries[this.default_country];
this.initialize_phone_number_formats_for_this_country();

@@ -305,19 +295,19 @@ } else {

this.country_metadata = undefined;
this.available_formats = [];
this.matching_formats = this.available_formats;
}
this.reset_formatting();
this.reset_format();
this.valid = false;
}
}, {
key: 'reset_formatting',
value: function reset_formatting() {
this.matching_formats = this.available_formats;
key: 'reset_format',
value: function reset_format() {
this.chosen_format = undefined;
this.template = undefined;
this.partially_populated_template = undefined;
this.last_match_position = 0;
this.last_match_position = -1;
this.national_prefix_is_part_of_formatting_template = false;
this.valid = false;
}

@@ -333,29 +323,3 @@

// using the selected phone number pattern.
var formatted_national_number = void 0;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(this.national_number), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var character = _step2.value;
formatted_national_number = this.format_next_national_number_digit(character);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return formatted_national_number;
return this.format_next_national_number_digits(this.national_number);
}

@@ -385,2 +349,4 @@ }, {

});
this.matching_formats = this.available_formats;
}

@@ -448,9 +414,9 @@ }, {

value: function attempt_to_format_complete_phone_number() {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator3 = (0, _getIterator3.default)(this.get_relevant_phone_number_formats()), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var format = _step3.value;
for (var _iterator = (0, _getIterator3.default)(this.get_relevant_phone_number_formats()), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var format = _step.value;

@@ -466,12 +432,12 @@ var matcher = new RegExp('^(?:' + (0, _metadata3.get_format_pattern)(format) + ')$');

} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
if (_didIteratorError) {
throw _iteratorError;
}

@@ -488,3 +454,9 @@ }

if (this.is_international()) {
return '+' + this.country_phone_code + ' ' + formatted_national_number;
var result = '+' + this.country_phone_code;
if (formatted_national_number) {
result += ' ' + formatted_national_number;
}
return result;
}

@@ -565,9 +537,9 @@

// format where a formatting template could be created.
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator4 = (0, _getIterator3.default)(this.get_relevant_phone_number_formats()), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var format = _step4.value;
for (var _iterator2 = (0, _getIterator3.default)(this.get_relevant_phone_number_formats()), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var format = _step2.value;

@@ -588,3 +560,3 @@ // If this format is currently being used

// using the old template needs to be reset.
this.last_match_position = 0;
this.last_match_position = -1;

@@ -597,12 +569,12 @@ return true;

} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
if (_didIteratorError2) {
throw _iteratorError2;
}

@@ -612,3 +584,3 @@ }

this.reset_formatting();
this.reset_format();
}

@@ -684,25 +656,50 @@ }, {

}, {
key: 'format_next_national_number_digit',
value: function format_next_national_number_digit(digit) {
// If there is room for more digits in current `template`,
// then set the next digit in the `template`,
// and return the formatted digits so far.
if (this.chosen_format && this.partially_populated_template.slice(this.last_match_position + 1).search(DIGIT_PLACEHOLDER_MATCHER) >= 0) {
var digit_pattern_start = this.partially_populated_template.search(DIGIT_PLACEHOLDER_MATCHER);
this.partially_populated_template = this.partially_populated_template.replace(DIGIT_PLACEHOLDER_MATCHER, digit);
this.last_match_position = digit_pattern_start;
key: 'format_next_national_number_digits',
value: function format_next_national_number_digits(digits) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = (0, _getIterator3.default)(digits), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var digit = _step3.value;
// If there is room for more digits in current `template`,
// then set the next digit in the `template`,
// and return the formatted digits so far.
// If more digits are entered than the current format could handle
if (this.partially_populated_template.slice(this.last_match_position + 1).search(DIGIT_PLACEHOLDER_MATCHER) === -1) {
// Reset the current format,
// so that the new format will be chosen
// in a subsequent `this.choose_another_format()` call
// later in code.
this.chosen_format = undefined;
this.template = undefined;
this.partially_populated_template = undefined;
return;
}
this.last_match_position = this.partially_populated_template.search(DIGIT_PLACEHOLDER_MATCHER);
this.partially_populated_template = this.partially_populated_template.replace(DIGIT_PLACEHOLDER_MATCHER, digit);
}
// Return the formatted phone number so far
return close_dangling_braces(this.partially_populated_template, digit_pattern_start + 1).replace(DIGIT_PLACEHOLDER_MATCHER_GLOBAL, ' ');
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
// More digits are entered than the current format could handle
// Reset the current format,
// so that the new format will be chosen
// in a subsequent `this.choose_another_format()` call
// later in code.
this.chosen_format = undefined;
this.template = undefined;
this.partially_populated_template = undefined;
return close_dangling_braces(this.partially_populated_template, this.last_match_position + 1).replace(DIGIT_PLACEHOLDER_MATCHER_GLOBAL, ' ');
}

@@ -764,9 +761,9 @@ }, {

var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator5 = (0, _getIterator3.default)(string), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var character = _step5.value;
for (var _iterator4 = (0, _getIterator3.default)(string), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var character = _step4.value;

@@ -778,12 +775,12 @@ if (character === symbol) {

} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
if (_didIteratorError4) {
throw _iteratorError4;
}

@@ -790,0 +787,0 @@ }

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

0.1.16 / 01.12.2016
===================
* "As you type" formatter now formats the whole input passed to the `.input()` function one at a time without splitting it into individual characters (which yields better performance)
0.1.14 / 01.12.2016

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

{
"name": "libphonenumber-js",
"version": "0.1.15",
"version": "0.1.16",
"description": "A simpler (and smaller) rewrite of Google Android's famous libphonenumber library",

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

@@ -157,5 +157,5 @@ # libphonenumber-js

## To do
<!-- ## To do
* Add an ability to input a value for "as you type" formatter not just character-by-character but as a whole (performance optimization)
Everything's done -->

@@ -162,0 +162,0 @@ ## Bug reporting

@@ -119,2 +119,4 @@ // This is an enhanced port of Google Android `libphonenumber`'s

{
this.valid = false
// Parse input

@@ -140,31 +142,22 @@

// Feed the parsed input character-by-character
for (let character of parse_phone_number(extracted_number))
{
this.current_output = this.input_character(character)
}
let input = parse_phone_number(extracted_number)
return this.current_output
}
input_character(character)
{
if (character === '+')
// If an out of position '+' sign detected
// (or a second '+' sign),
// then just drop it from the input.
if (input[0] === '+')
{
// If an out of position '+' sign detected
// (or a second '+' sign),
// then just don't allow it being input.
if (this.parsed_input)
if (!this.parsed_input)
{
return this.current_output
this.parsed_input += '+'
}
input = input.slice(1)
}
// A digit then
else
{
this.national_number += character
}
this.parsed_input += character
this.parsed_input += input
// Add digits to the national number
this.national_number += input
// Try to format the parsed input

@@ -181,21 +174,34 @@

// then it means that this is the country code.
if (this.extract_country_phone_code())
// If no country phone code could be extracted so far,
// then just return the raw phone number.
if (!this.extract_country_phone_code())
{
// If the possible phone number formats
// haven't been initialized during instance creation,
// then do it.
if (!this.default_country)
{
this.initialize_phone_number_formats_for_this_country()
this.determine_the_country()
}
// Return raw phone number
return this.parsed_input
}
return '+' + this.country_phone_code
// If the possible phone number formats
// haven't been initialized during instance creation,
// then do it now.
if (!this.default_country)
{
this.initialize_phone_number_formats_for_this_country()
this.reset_format()
this.determine_the_country()
}
// Return raw phone number
return this.parsed_input
}
if (!this.country)
// `this.country` could be `undefined`,
// for instance, when there is ambiguity
// in a form of several different countries
// each corresponding to the same country phone code
// (e.g. NANPA: USA, Canada, etc),
// and there's not enough digits entered
// to reliably determine the country
// the phone number belongs to.
// Therefore, in cases of such ambiguity,
// each time something is input,
// try to determine the country
// (if it's not determined yet).
else if (!this.country)
{

@@ -223,9 +229,10 @@ this.determine_the_country()

// therefore reset all previous formatting data.
this.reset_formatting()
// (and leading digits matching state)
this.matching_formats = this.available_formats
this.reset_format()
}
}
// Format the next phone number digit
// since the previously chose phone number format
// still holds.
// Format the next phone number digits
// using the previously chosen phone number format.
//

@@ -236,6 +243,10 @@ // This is done here because if `attempt_to_format_complete_phone_number`

//
const national_number_formatted_with_previous_format = this.format_next_national_number_digit(character)
let national_number_formatted_with_previous_format
if (this.chosen_format)
{
national_number_formatted_with_previous_format = this.format_next_national_number_digits(input)
}
// See if the input digits can be formatted properly already. If not,
// use the results from format_next_national_number_digit(), which does formatting
// use the results from format_next_national_number_digits(), which does formatting
// based on the formatting pattern chosen.

@@ -251,4 +262,2 @@

this.valid = false
// Check if the previously chosen phone number format still holds

@@ -319,2 +328,3 @@ this.match_formats_by_leading_digits()

this.country_metadata = metadata.countries[this.default_country]
this.initialize_phone_number_formats_for_this_country()

@@ -326,19 +336,19 @@ }

this.country_metadata = undefined
this.available_formats = []
this.matching_formats = this.available_formats
}
this.reset_formatting()
this.reset_format()
this.valid = false
}
reset_formatting()
reset_format()
{
this.matching_formats = this.available_formats
this.chosen_format = undefined
this.template = undefined
this.partially_populated_template = undefined
this.last_match_position = 0
this.last_match_position = -1
this.national_prefix_is_part_of_formatting_template = false
this.valid = false
}

@@ -352,9 +362,3 @@

// using the selected phone number pattern.
let formatted_national_number
for (let character of this.national_number)
{
formatted_national_number = this.format_next_national_number_digit(character)
}
return formatted_national_number
return this.format_next_national_number_digits(this.national_number)
}

@@ -390,2 +394,4 @@

})
this.matching_formats = this.available_formats
}

@@ -480,3 +486,10 @@

{
return '+' + this.country_phone_code + ' ' + formatted_national_number
let result = '+' + this.country_phone_code
if (formatted_national_number)
{
result += ' ' + formatted_national_number
}
return result
}

@@ -576,3 +589,3 @@

// using the old template needs to be reset.
this.last_match_position = 0
this.last_match_position = -1

@@ -584,3 +597,3 @@ return true

// No format matches the national phone number entered
this.reset_formatting()
this.reset_format()
}

@@ -661,27 +674,31 @@

format_next_national_number_digit(digit)
format_next_national_number_digits(digits)
{
// If there is room for more digits in current `template`,
// then set the next digit in the `template`,
// and return the formatted digits so far.
if (this.chosen_format && this.partially_populated_template.slice(this.last_match_position + 1).search(DIGIT_PLACEHOLDER_MATCHER) >= 0)
for (let digit of digits)
{
const digit_pattern_start = this.partially_populated_template.search(DIGIT_PLACEHOLDER_MATCHER)
// If there is room for more digits in current `template`,
// then set the next digit in the `template`,
// and return the formatted digits so far.
// If more digits are entered than the current format could handle
if (this.partially_populated_template.slice(this.last_match_position + 1).search(DIGIT_PLACEHOLDER_MATCHER) === -1)
{
// Reset the current format,
// so that the new format will be chosen
// in a subsequent `this.choose_another_format()` call
// later in code.
this.chosen_format = undefined
this.template = undefined
this.partially_populated_template = undefined
return
}
this.last_match_position = this.partially_populated_template.search(DIGIT_PLACEHOLDER_MATCHER)
this.partially_populated_template = this.partially_populated_template.replace(DIGIT_PLACEHOLDER_MATCHER, digit)
this.last_match_position = digit_pattern_start
// Return the formatted phone number so far
return close_dangling_braces(this.partially_populated_template, digit_pattern_start + 1)
.replace(DIGIT_PLACEHOLDER_MATCHER_GLOBAL, ' ')
}
// More digits are entered than the current format could handle
// Reset the current format,
// so that the new format will be chosen
// in a subsequent `this.choose_another_format()` call
// later in code.
this.chosen_format = undefined
this.template = undefined
this.partially_populated_template = undefined
// Return the formatted phone number so far
return close_dangling_braces(this.partially_populated_template, this.last_match_position + 1)
.replace(DIGIT_PLACEHOLDER_MATCHER_GLOBAL, ' ')
}

@@ -688,0 +705,0 @@

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