libphonenumber-js
Advanced tools
Comparing version 1.7.19 to 1.7.20
@@ -66,2 +66,3 @@ 'use strict'; | ||
// https://github.com/googlei18n/libphonenumber/commit/a395b4fef3caf57c4bc5f082e1152a4d2bd0ba4c | ||
// "We no longer have numbers in formatting matching patterns, only \d." | ||
// Because this library supports generating custom metadata | ||
@@ -68,0 +69,0 @@ // some users may still be using old metadata so the relevant |
@@ -61,3 +61,3 @@ 'use strict'; | ||
*/ | ||
STRICT_GROUPING: function STRICT_GROUPING(number, candidate, metadata) { | ||
STRICT_GROUPING: function STRICT_GROUPING(number, candidate, metadata, regExpCache) { | ||
var candidateString = candidate.toString(); | ||
@@ -69,3 +69,3 @@ | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsRemainGrouped); | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsRemainGrouped, regExpCache); | ||
}, | ||
@@ -85,3 +85,3 @@ | ||
*/ | ||
EXACT_GROUPING: function EXACT_GROUPING(number, candidate, metadata) { | ||
EXACT_GROUPING: function EXACT_GROUPING(number, candidate, metadata, regExpCache) { | ||
var candidateString = candidate.toString(); | ||
@@ -93,3 +93,3 @@ | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsAreExactlyPresent); | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsAreExactlyPresent, regExpCache); | ||
} | ||
@@ -197,5 +197,3 @@ }; | ||
function checkNumberGroupingIsValid(number, candidate, metadata, checkGroups) { | ||
// TODO: Evaluate how this works for other locales (testing has been limited to NANPA regions) | ||
// and optimise if necessary. | ||
function checkNumberGroupingIsValid(number, candidate, metadata, checkGroups, regExpCache) { | ||
var normalizedCandidate = normalizeDigits(candidate, true /* keep non-digits */); | ||
@@ -207,4 +205,5 @@ var formattedNumberGroups = getNationalNumberGroups(metadata, number, null); | ||
// If this didn't pass, see if there are any alternate formats, and try them instead. | ||
// If this didn't pass, see if there are any alternate formats that match, and try them instead. | ||
var alternateFormats = MetadataManager.getAlternateFormatsForCountry(number.getCountryCode()); | ||
var nationalSignificantNumber = util.getNationalSignificantNumber(number); | ||
@@ -226,4 +225,11 @@ if (alternateFormats) { | ||
if (alternateFormat.leadingDigitsPatterns().length > 0) { | ||
// There is only one leading digits pattern for alternate formats. | ||
var leadingDigitsRegExp = regExpCache.getPatternForRegExp('^' + alternateFormat.leadingDigitsPatterns()[0]); | ||
if (!leadingDigitsRegExp.test(nationalSignificantNumber)) { | ||
// Leading digits don't match; try another one. | ||
continue; | ||
} | ||
} | ||
formattedNumberGroups = getNationalNumberGroups(metadata, number, alternateFormat); | ||
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) { | ||
@@ -240,3 +246,4 @@ return true; | ||
* Helper method to get the national-number part of a number, formatted without any national | ||
* prefix, and return it as a set of digit blocks that would be formatted together. | ||
* prefix, and return it as a set of digit blocks that would be formatted together following | ||
* standard formatting rules. | ||
*/ | ||
@@ -250,3 +257,3 @@ function getNationalNumberGroups(metadata, number, formattingPattern) { | ||
// This will be in the format +CC-DG;ext=EXT where DG represents groups of digits. | ||
// This will be in the format +CC-DG1-DG2-DGX;ext=EXT where DG1..DGX represents groups of digits. | ||
var rfc3966Format = formatNumber(number, 'RFC3966', metadata); | ||
@@ -253,0 +260,0 @@ |
@@ -23,2 +23,6 @@ 'use strict'; | ||
var _RegExpCache = require('./findNumbers/RegExpCache'); | ||
var _RegExpCache2 = _interopRequireDefault(_RegExpCache); | ||
var _util = require('./findNumbers/util'); | ||
@@ -169,3 +173,4 @@ | ||
/** The iteration tristate. */ | ||
/** The next index to start searching at. Undefined in {@link State#DONE}. */ | ||
function PhoneNumberMatcher() { | ||
@@ -180,2 +185,3 @@ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
this.searchIndex = 0; | ||
this.regExpCache = new _RegExpCache2.default(32); | ||
@@ -222,5 +228,10 @@ options = _extends({}, options, { | ||
/** The next index to start searching at. Undefined in {@link State#DONE}. */ | ||
// A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3 | ||
// countries being used for the same doc with ~10 patterns for each country. Some pages will have | ||
// a lot more countries in use, but typically fewer numbers for each so expanding the cache for | ||
// that use-case won't have a lot of benefit. | ||
/** The iteration tristate. */ | ||
_createClass(PhoneNumberMatcher, [{ | ||
@@ -348,3 +359,3 @@ key: 'find', | ||
if (this.leniency(number, candidate, this.metadata)) { | ||
if (this.leniency(number, candidate, this.metadata, this.regExpCache)) { | ||
// // We used parseAndKeepRawInput to create this number, | ||
@@ -351,0 +362,0 @@ // // but for now we don't return the extra values parsed. |
@@ -47,2 +47,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
// https://github.com/googlei18n/libphonenumber/commit/a395b4fef3caf57c4bc5f082e1152a4d2bd0ba4c | ||
// "We no longer have numbers in formatting matching patterns, only \d." | ||
// Because this library supports generating custom metadata | ||
@@ -49,0 +50,0 @@ // some users may still be using old metadata so the relevant |
@@ -47,3 +47,3 @@ import isValidNumber from '../validate_'; | ||
*/ | ||
STRICT_GROUPING: function STRICT_GROUPING(number, candidate, metadata) { | ||
STRICT_GROUPING: function STRICT_GROUPING(number, candidate, metadata, regExpCache) { | ||
var candidateString = candidate.toString(); | ||
@@ -55,3 +55,3 @@ | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsRemainGrouped); | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsRemainGrouped, regExpCache); | ||
}, | ||
@@ -71,3 +71,3 @@ | ||
*/ | ||
EXACT_GROUPING: function EXACT_GROUPING(number, candidate, metadata) { | ||
EXACT_GROUPING: function EXACT_GROUPING(number, candidate, metadata, regExpCache) { | ||
var candidateString = candidate.toString(); | ||
@@ -79,3 +79,3 @@ | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsAreExactlyPresent); | ||
return checkNumberGroupingIsValid(number, candidate, metadata, allNumberGroupsAreExactlyPresent, regExpCache); | ||
} | ||
@@ -182,5 +182,3 @@ }; | ||
function checkNumberGroupingIsValid(number, candidate, metadata, checkGroups) { | ||
// TODO: Evaluate how this works for other locales (testing has been limited to NANPA regions) | ||
// and optimise if necessary. | ||
function checkNumberGroupingIsValid(number, candidate, metadata, checkGroups, regExpCache) { | ||
var normalizedCandidate = normalizeDigits(candidate, true /* keep non-digits */); | ||
@@ -192,4 +190,5 @@ var formattedNumberGroups = getNationalNumberGroups(metadata, number, null); | ||
// If this didn't pass, see if there are any alternate formats, and try them instead. | ||
// If this didn't pass, see if there are any alternate formats that match, and try them instead. | ||
var alternateFormats = MetadataManager.getAlternateFormatsForCountry(number.getCountryCode()); | ||
var nationalSignificantNumber = util.getNationalSignificantNumber(number); | ||
@@ -211,4 +210,11 @@ if (alternateFormats) { | ||
if (alternateFormat.leadingDigitsPatterns().length > 0) { | ||
// There is only one leading digits pattern for alternate formats. | ||
var leadingDigitsRegExp = regExpCache.getPatternForRegExp('^' + alternateFormat.leadingDigitsPatterns()[0]); | ||
if (!leadingDigitsRegExp.test(nationalSignificantNumber)) { | ||
// Leading digits don't match; try another one. | ||
continue; | ||
} | ||
} | ||
formattedNumberGroups = getNationalNumberGroups(metadata, number, alternateFormat); | ||
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) { | ||
@@ -225,3 +231,4 @@ return true; | ||
* Helper method to get the national-number part of a number, formatted without any national | ||
* prefix, and return it as a set of digit blocks that would be formatted together. | ||
* prefix, and return it as a set of digit blocks that would be formatted together following | ||
* standard formatting rules. | ||
*/ | ||
@@ -235,3 +242,3 @@ function getNationalNumberGroups(metadata, number, formattingPattern) { | ||
// This will be in the format +CC-DG;ext=EXT where DG represents groups of digits. | ||
// This will be in the format +CC-DG1-DG2-DGX;ext=EXT where DG1..DGX represents groups of digits. | ||
var rfc3966Format = formatNumber(number, 'RFC3966', metadata); | ||
@@ -238,0 +245,0 @@ |
@@ -19,2 +19,4 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
import RegExpCache from './findNumbers/RegExpCache'; | ||
import { limit, trimAfterFirstMatch } from './findNumbers/util'; | ||
@@ -148,3 +150,4 @@ | ||
/** The iteration tristate. */ | ||
/** The next index to start searching at. Undefined in {@link State#DONE}. */ | ||
function PhoneNumberMatcher() { | ||
@@ -159,2 +162,3 @@ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
this.searchIndex = 0; | ||
this.regExpCache = new RegExpCache(32); | ||
@@ -201,5 +205,10 @@ options = _extends({}, options, { | ||
/** The next index to start searching at. Undefined in {@link State#DONE}. */ | ||
// A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3 | ||
// countries being used for the same doc with ~10 patterns for each country. Some pages will have | ||
// a lot more countries in use, but typically fewer numbers for each so expanding the cache for | ||
// that use-case won't have a lot of benefit. | ||
/** The iteration tristate. */ | ||
_createClass(PhoneNumberMatcher, [{ | ||
@@ -327,3 +336,3 @@ key: 'find', | ||
if (this.leniency(number, candidate, this.metadata)) { | ||
if (this.leniency(number, candidate, this.metadata, this.regExpCache)) { | ||
// // We used parseAndKeepRawInput to create this number, | ||
@@ -330,0 +339,0 @@ // // but for now we don't return the extra values parsed. |
{ | ||
"name": "libphonenumber-js", | ||
"version": "1.7.19", | ||
"version": "1.7.20", | ||
"description": "A simpler (and smaller) rewrite of Google Android's popular libphonenumber library", | ||
@@ -50,4 +50,4 @@ "main": "index.common.js", | ||
"test": "mocha --require babel-core/register --colors --bail --reporter spec --require ./test/setup.js \"source/**/*.test.js\" \"test/**/*.test.js\" --recursive", | ||
"test-coverage": "istanbul cover -x \"build/**\" -x \"es6/**\" -x \"*.test.js\" -x \"source/findNumbers/Leniency.js\" -x \"source/PhoneNumberMatcher.js\" node_modules/mocha/bin/_mocha -- --require babel-core/register --colors --reporter dot --require ./test/setup.js \"source/**/*.test.js\" \"test/**/*.test.js\" --recursive", | ||
"test-travis": "istanbul cover -x \"build/**\" -x \"es6/**\" -x \"*.test.js\" -x \"source/findNumbers/Leniency.js\" -x \"source/PhoneNumberMatcher.js\" node_modules/mocha/bin/_mocha --report lcovonly -- --require babel-core/register --colors --reporter spec --require ./test/setup.js \"source/**/*.test.js\" \"test/**/*.test.js\" --recursive", | ||
"test-coverage": "istanbul cover -x \"build/**\" -x \"es6/**\" -x \"*.test.js\" -x \"source/findNumbers/Leniency.js\" -x \"source/findNumbers/RegExpCache.js\" -x \"source/findNumbers/LRUCache.js\" -x \"source/PhoneNumberMatcher.js\" -x \"source/tools/semver-compare.js\" node_modules/mocha/bin/_mocha -- --require babel-core/register --colors --reporter dot --require ./test/setup.js \"source/**/*.test.js\" \"test/**/*.test.js\" --recursive", | ||
"test-travis": "istanbul cover -x \"build/**\" -x \"es6/**\" -x \"*.test.js\" -x \"source/findNumbers/Leniency.js\" -x \"source/findNumbers/RegExpCache.js\" -x \"source/findNumbers/LRUCache.js\" -x \"source/PhoneNumberMatcher.js\" -x \"source/tools/semver-compare.js\" node_modules/mocha/bin/_mocha --report lcovonly -- --require babel-core/register --colors --reporter spec --require ./test/setup.js \"source/**/*.test.js\" \"test/**/*.test.js\" --recursive", | ||
"build:browser:production": "npm-run-all build:browser:production:legacy build:browser:production:min build:browser:production:max build:browser:production:mobile", | ||
@@ -54,0 +54,0 @@ "build:browser:production:legacy": "webpack --mode production --progress --colors", |
@@ -1349,3 +1349,3 @@ # libphonenumber-js | ||
Google periodically releases new metadata with the changes described in the [release notes](https://github.com/googlei18n/libphonenumber/blob/master/release_notes.txt). Sometimes those are minor non-breaking updates, sometimes those are major-version breaking updates. The metadata should be periodically updated via `autoupdate.cmd` (Windows) and `autoupdate.sh` (Linux/macOS) scripts. Also Google sometimes (very rarely) updates their code: [`phonenumberutil.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js) (`parseNumber()`, `formatNumber()`, `isValidNumber()`, `getNumberType()`), [`asyoutypeformatter.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/asyoutypeformatter.js) (`AsYouType`), [`PhoneNumberMatcher`](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java) (`findNumbers()`). The latest sync-up was performed on October 18th, 2018. | ||
Google periodically releases new metadata with the changes described in the [release notes](https://github.com/googlei18n/libphonenumber/blob/master/release_notes.txt). Sometimes those are minor non-breaking updates, sometimes those are major-version breaking updates. The metadata should be periodically updated via `autoupdate.cmd` (Windows) and `autoupdate.sh` (Linux/macOS) scripts. Also Google sometimes (very rarely) updates their code: [`phonenumberutil.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js) (`parseNumber()`, `formatNumber()`, `isValidNumber()`, `getNumberType()`), [`asyoutypeformatter.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/asyoutypeformatter.js) (`AsYouType`), [`PhoneNumberMatcher`](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java) (`findNumbers()`). The latest sync-up was performed on June 12th, 2019. | ||
@@ -1352,0 +1352,0 @@ ## Contributing |
@@ -56,2 +56,3 @@ // This is an enhanced port of Google Android `libphonenumber`'s | ||
// https://github.com/googlei18n/libphonenumber/commit/a395b4fef3caf57c4bc5f082e1152a4d2bd0ba4c | ||
// "We no longer have numbers in formatting matching patterns, only \d." | ||
// Because this library supports generating custom metadata | ||
@@ -58,0 +59,0 @@ // some users may still be using old metadata so the relevant |
@@ -53,3 +53,3 @@ import isValidNumber from '../validate_' | ||
*/ | ||
STRICT_GROUPING(number, candidate, metadata) | ||
STRICT_GROUPING(number, candidate, metadata, regExpCache) | ||
{ | ||
@@ -71,3 +71,4 @@ const candidateString = candidate.toString() | ||
metadata, | ||
allNumberGroupsRemainGrouped | ||
allNumberGroupsRemainGrouped, | ||
regExpCache | ||
) | ||
@@ -87,3 +88,3 @@ }, | ||
*/ | ||
EXACT_GROUPING(number, candidate, metadata) | ||
EXACT_GROUPING(number, candidate, metadata, regExpCache) | ||
{ | ||
@@ -105,3 +106,4 @@ const candidateString = candidate.toString() | ||
metadata, | ||
allNumberGroupsAreExactlyPresent | ||
allNumberGroupsAreExactlyPresent, | ||
regExpCache | ||
) | ||
@@ -231,28 +233,30 @@ } | ||
function checkNumberGroupingIsValid | ||
( | ||
function checkNumberGroupingIsValid( | ||
number, | ||
candidate, | ||
metadata, | ||
checkGroups | ||
) | ||
{ | ||
// TODO: Evaluate how this works for other locales (testing has been limited to NANPA regions) | ||
// and optimise if necessary. | ||
checkGroups, | ||
regExpCache | ||
) { | ||
const normalizedCandidate = normalizeDigits(candidate, true /* keep non-digits */) | ||
let formattedNumberGroups = getNationalNumberGroups(metadata, number, null) | ||
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) | ||
{ | ||
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) { | ||
return true | ||
} | ||
// If this didn't pass, see if there are any alternate formats, and try them instead. | ||
// If this didn't pass, see if there are any alternate formats that match, and try them instead. | ||
const alternateFormats = MetadataManager.getAlternateFormatsForCountry(number.getCountryCode()) | ||
const nationalSignificantNumber = util.getNationalSignificantNumber(number) | ||
if (alternateFormats) | ||
{ | ||
for (const alternateFormat of alternateFormats.numberFormats()) | ||
{ | ||
if (alternateFormats) { | ||
for (const alternateFormat of alternateFormats.numberFormats()) { | ||
if (alternateFormat.leadingDigitsPatterns().length > 0) { | ||
// There is only one leading digits pattern for alternate formats. | ||
const leadingDigitsRegExp = regExpCache.getPatternForRegExp('^' + alternateFormat.leadingDigitsPatterns()[0]) | ||
if (!leadingDigitsRegExp.test(nationalSignificantNumber)) { | ||
// Leading digits don't match; try another one. | ||
continue | ||
} | ||
} | ||
formattedNumberGroups = getNationalNumberGroups(metadata, number, alternateFormat) | ||
if (checkGroups(metadata, number, normalizedCandidate, formattedNumberGroups)) { | ||
@@ -269,13 +273,11 @@ return true | ||
* Helper method to get the national-number part of a number, formatted without any national | ||
* prefix, and return it as a set of digit blocks that would be formatted together. | ||
* prefix, and return it as a set of digit blocks that would be formatted together following | ||
* standard formatting rules. | ||
*/ | ||
function getNationalNumberGroups | ||
( | ||
function getNationalNumberGroups( | ||
metadata, | ||
number, | ||
formattingPattern | ||
) | ||
{ | ||
if (formattingPattern) | ||
{ | ||
) { | ||
if (formattingPattern) { | ||
// We format the NSN only, and split that according to the separator. | ||
@@ -287,3 +289,3 @@ const nationalSignificantNumber = util.getNationalSignificantNumber(number) | ||
// This will be in the format +CC-DG;ext=EXT where DG represents groups of digits. | ||
// This will be in the format +CC-DG1-DG2-DGX;ext=EXT where DG1..DGX represents groups of digits. | ||
const rfc3966Format = formatNumber(number, 'RFC3966', metadata) | ||
@@ -290,0 +292,0 @@ |
@@ -17,2 +17,4 @@ /** | ||
import RegExpCache from './findNumbers/RegExpCache' | ||
import { | ||
@@ -148,2 +150,8 @@ limit, | ||
// A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3 | ||
// countries being used for the same doc with ~10 patterns for each country. Some pages will have | ||
// a lot more countries in use, but typically fewer numbers for each so expanding the cache for | ||
// that use-case won't have a lot of benefit. | ||
regExpCache = new RegExpCache(32) | ||
/** | ||
@@ -325,3 +333,3 @@ * Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a | ||
if (this.leniency(number, candidate, this.metadata)) | ||
if (this.leniency(number, candidate, this.metadata, this.regExpCache)) | ||
{ | ||
@@ -328,0 +336,0 @@ // // We used parseAndKeepRawInput to create this number, |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4647869
330
23331