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 1.7.44 to 1.7.45

.github/ISSUE_TEMPLATE/bug_report.md

62

build/AsYouType.js

@@ -93,4 +93,6 @@ "use strict";

var MIN_LEADING_DIGITS_LENGTH = 3;
var VALID_FORMATTED_PHONE_NUMBER_PART = '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']*';
var VALID_FORMATTED_PHONE_NUMBER_PART = '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']+';
var VALID_FORMATTED_PHONE_NUMBER_PART_PATTERN = new RegExp('^' + VALID_FORMATTED_PHONE_NUMBER_PART + '$', 'i');
var VALID_PHONE_NUMBER = '(?:' + '[' + _constants.PLUS_CHARS + ']' + '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']*' + '|' + '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']+' + ')';
var AFTER_PHONE_NUMBER_DIGITS_END_PATTERN = new RegExp('[^' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']+' + '.*' + '$');
var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;

@@ -159,3 +161,2 @@

this.setCountry(this.defaultCountry, this.defaultCallingCode);
this.resetFormat();
return this;

@@ -174,2 +175,4 @@ }

}
this.resetFormat();
}

@@ -213,12 +216,4 @@ }, {

// Extract a formatted phone number part from text.
var extractedNumber = (0, _parse_.extractFormattedPhoneNumber)(text) || ''; // Special case for a lone '+' sign
// because it's not extracted in such cases.
var extractedNumber = extractFormattedPhoneNumber(text) || ''; // Trim a `+`.
if (!extractedNumber) {
if (text && text.indexOf('+') >= 0) {
extractedNumber = '+';
}
} // Trim a `+`.
if (extractedNumber[0] === '+') {

@@ -228,5 +223,6 @@ // Trim the `+`.

if (this.digits || this.isInternational()) {// If an out of position `+` is detected
if (this.digits) {// If an out of position `+` is detected
// (or a second `+`) then just ignore it.
} else {
this.formattedOutput = '+';
this.startInternationalNumber();

@@ -293,6 +289,4 @@ }

// There must be some digits in order to extract anything from them.
if (!this.digits) {
// Don't format the phone number.
return;
} // If one looks at country phone codes
//
// If one looks at country phone codes
// then they can notice that no one country phone code

@@ -306,4 +300,2 @@ // is ever a (leftmost) substring of another country phone code.

//
if (!this.extractCountryCallingCode()) {

@@ -1198,2 +1190,36 @@ // Don't format the phone number.

}
/**
* Extracts formatted phone number from text (if there's any).
* @param {string} text
* @return {string} [formattedPhoneNumber]
*/
function extractFormattedPhoneNumber(text) {
// Attempt to extract a possible number from the string passed in.
var startsAt = text.search(VALID_PHONE_NUMBER);
if (startsAt < 0) {
return;
} // Trim everything to the left of the phone number.
text = text.slice(startsAt); // Trim the `+`.
var hasPlus;
if (text[0] === '+') {
hasPlus = true;
text = text.slice('+'.length);
} // Trim everything to the right of the phone number.
text = text.replace(AFTER_PHONE_NUMBER_DIGITS_END_PATTERN, ''); // Re-add the previously trimmed `+`.
if (hasPlus) {
text = '+' + text;
}
return text;
}
//# sourceMappingURL=AsYouType.js.map

@@ -57,2 +57,42 @@ "use strict";

});
it('should populate international number template (digit by digit) (default country)', function () {
var formatter = new AsYouType('US');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('').should.equal('');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('+').should.equal('+');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('1').should.equal('+1');
formatter.template.should.equal('xx xxx xxx xxxx'); // Hasn't yet started formatting the phone number using the template.
formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx'); // Has some national number digits, starts formatting the phone number using the template.
formatter.input('213');
formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx');
formatter.input('3734253');
formatter.populatedNationalNumberTemplate.should.equal('213 373 4253');
});
it('should populate international number template (digit by digit)', function () {
var formatter = new AsYouType();
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('').should.equal('');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('+').should.equal('+');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('1').should.equal('+1');
formatter.template.should.equal('xx xxx xxx xxxx'); // Hasn't yet started formatting the phone number using the template.
formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx'); // Has some national number digits, starts formatting the phone number using the template.
formatter.input('213');
formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx');
formatter.input('3734253');
formatter.populatedNationalNumberTemplate.should.equal('213 373 4253');
});
it('should populate national number template (attempt to format complete number)', function () {

@@ -325,3 +365,3 @@ var formatter = new AsYouType('US');

formatter = new AsYouType('RU');
formatter.input('+1abc2').should.equal(''); // Should reset default country when explicitly
formatter.input('+1abc2').should.equal('+1'); // Should reset default country when explicitly
// typing in an international phone number

@@ -353,3 +393,3 @@

it('should not accept phone number extensions', function () {
new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('');
new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('+1 213 373 4253');
});

@@ -356,0 +396,0 @@ it('should parse non-European digits', function () {

@@ -197,2 +197,3 @@ "use strict";

* @param {string} text
* @param {boolean} throwOnError — By default, it won't throw if the text is too long.
* @return {string}

@@ -205,3 +206,3 @@ * @example

function extractFormattedPhoneNumber(text, v2) {
function extractFormattedPhoneNumber(text, throwOnError) {
if (!text) {

@@ -212,3 +213,3 @@ return;

if (text.length > MAX_INPUT_STRING_LENGTH) {
if (v2) {
if (throwOnError) {
throw new _ParseError["default"]('TOO_LONG');

@@ -215,0 +216,0 @@ }

@@ -24,3 +24,3 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import { matchesEntirely } from './util';
import { extractCountryCallingCode as _extractCountryCallingCode, extractFormattedPhoneNumber, findCountryCode, stripNationalPrefixAndCarrierCode, stripNationalPrefixAndCarrierCodeFromCompleteNumber, extractCountryCallingCodeFromInternationalNumberWithoutPlusSign } from './parse_';
import { extractCountryCallingCode as _extractCountryCallingCode, findCountryCode, stripNationalPrefixAndCarrierCode, stripNationalPrefixAndCarrierCodeFromCompleteNumber, extractCountryCallingCodeFromInternationalNumberWithoutPlusSign } from './parse_';
import { FIRST_GROUP_PATTERN, formatNationalNumberUsingFormat, applyInternationalSeparatorStyle } from './format_';

@@ -79,4 +79,6 @@ import { stripIDDPrefix } from './IDD';

var MIN_LEADING_DIGITS_LENGTH = 3;
var VALID_FORMATTED_PHONE_NUMBER_PART = '[' + VALID_PUNCTUATION + VALID_DIGITS + ']*';
var VALID_FORMATTED_PHONE_NUMBER_PART = '[' + VALID_PUNCTUATION + VALID_DIGITS + ']+';
var VALID_FORMATTED_PHONE_NUMBER_PART_PATTERN = new RegExp('^' + VALID_FORMATTED_PHONE_NUMBER_PART + '$', 'i');
var VALID_PHONE_NUMBER = '(?:' + '[' + PLUS_CHARS + ']' + '[' + VALID_PUNCTUATION + VALID_DIGITS + ']*' + '|' + '[' + VALID_PUNCTUATION + VALID_DIGITS + ']+' + ')';
var AFTER_PHONE_NUMBER_DIGITS_END_PATTERN = new RegExp('[^' + VALID_PUNCTUATION + VALID_DIGITS + ']+' + '.*' + '$');
var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;

@@ -145,3 +147,2 @@

this.setCountry(this.defaultCountry, this.defaultCallingCode);
this.resetFormat();
return this;

@@ -160,2 +161,4 @@ }

}
this.resetFormat();
}

@@ -199,12 +202,4 @@ }, {

// Extract a formatted phone number part from text.
var extractedNumber = extractFormattedPhoneNumber(text) || ''; // Special case for a lone '+' sign
// because it's not extracted in such cases.
var extractedNumber = extractFormattedPhoneNumber(text) || ''; // Trim a `+`.
if (!extractedNumber) {
if (text && text.indexOf('+') >= 0) {
extractedNumber = '+';
}
} // Trim a `+`.
if (extractedNumber[0] === '+') {

@@ -214,5 +209,6 @@ // Trim the `+`.

if (this.digits || this.isInternational()) {// If an out of position `+` is detected
if (this.digits) {// If an out of position `+` is detected
// (or a second `+`) then just ignore it.
} else {
this.formattedOutput = '+';
this.startInternationalNumber();

@@ -279,6 +275,4 @@ }

// There must be some digits in order to extract anything from them.
if (!this.digits) {
// Don't format the phone number.
return;
} // If one looks at country phone codes
//
// If one looks at country phone codes
// then they can notice that no one country phone code

@@ -292,4 +286,2 @@ // is ever a (leftmost) substring of another country phone code.

//
if (!this.extractCountryCallingCode()) {

@@ -1179,2 +1171,35 @@ // Don't format the phone number.

}
/**
* Extracts formatted phone number from text (if there's any).
* @param {string} text
* @return {string} [formattedPhoneNumber]
*/
function extractFormattedPhoneNumber(text) {
// Attempt to extract a possible number from the string passed in.
var startsAt = text.search(VALID_PHONE_NUMBER);
if (startsAt < 0) {
return;
} // Trim everything to the left of the phone number.
text = text.slice(startsAt); // Trim the `+`.
var hasPlus;
if (text[0] === '+') {
hasPlus = true;
text = text.slice('+'.length);
} // Trim everything to the right of the phone number.
text = text.replace(AFTER_PHONE_NUMBER_DIGITS_END_PATTERN, ''); // Re-add the previously trimmed `+`.
if (hasPlus) {
text = '+' + text;
}
return text;
}
//# sourceMappingURL=AsYouType.js.map

@@ -50,2 +50,42 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

});
it('should populate international number template (digit by digit) (default country)', function () {
var formatter = new AsYouType('US');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('').should.equal('');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('+').should.equal('+');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('1').should.equal('+1');
formatter.template.should.equal('xx xxx xxx xxxx'); // Hasn't yet started formatting the phone number using the template.
formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx'); // Has some national number digits, starts formatting the phone number using the template.
formatter.input('213');
formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx');
formatter.input('3734253');
formatter.populatedNationalNumberTemplate.should.equal('213 373 4253');
});
it('should populate international number template (digit by digit)', function () {
var formatter = new AsYouType();
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('').should.equal('');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('+').should.equal('+');
expect(formatter.template).to.be.undefined;
expect(formatter.populatedNationalNumberTemplate).to.be.undefined;
formatter.input('1').should.equal('+1');
formatter.template.should.equal('xx xxx xxx xxxx'); // Hasn't yet started formatting the phone number using the template.
formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx'); // Has some national number digits, starts formatting the phone number using the template.
formatter.input('213');
formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx');
formatter.input('3734253');
formatter.populatedNationalNumberTemplate.should.equal('213 373 4253');
});
it('should populate national number template (attempt to format complete number)', function () {

@@ -318,3 +358,3 @@ var formatter = new AsYouType('US');

formatter = new AsYouType('RU');
formatter.input('+1abc2').should.equal(''); // Should reset default country when explicitly
formatter.input('+1abc2').should.equal('+1'); // Should reset default country when explicitly
// typing in an international phone number

@@ -346,3 +386,3 @@

it('should not accept phone number extensions', function () {
new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('');
new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('+1 213 373 4253');
});

@@ -349,0 +389,0 @@ it('should parse non-European digits', function () {

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

// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js
import { VALID_DIGITS, VALID_PUNCTUATION, PLUS_CHARS, MIN_LENGTH_FOR_NSN, MAX_LENGTH_FOR_NSN, MAX_LENGTH_COUNTRY_CODE } from './constants';
import { VALID_DIGITS, PLUS_CHARS, MIN_LENGTH_FOR_NSN, MAX_LENGTH_FOR_NSN, MAX_LENGTH_COUNTRY_CODE } from './constants';
import { matchesEntirely } from './util';

@@ -168,2 +168,3 @@ import ParseError from './ParseError';

* @param {string} text
* @param {boolean} throwOnError — By default, it won't throw if the text is too long.
* @return {string}

@@ -175,3 +176,3 @@ * @example

export function extractFormattedPhoneNumber(text, v2) {
export function extractFormattedPhoneNumber(text, throwOnError) {
if (!text) {

@@ -182,3 +183,3 @@ return;

if (text.length > MAX_INPUT_STRING_LENGTH) {
if (v2) {
if (throwOnError) {
throw new ParseError('TOO_LONG');

@@ -185,0 +186,0 @@ }

{
"name": "libphonenumber-js",
"version": "1.7.44",
"version": "1.7.45",
"description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",

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

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

extractCountryCallingCode,
extractFormattedPhoneNumber,
findCountryCode,

@@ -104,6 +103,29 @@ stripNationalPrefixAndCarrierCode,

VALID_DIGITS +
']*'
']+'
const VALID_FORMATTED_PHONE_NUMBER_PART_PATTERN = new RegExp('^' + VALID_FORMATTED_PHONE_NUMBER_PART + '$', 'i')
const VALID_PHONE_NUMBER =
'(?:' +
'[' + PLUS_CHARS + ']' +
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']*' +
'|' +
'[' +
VALID_PUNCTUATION +
VALID_DIGITS +
']+' +
')'
const AFTER_PHONE_NUMBER_DIGITS_END_PATTERN = new RegExp(
'[^' +
VALID_PUNCTUATION +
VALID_DIGITS +
']+' +
'.*' +
'$'
)
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false

@@ -161,3 +183,2 @@

this.setCountry(this.defaultCountry, this.defaultCallingCode)
this.resetFormat()
return this

@@ -174,2 +195,3 @@ }

}
this.resetFormat()
}

@@ -211,9 +233,2 @@

let extractedNumber = extractFormattedPhoneNumber(text) || ''
// Special case for a lone '+' sign
// because it's not extracted in such cases.
if (!extractedNumber) {
if (text && text.indexOf('+') >= 0) {
extractedNumber = '+'
}
}
// Trim a `+`.

@@ -223,6 +238,7 @@ if (extractedNumber[0] === '+') {

extractedNumber = extractedNumber.slice('+'.length)
if (this.digits || this.isInternational()) {
if (this.digits) {
// If an out of position `+` is detected
// (or a second `+`) then just ignore it.
} else {
this.formattedOutput = '+'
this.startInternationalNumber()

@@ -288,6 +304,3 @@ }

// There must be some digits in order to extract anything from them.
if (!this.digits) {
// Don't format the phone number.
return
}
//
// If one looks at country phone codes

@@ -1112,2 +1125,30 @@ // then they can notice that no one country phone code

return result + string
}
/**
* Extracts formatted phone number from text (if there's any).
* @param {string} text
* @return {string} [formattedPhoneNumber]
*/
function extractFormattedPhoneNumber(text) {
// Attempt to extract a possible number from the string passed in.
const startsAt = text.search(VALID_PHONE_NUMBER)
if (startsAt < 0) {
return
}
// Trim everything to the left of the phone number.
text = text.slice(startsAt)
// Trim the `+`.
let hasPlus
if (text[0] === '+') {
hasPlus = true
text = text.slice('+'.length)
}
// Trim everything to the right of the phone number.
text = text.replace(AFTER_PHONE_NUMBER_DIGITS_END_PATTERN, '')
// Re-add the previously trimmed `+`.
if (hasPlus) {
text = '+' + text
}
return text
}

@@ -31,2 +31,44 @@ import metadata from '../metadata.min.json'

it('should populate international number template (digit by digit) (default country)', () => {
const formatter = new AsYouType('US')
expect(formatter.template).to.be.undefined
expect(formatter.populatedNationalNumberTemplate).to.be.undefined
formatter.input('').should.equal('')
expect(formatter.template).to.be.undefined
expect(formatter.populatedNationalNumberTemplate).to.be.undefined
formatter.input('+').should.equal('+')
expect(formatter.template).to.be.undefined
expect(formatter.populatedNationalNumberTemplate).to.be.undefined
formatter.input('1').should.equal('+1')
formatter.template.should.equal('xx xxx xxx xxxx')
// Hasn't yet started formatting the phone number using the template.
formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx')
// Has some national number digits, starts formatting the phone number using the template.
formatter.input('213')
formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx')
formatter.input('3734253')
formatter.populatedNationalNumberTemplate.should.equal('213 373 4253')
})
it('should populate international number template (digit by digit)', () => {
const formatter = new AsYouType()
expect(formatter.template).to.be.undefined
expect(formatter.populatedNationalNumberTemplate).to.be.undefined
formatter.input('').should.equal('')
expect(formatter.template).to.be.undefined
expect(formatter.populatedNationalNumberTemplate).to.be.undefined
formatter.input('+').should.equal('+')
expect(formatter.template).to.be.undefined
expect(formatter.populatedNationalNumberTemplate).to.be.undefined
formatter.input('1').should.equal('+1')
formatter.template.should.equal('xx xxx xxx xxxx')
// Hasn't yet started formatting the phone number using the template.
formatter.populatedNationalNumberTemplate.should.equal('xxx xxx xxxx')
// Has some national number digits, starts formatting the phone number using the template.
formatter.input('213')
formatter.populatedNationalNumberTemplate.should.equal('213 xxx xxxx')
formatter.input('3734253')
formatter.populatedNationalNumberTemplate.should.equal('213 373 4253')
})
it('should populate national number template (attempt to format complete number)', () => {

@@ -396,3 +438,3 @@ const formatter = new AsYouType('US')

formatter.input('+1abc2').should.equal('')
formatter.input('+1abc2').should.equal('+1')

@@ -433,3 +475,3 @@ // Should reset default country when explicitly

it('should not accept phone number extensions', () => {
new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('')
new AsYouType().input('+1-213-373-4253 ext. 123').should.equal('+1 213 373 4253')
})

@@ -436,0 +478,0 @@

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

VALID_DIGITS,
VALID_PUNCTUATION,
PLUS_CHARS,

@@ -189,2 +188,3 @@ MIN_LENGTH_FOR_NSN,

* @param {string} text
* @param {boolean} throwOnError — By default, it won't throw if the text is too long.
* @return {string}

@@ -195,3 +195,3 @@ * @example

*/
export function extractFormattedPhoneNumber(text, v2) {
export function extractFormattedPhoneNumber(text, throwOnError) {
if (!text) {

@@ -201,3 +201,3 @@ return

if (text.length > MAX_INPUT_STRING_LENGTH) {
if (v2) {
if (throwOnError) {
throw new ParseError('TOO_LONG')

@@ -204,0 +204,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 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 too big to display

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

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 too big to display

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

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

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

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