Skip to content
Commits on Source (45)
......@@ -42,4 +42,8 @@ npm-debug.log
/examples.mobile.json.js
# code coverage
.nyc_output
\ No newline at end of file
.nyc_output
*.d.cts
/max/index.d.ts
/mobile/index.d.ts
\ No newline at end of file
This diff is collapsed.
......@@ -1120,12 +1120,25 @@ parseIncompletePhoneNumber('+7 800 555') === '+7800555'
parseIncompletePhoneNumber('+٤٤٢٣٢٣٢٣٤') === '+442323234'
```
### formatIncompletePhoneNumber(value: string, country: string?): string
### formatIncompletePhoneNumber(value: string, defaultCountry?: string | options?: object): string
Formats incomplete phone number as a national one for a given `country`. If `country` is not specified then outputs the phone number in international format. This is just an alias for `new AsYouType(country, metadata).input(value)`. Can be used for building a phone number input component (e.g. [react-phone-number-input](https://gitlab.com/catamphetamine/react-phone-number-input/)).
Formats a possibly incomplete phone number.
While the usual `parsePhoneNumber(string).format()` function could only be used to format a complete phone number, this function could be used to format a possibly incomplete phone number.
The `value` argument should be a (possibly incomplete) phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format.
For the description of the `defaultCountry?: string | options?: object` argument, see [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function description.
This function is just an alias for `new AsYouType(defaultCountry, metadata).input(value)`. It can be used for building a phone number input component (e.g. [react-phone-number-input](https://gitlab.com/catamphetamine/react-phone-number-input/)).
```js
// National numbers, with second argument.
formatIncompletePhoneNumber('8800555', 'RU') === '8 (800) 555'
formatIncompletePhoneNumber('8800555', { defaultCountry: 'RU' }) === '8 (800) 555'
formatIncompletePhoneNumber('8800555', { defaultCallingCode: '7' }) === '8 (800) 555'
// International numbers, without second argument.
formatIncompletePhoneNumber('+7800555') === '+7 800 555'
```
......
......@@ -40,10 +40,10 @@ export {
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
export function parsePhoneNumber(text: string, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry: CountryCode, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumberWithError(text: string, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry: CountryCode, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }, metadata: MetadataJson): PhoneNumber;
// `parsePhoneNumberFromString()` named export is now considered legacy:
// it has been promoted to a default export due to being too verbose.
......@@ -88,7 +88,7 @@ export function getExtPrefix(countryCode: CountryCode, metadata: MetadataJson):
export function getExampleNumber(country: CountryCode, examples: Examples, metadata: MetadataJson): PhoneNumber | undefined;
export function formatIncompletePhoneNumber(number: string, metadata: MetadataJson): string;
export function formatIncompletePhoneNumber(number: string, countryCode: CountryCode, metadata: MetadataJson): string;
export function formatIncompletePhoneNumber(number: string, defaultCountryCode: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string } | undefined, metadata: MetadataJson): string;
export function parseIncompletePhoneNumber(text: string): string;
export function parsePhoneNumberCharacter(character: string): string;
export function parseDigits(character: string): string;
......
......@@ -6,7 +6,6 @@
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
}
......
......@@ -48,10 +48,10 @@ export {
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
export function parsePhoneNumber(text: string, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry: CountryCode, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumberWithError(text: string, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry: CountryCode, metadata: MetadataJson): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }, metadata: MetadataJson): PhoneNumber;
// `parsePhoneNumberFromString()` named export is now considered legacy:
// it has been promoted to a default export due to being too verbose.
......@@ -140,7 +140,7 @@ export function getExtPrefix(countryCode: CountryCode, metadata: MetadataJson):
export function isSupportedCountry(countryCode: CountryCode, metadata: MetadataJson): boolean;
export function formatIncompletePhoneNumber(number: string, metadata: MetadataJson): string;
export function formatIncompletePhoneNumber(number: string, countryCode: CountryCode, metadata: MetadataJson): string;
export function formatIncompletePhoneNumber(number: string, defaultCountryCode: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string } | undefined, metadata: MetadataJson): string;
export function parseIncompletePhoneNumber(text: string): string;
export function parsePhoneNumberCharacter(character: string): string;
export function parseDigits(character: string): string;
......
......@@ -66,8 +66,8 @@ export interface ParsedNumber {
}
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry?: CountryCode): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
// `parsePhoneNumberFromString()` named export is now considered legacy:
// it has been promoted to a default export due to being too verbose.
......@@ -139,9 +139,10 @@ export function getCountryCallingCode(countryCode: CountryCode): CountryCallingC
// Deprecated.
export function getPhoneCode(countryCode: CountryCode): CountryCallingCode;
export function getExtPrefix(countryCode: CountryCode): string;
export function isSupportedCountry(countryCode: string): boolean;
export function isSupportedCountry(countryCode: string): countryCode is CountryCode;
export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
export function parseIncompletePhoneNumber(text: string): string;
export function parsePhoneNumberCharacter(character: string): string;
export function parseDigits(character: string): string;
......
......@@ -3,11 +3,9 @@
"name": "libphonenumber-js/max",
"main": "index.cjs",
"module": "index.js",
"types": "../min/index.d.ts",
"type": "module",
"exports": {
".": {
"types": "../min/index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
}
......
......@@ -39,9 +39,9 @@ export {
};
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry?: CountryCode): PhoneNumber;
export function parsePhoneNumberWithError(text: string, defaultCountry?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string, extract?: boolean }): PhoneNumber;
// `parsePhoneNumberFromString()` named export is now considered legacy:
// it has been promoted to a default export due to being too verbose.
......@@ -68,14 +68,14 @@ export class PhoneNumberMatcher {
next(): NumberFound | undefined;
}
export function isSupportedCountry(countryCode: CountryCode): boolean;
export function isSupportedCountry(countryCode: string): countryCode is CountryCode;
export function getCountries(): CountryCode[];
export function getCountryCallingCode(countryCode: CountryCode): CountryCallingCode;
export function getExtPrefix(countryCode: CountryCode): string;
export function getExampleNumber(country: CountryCode, examples: Examples): PhoneNumber | undefined;
export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
export function formatIncompletePhoneNumber(number: string, defaultCountryCode?: CountryCode | { defaultCountry?: CountryCode, defaultCallingCode?: string }): string;
export function parseIncompletePhoneNumber(text: string): string;
export function parsePhoneNumberCharacter(character: string): string;
export function parseDigits(character: string): string;
......
......@@ -6,7 +6,6 @@
"type": "module",
"exports": {
".": {
"types": "../min/index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
}
......
......@@ -3,11 +3,9 @@
"name": "libphonenumber-js/mobile",
"main": "index.cjs",
"module": "index.js",
"types": "../min/index.d.ts",
"type": "module",
"exports": {
".": {
"types": "../min/index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
}
......
This diff is collapsed.
{
"name": "libphonenumber-js",
"version": "1.10.30",
"version": "1.10.49",
"description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",
"main": "index.cjs",
"module": "index.js",
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
},
"./min": {
"types": "./min/index.d.ts",
"import": "./min/index.js",
"require": "./min/index.cjs"
},
"./max": {
"types": "./min/index.d.ts",
"import": "./max/index.js",
"require": "./max/index.cjs"
},
"./mobile": {
"types": "./min/index.d.ts",
"import": "./mobile/index.js",
"require": "./mobile/index.cjs"
},
"./core": {
"types": "./core/index.d.ts",
"import": "./core/index.js",
"require": "./core/index.cjs"
},
"./min/metadata": {
"types": "./metadata.min.json.d.ts",
"import": "./metadata.min.json.js",
"require": "./metadata.min.json"
},
"./metadata.min": {
"types": "./metadata.min.json.d.ts",
"import": "./metadata.min.json.js",
"require": "./metadata.min.json"
},
"./metadata.min.json": {
"types": "./metadata.min.json.d.ts",
"import": "./metadata.min.json.js",
"require": "./metadata.min.json"
},
"./metadata.full": {
"types": "./metadata.max.json.d.ts",
"import": "./metadata.max.json.js",
"require": "./metadata.max.json"
},
"./metadata.full.json": {
"types": "./metadata.max.json.d.ts",
"import": "./metadata.max.json.js",
"require": "./metadata.max.json"
},
"./max/metadata": {
"types": "./metadata.max.json.d.ts",
"import": "./metadata.max.json.js",
"require": "./metadata.max.json"
},
"./metadata.max": {
"types": "./metadata.max.json.d.ts",
"import": "./metadata.max.json.js",
"require": "./metadata.max.json"
},
"./metadata.max.json": {
"types": "./metadata.max.json.d.ts",
"import": "./metadata.max.json.js",
"require": "./metadata.max.json"
},
"./mobile/metadata": {
"types": "./metadata.mobile.json.d.ts",
"import": "./metadata.mobile.json.js",
"require": "./metadata.mobile.json"
},
"./metadata.mobile": {
"types": "./metadata.mobile.json.d.ts",
"import": "./metadata.mobile.json.js",
"require": "./metadata.mobile.json"
},
"./metadata.mobile.json": {
"types": "./metadata.mobile.json.d.ts",
"import": "./metadata.mobile.json.js",
"require": "./metadata.mobile.json"
},
"./mobile/examples": {
"types": "./examples.mobile.json.d.ts",
"import": "./examples.mobile.json.js",
"require": "./examples.mobile.json"
},
"./examples.mobile": {
"types": "./examples.mobile.json.d.ts",
"import": "./examples.mobile.json.js",
"require": "./examples.mobile.json"
},
"./examples.mobile.json": {
"types": "./examples.mobile.json.d.ts",
"import": "./examples.mobile.json.js",
"require": "./examples.mobile.json"
},
......@@ -113,6 +94,7 @@
"@babel/register": "^7.17.7",
"babel-plugin-istanbul": "^6.1.1",
"chai": "^4.3.6",
"cpy-cli": "^5.0.0",
"crlf": "^1.1.1",
"cross-env": "^7.0.3",
"istanbul": "^1.1.0-alpha.1",
......@@ -121,6 +103,7 @@
"mocha": "^10.0.0",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"renamer": "^4.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.73.0",
"rollup-plugin-json": "^4.0.0",
......@@ -155,13 +138,15 @@
"coveralls--nyc-is-very-slow-and-is-not-used": "nyc report --reporter=text-lcov | coveralls",
"test-travis": "node --experimental-json-modules node_modules/istanbul/lib/cli.js cover -x \"build/**\" -x \"es6/**\" -x \"*.test.js\" -x \"source/findNumbers/Leniency.js\" -x \"source/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.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 -- --colors --reporter spec --require ./test/setup.js \"source/**/*.test.js\" \"test/**/*.test.js\" --recursive",
"clean": "rimraf ./build/**/* ./es6/**/*",
"build:commonjs": "npm-run-all build:commonjs:with-tests build:commonjs:package.json",
"build:commonjs": "npm-run-all build:commonjs:with-tests build:commonjs:package.json build:commonjs:create-typescript-definitions",
"build:commonjs:before-es-modules": "cross-env BABEL_ENV=commonjs babel ./source --out-dir ./build --source-maps --ignore test.js",
"build:commonjs:with-tests": "cross-env BABEL_ENV=commonjs babel ./source --out-dir ./build --source-maps",
"build:commonjs:package.json": "node runnable/create-commonjs-package-json.js",
"build:commonjs:create-typescript-definitions": "rimraf --verbose --glob ./*.d.cts \"./!(node_modules)/**/*.d.cts\" && cpy **/{index,metadata*,examples*,types}.d.ts . --rename={{basename}}.cts && renamer --find d.cts.ts --replace d.cts ./*.d.cts.ts \"./!(node_modules)/**/*.d.cts.ts\"",
"build:modules:copy-typescript-definitions": "cpy --flat min/index.d.ts max && cpy --flat min/index.d.ts mobile",
"build:modules": "cross-env BABEL_ENV=es6 babel ./source --out-dir ./es6 --source-maps --ignore test.js",
"build:bundle": "rollup --config rollup.config.mjs",
"build": "npm-run-all clean build:commonjs build:modules build:bundle",
"build": "npm-run-all clean build:modules:copy-typescript-definitions build:modules build:commonjs build:bundle",
"prepublishOnly": "npm run metadata:generate && npm run generate-country-codes && crlf --set=LF metadata.*.json && npm-run-all build test"
},
"repository": {
......
......@@ -4,6 +4,8 @@ import AsYouTypeState from './AsYouTypeState.js'
import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.js'
import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser.js'
import getCountryByCallingCode from './helpers/getCountryByCallingCode.js'
import getCountryByNationalNumber from './helpers/getCountryByNationalNumber.js'
import isObject from './helpers/isObject.js'
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
......@@ -15,6 +17,9 @@ export default class AsYouType {
constructor(optionsOrDefaultCountry, metadata) {
this.metadata = new Metadata(metadata)
const [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)
// `this.defaultCountry` and `this.defaultCallingCode` aren't required to be in sync.
// For example, `this.defaultCountry` could be `"AR"` and `this.defaultCallingCode` could be `undefined`.
// So `this.defaultCountry` and `this.defaultCallingCode` are totally independent.
this.defaultCountry = defaultCountry
this.defaultCallingCode = defaultCallingCode
this.reset()
......@@ -26,7 +31,7 @@ export default class AsYouType {
let defaultCallingCode
// Turns out `null` also has type "object". Weird.
if (optionsOrDefaultCountry) {
if (typeof optionsOrDefaultCountry === 'object') {
if (isObject(optionsOrDefaultCountry)) {
defaultCountry = optionsOrDefaultCountry.defaultCountry
defaultCallingCode = optionsOrDefaultCountry.defaultCallingCode
} else {
......@@ -273,8 +278,11 @@ export default class AsYouType {
determineTheCountry() {
this.state.setCountry(getCountryByCallingCode(
this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
this.state.nationalSignificantNumber,
this.metadata
{
nationalNumber: this.state.nationalSignificantNumber,
defaultCountry: this.defaultCountry,
metadata: this.metadata
}
))
}
......@@ -339,16 +347,55 @@ export default class AsYouType {
// `this._getCountry()` is basically same as `this.state.country`
// with the only change that it return `undefined` in case of a
// "non-geographic" numbering plan instead of `"001"` "internal use" value.
const country = this._getCountry()
let country = this._getCountry()
if (!nationalSignificantNumber) {
return
}
// `state.country` and `state.callingCode` aren't required to be in sync.
// For example, `country` could be `"AR"` and `callingCode` could be `undefined`.
// So `country` and `callingCode` are totally independent.
if (!country && !callingCode) {
return
}
// By default, if `defaultCountry` parameter was passed when
// creating `AsYouType` instance, `state.country` is gonna be
// that `defaultCountry`, which doesn't entirely conform with
// `parsePhoneNumber()`'s behavior where it attempts to determine
// the country more precisely in cases when multiple countries
// could correspond to the same `countryCallingCode`.
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103#note_1417192969
//
// Because `AsYouType.getNumber()` method is supposed to be a 1:1
// equivalent for `parsePhoneNumber(AsYouType.getNumberValue())`,
// then it should also behave accordingly in cases of `country` ambiguity.
// That's how users of this library would expect it to behave anyway.
//
if (country) {
if (country === this.defaultCountry) {
// `state.country` and `state.callingCode` aren't required to be in sync.
// For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`.
// So `state.country` and `state.callingCode` are totally independent.
const metadata = new Metadata(this.metadata.metadata)
metadata.selectNumberingPlan(country)
const callingCode = metadata.numberingPlan.callingCode()
const ambiguousCountries = this.metadata.getCountryCodesForCallingCode(callingCode)
if (ambiguousCountries.length > 1) {
const exactCountry = getCountryByNationalNumber(nationalSignificantNumber, {
countries: ambiguousCountries,
defaultCountry: this.defaultCountry,
metadata: this.metadata.metadata
})
if (exactCountry) {
country = exactCountry
}
}
}
}
const phoneNumber = new PhoneNumber(
country || callingCode,
nationalSignificantNumber,
......
......@@ -1363,6 +1363,22 @@ describe('AsYouType.getNumberValue()', () => {
formatter.input('421901222333').should.equal('421 901 222 333')
formatter.getTemplate().should.equal('xxx xxx xxx xxx')
})
it('should choose `defaultCountry` (non-"main" one) when multiple countries match the number', function() {
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103
const formatter = new AsYouType('CA')
formatter.input('8004001000')
formatter.getNumber().country.should.equal('CA')
const formatter2 = new AsYouType('US')
formatter2.input('4389999999')
formatter2.getNumber().country.should.equal('CA')
// No country matches the national number digits.
const formatter3 = new AsYouType('US')
formatter3.input('1111111111')
formatter3.getNumber().country.should.equal('US')
})
})
function type(something) {
......
// This "state" object simply holds the state of the "AsYouType" parser:
//
// * `country?: string`
// * `callingCode?: string`
// * `digits: string`
// * `international: boolean`
// * `missingPlus: boolean`
// * `IDDPrefix?: string`
// * `carrierCode?: string`
// * `nationalPrefix?: string`
// * `nationalSignificantNumber?: string`
// * `nationalSignificantNumberMatchesInput: boolean`
// * `complexPrefixBeforeNationalSignificantNumber?: string`
//
// `state.country` and `state.callingCode` aren't required to be in sync.
// For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`.
// So `state.country` and `state.callingCode` are totally independent.
//
export default class AsYouTypeState {
constructor({ onCountryChange, onCallingCodeChange }) {
this.onCountryChange = onCountryChange
......@@ -6,8 +24,8 @@ export default class AsYouTypeState {
reset({ country, callingCode }) {
this.international = false
this.missingPlus = false
this.IDDPrefix = undefined
this.missingPlus = undefined
this.callingCode = undefined
this.digits = ''
this.resetNationalSignificantNumber()
......
......@@ -33,7 +33,11 @@ export default class PhoneNumber {
this.countryCallingCode = countryCallingCode
this.nationalNumber = nationalNumber
this.number = '+' + this.countryCallingCode + this.nationalNumber
this.metadata = metadata
// Exclude `metadata` property output from `PhoneNumber.toString()`
// so that it doesn't clutter the console output of Node.js.
// Previously, when Node.js did `console.log(new PhoneNumber(...))`,
// it would output the whole internal structure of the `metadata` object.
this.getMetadata = () => metadata
}
setExt(ext) {
......@@ -47,20 +51,20 @@ export default class PhoneNumber {
return getPossibleCountriesForNumber(
this.countryCallingCode,
this.nationalNumber,
this.metadata
this.getMetadata()
)
}
isPossible() {
return isPossibleNumber(this, { v2: true }, this.metadata)
return isPossibleNumber(this, { v2: true }, this.getMetadata())
}
isValid() {
return isValidNumber(this, { v2: true }, this.metadata)
return isValidNumber(this, { v2: true }, this.getMetadata())
}
isNonGeographic() {
const metadata = new Metadata(this.metadata)
const metadata = new Metadata(this.getMetadata())
return metadata.isNonGeographicCallingCode(this.countryCallingCode)
}
......@@ -75,7 +79,7 @@ export default class PhoneNumber {
// and just left the `validatePhoneNumberLength()` function, even though that one would require
// and additional step to also validate the actual country / calling code of the phone number.
// validateLength() {
// const metadata = new Metadata(this.metadata)
// const metadata = new Metadata(this.getMetadata())
// metadata.selectNumberingPlan(this.countryCallingCode)
// const result = checkNumberLength(this.nationalNumber, metadata)
// if (result !== 'IS_POSSIBLE') {
......@@ -84,7 +88,7 @@ export default class PhoneNumber {
// }
getType() {
return getNumberType(this, { v2: true }, this.metadata)
return getNumberType(this, { v2: true }, this.getMetadata())
}
format(format, options) {
......@@ -92,7 +96,7 @@ export default class PhoneNumber {
this,
format,
options ? { ...options, v2: true } : { v2: true },
this.metadata
this.getMetadata()
)
}
......
......@@ -345,7 +345,12 @@ export default class PhoneNumberMatcher
return
}
if (this.leniency(phoneNumber, candidate, this.metadata, this.regExpCache)) {
if (this.leniency(phoneNumber, {
candidate,
defaultCountry: this.options.defaultCountry,
metadata: this.metadata,
regExpCache: this.regExpCache
})) {
return {
startsAt: offset,
endsAt: offset + candidate.length,
......
......@@ -19,7 +19,7 @@ export default
/**
* Phone numbers accepted are "possible", but not necessarily "valid".
*/
POSSIBLE(phoneNumber, candidate, metadata)
POSSIBLE(phoneNumber, { candidate, metadata })
{
return true
},
......@@ -29,7 +29,7 @@ export default
* Numbers written in national format must have their national-prefix
* present if it is usually written for a number of this type.
*/
VALID(phoneNumber, candidate, metadata)
VALID(phoneNumber, { candidate, defaultCountry, metadata })
{
if (
!phoneNumber.isValid() ||
......@@ -40,7 +40,7 @@ export default
}
// Skipped for simplicity.
// return isNationalPrefixPresentIfRequired(phoneNumber, metadata)
// return isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
return true
},
......@@ -56,13 +56,13 @@ export default
* country code "+1". If you are not sure about which level to use,
* email the discussion group libphonenumber-discuss@googlegroups.com.
*/
STRICT_GROUPING(phoneNumber, candidate, metadata, regExpCache)
STRICT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
{
if (
!phoneNumber.isValid() ||
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
!isNationalPrefixPresentIfRequired(phoneNumber, metadata)
!isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
)
{
return false
......@@ -89,13 +89,13 @@ export default
* country code "+1". If you are not sure about which level to use, email the discussion group
* libphonenumber-discuss@googlegroups.com.
*/
EXACT_GROUPING(phoneNumber, candidate, metadata, regExpCache)
EXACT_GROUPING(phoneNumber, { candidate, defaultCountry, metadata, regExpCache })
{
if (
!phoneNumber.isValid() ||
!containsOnlyValidXChars(phoneNumber, candidate, metadata) ||
containsMoreThanOneSlashInNationalNumber(phoneNumber, candidate) ||
!isNationalPrefixPresentIfRequired(phoneNumber, metadata)
!isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata })
)
{
return false
......@@ -157,7 +157,7 @@ function containsOnlyValidXChars(phoneNumber, candidate, metadata)
return true
}
function isNationalPrefixPresentIfRequired(phoneNumber, _metadata)
function isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metadata: _metadata })
{
// First, check how we deduced the country code. If it was written in international format, then
// the national prefix is not required.
......@@ -169,7 +169,11 @@ function isNationalPrefixPresentIfRequired(phoneNumber, _metadata)
const metadata = new Metadata(_metadata)
metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, phoneNumber.nationalNumber, metadata)
const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, {
nationalNumber: phoneNumber.nationalNumber,
defaultCountry,
metadata
})
// Check if a national prefix should be present when formatting this number.
const nationalNumber = phoneNumber.nationalNumber
......
......@@ -5,13 +5,13 @@ import AsYouType from './AsYouType.js'
* The phone number can be either in E.164 format
* or in a form of national number digits.
* @param {string} value - A possibly incomplete phone number. Either in E.164 format or in a form of national number digits.
* @param {string?} country - Two-letter ("ISO 3166-1 alpha-2") country code.
* @param {string|object} [optionsOrDefaultCountry] - A two-letter ("ISO 3166-1 alpha-2") country code, or an object of shape `{ defaultCountry?: string, defaultCallingCode?: string }`.
* @return {string} Formatted (possibly incomplete) phone number.
*/
export default function formatIncompletePhoneNumber(value, country, metadata) {
export default function formatIncompletePhoneNumber(value, optionsOrDefaultCountry, metadata) {
if (!metadata) {
metadata = country
country = undefined
metadata = optionsOrDefaultCountry
optionsOrDefaultCountry = undefined
}
return new AsYouType(country, metadata).input(value)
return new AsYouType(optionsOrDefaultCountry, metadata).input(value)
}
\ No newline at end of file