123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- import compare from './tools/semver-compare.js'
- import isObject from './helpers/isObject.js'
- const V2 = '1.0.18'
- const V3 = '1.2.0'
- const V4 = '1.7.35'
- const DEFAULT_EXT_PREFIX = ' ext. '
- const CALLING_CODE_REG_EXP = /^\d+$/
- export default class Metadata {
- constructor(metadata) {
- validateMetadata(metadata)
- this.metadata = metadata
- setVersion.call(this, metadata)
- }
- getCountries() {
- return Object.keys(this.metadata.countries).filter(_ => _ !== '001')
- }
- getCountryMetadata(countryCode) {
- return this.metadata.countries[countryCode]
- }
- nonGeographic() {
- if (this.v1 || this.v2 || this.v3) return
-
-
-
- return this.metadata.nonGeographic || this.metadata.nonGeographical
- }
- hasCountry(country) {
- return this.getCountryMetadata(country) !== undefined
- }
- hasCallingCode(callingCode) {
- if (this.getCountryCodesForCallingCode(callingCode)) {
- return true
- }
- if (this.nonGeographic()) {
- if (this.nonGeographic()[callingCode]) {
- return true
- }
- } else {
-
- const countryCodes = this.countryCallingCodes()[callingCode]
- if (countryCodes && countryCodes.length === 1 && countryCodes[0] === '001') {
- return true
- }
- }
- }
- isNonGeographicCallingCode(callingCode) {
- if (this.nonGeographic()) {
- return this.nonGeographic()[callingCode] ? true : false
- } else {
- return this.getCountryCodesForCallingCode(callingCode) ? false : true
- }
- }
-
- country(countryCode) {
- return this.selectNumberingPlan(countryCode)
- }
- selectNumberingPlan(countryCode, callingCode) {
-
- if (countryCode && CALLING_CODE_REG_EXP.test(countryCode)) {
- callingCode = countryCode
- countryCode = null
- }
- if (countryCode && countryCode !== '001') {
- if (!this.hasCountry(countryCode)) {
- throw new Error(`Unknown country: ${countryCode}`)
- }
- this.numberingPlan = new NumberingPlan(this.getCountryMetadata(countryCode), this)
- } else if (callingCode) {
- if (!this.hasCallingCode(callingCode)) {
- throw new Error(`Unknown calling code: ${callingCode}`)
- }
- this.numberingPlan = new NumberingPlan(this.getNumberingPlanMetadata(callingCode), this)
- } else {
- this.numberingPlan = undefined
- }
- return this
- }
- getCountryCodesForCallingCode(callingCode) {
- const countryCodes = this.countryCallingCodes()[callingCode]
- if (countryCodes) {
-
-
-
-
-
-
-
-
-
- if (countryCodes.length === 1 && countryCodes[0].length === 3) {
- return
- }
- return countryCodes
- }
- }
- getCountryCodeForCallingCode(callingCode) {
- const countryCodes = this.getCountryCodesForCallingCode(callingCode)
- if (countryCodes) {
- return countryCodes[0]
- }
- }
- getNumberingPlanMetadata(callingCode) {
- const countryCode = this.getCountryCodeForCallingCode(callingCode)
- if (countryCode) {
- return this.getCountryMetadata(countryCode)
- }
- if (this.nonGeographic()) {
- const metadata = this.nonGeographic()[callingCode]
- if (metadata) {
- return metadata
- }
- } else {
-
-
-
-
-
- const countryCodes = this.countryCallingCodes()[callingCode]
- if (countryCodes && countryCodes.length === 1 && countryCodes[0] === '001') {
- return this.metadata.countries['001']
- }
- }
- }
-
- countryCallingCode() {
- return this.numberingPlan.callingCode()
- }
-
- IDDPrefix() {
- return this.numberingPlan.IDDPrefix()
- }
-
- defaultIDDPrefix() {
- return this.numberingPlan.defaultIDDPrefix()
- }
-
- nationalNumberPattern() {
- return this.numberingPlan.nationalNumberPattern()
- }
-
- possibleLengths() {
- return this.numberingPlan.possibleLengths()
- }
-
- formats() {
- return this.numberingPlan.formats()
- }
-
- nationalPrefixForParsing() {
- return this.numberingPlan.nationalPrefixForParsing()
- }
-
- nationalPrefixTransformRule() {
- return this.numberingPlan.nationalPrefixTransformRule()
- }
-
- leadingDigits() {
- return this.numberingPlan.leadingDigits()
- }
-
- hasTypes() {
- return this.numberingPlan.hasTypes()
- }
-
- type(type) {
- return this.numberingPlan.type(type)
- }
-
- ext() {
- return this.numberingPlan.ext()
- }
- countryCallingCodes() {
- if (this.v1) return this.metadata.country_phone_code_to_countries
- return this.metadata.country_calling_codes
- }
-
- chooseCountryByCountryCallingCode(callingCode) {
- return this.selectNumberingPlan(callingCode)
- }
- hasSelectedNumberingPlan() {
- return this.numberingPlan !== undefined
- }
- }
- class NumberingPlan {
- constructor(metadata, globalMetadataObject) {
- this.globalMetadataObject = globalMetadataObject
- this.metadata = metadata
- setVersion.call(this, globalMetadataObject.metadata)
- }
- callingCode() {
- return this.metadata[0]
- }
-
-
-
-
-
-
- getDefaultCountryMetadataForRegion() {
- return this.globalMetadataObject.getNumberingPlanMetadata(this.callingCode())
- }
-
- IDDPrefix() {
- if (this.v1 || this.v2) return
- return this.metadata[1]
- }
-
- defaultIDDPrefix() {
- if (this.v1 || this.v2) return
- return this.metadata[12]
- }
- nationalNumberPattern() {
- if (this.v1 || this.v2) return this.metadata[1]
- return this.metadata[2]
- }
-
- possibleLengths() {
- if (this.v1) return
- return this.metadata[this.v2 ? 2 : 3]
- }
- _getFormats(metadata) {
- return metadata[this.v1 ? 2 : this.v2 ? 3 : 4]
- }
-
-
-
- formats() {
- const formats = this._getFormats(this.metadata) || this._getFormats(this.getDefaultCountryMetadataForRegion()) || []
- return formats.map(_ => new Format(_, this))
- }
- nationalPrefix() {
- return this.metadata[this.v1 ? 3 : this.v2 ? 4 : 5]
- }
- _getNationalPrefixFormattingRule(metadata) {
- return metadata[this.v1 ? 4 : this.v2 ? 5 : 6]
- }
-
-
-
- nationalPrefixFormattingRule() {
- return this._getNationalPrefixFormattingRule(this.metadata) || this._getNationalPrefixFormattingRule(this.getDefaultCountryMetadataForRegion())
- }
- _nationalPrefixForParsing() {
- return this.metadata[this.v1 ? 5 : this.v2 ? 6 : 7]
- }
- nationalPrefixForParsing() {
-
-
- return this._nationalPrefixForParsing() || this.nationalPrefix()
- }
- nationalPrefixTransformRule() {
- return this.metadata[this.v1 ? 6 : this.v2 ? 7 : 8]
- }
- _getNationalPrefixIsOptionalWhenFormatting() {
- return !!this.metadata[this.v1 ? 7 : this.v2 ? 8 : 9]
- }
-
-
-
-
- nationalPrefixIsOptionalWhenFormattingInNationalFormat() {
- return this._getNationalPrefixIsOptionalWhenFormatting(this.metadata) ||
- this._getNationalPrefixIsOptionalWhenFormatting(this.getDefaultCountryMetadataForRegion())
- }
- leadingDigits() {
- return this.metadata[this.v1 ? 8 : this.v2 ? 9 : 10]
- }
- types() {
- return this.metadata[this.v1 ? 9 : this.v2 ? 10 : 11]
- }
- hasTypes() {
-
-
- if (this.types() && this.types().length === 0) {
- return false
- }
-
-
- return !!this.types()
- }
- type(type) {
- if (this.hasTypes() && getType(this.types(), type)) {
- return new Type(getType(this.types(), type), this)
- }
- }
- ext() {
- if (this.v1 || this.v2) return DEFAULT_EXT_PREFIX
- return this.metadata[13] || DEFAULT_EXT_PREFIX
- }
- }
- class Format {
- constructor(format, metadata) {
- this._format = format
- this.metadata = metadata
- }
- pattern() {
- return this._format[0]
- }
- format() {
- return this._format[1]
- }
- leadingDigitsPatterns() {
- return this._format[2] || []
- }
- nationalPrefixFormattingRule() {
- return this._format[3] || this.metadata.nationalPrefixFormattingRule()
- }
- nationalPrefixIsOptionalWhenFormattingInNationalFormat() {
- return !!this._format[4] || this.metadata.nationalPrefixIsOptionalWhenFormattingInNationalFormat()
- }
- nationalPrefixIsMandatoryWhenFormattingInNationalFormat() {
-
-
-
-
-
- return this.usesNationalPrefix() && !this.nationalPrefixIsOptionalWhenFormattingInNationalFormat()
- }
-
- usesNationalPrefix() {
- return this.nationalPrefixFormattingRule() &&
-
- !FIRST_GROUP_ONLY_PREFIX_PATTERN.test(this.nationalPrefixFormattingRule())
-
-
-
-
- ? true
- : false
- }
- internationalFormat() {
- return this._format[5] || this.format()
- }
- }
- const FIRST_GROUP_ONLY_PREFIX_PATTERN = /^\(?\$1\)?$/
- class Type {
- constructor(type, metadata) {
- this.type = type
- this.metadata = metadata
- }
- pattern() {
- if (this.metadata.v1) return this.type
- return this.type[0]
- }
- possibleLengths() {
- if (this.metadata.v1) return
- return this.type[1] || this.metadata.possibleLengths()
- }
- }
- function getType(types, type) {
- switch (type) {
- case 'FIXED_LINE':
- return types[0]
- case 'MOBILE':
- return types[1]
- case 'TOLL_FREE':
- return types[2]
- case 'PREMIUM_RATE':
- return types[3]
- case 'PERSONAL_NUMBER':
- return types[4]
- case 'VOICEMAIL':
- return types[5]
- case 'UAN':
- return types[6]
- case 'PAGER':
- return types[7]
- case 'VOIP':
- return types[8]
- case 'SHARED_COST':
- return types[9]
- }
- }
- export function validateMetadata(metadata) {
- if (!metadata) {
- throw new Error('[libphonenumber-js] `metadata` argument not passed. Check your arguments.')
- }
-
-
- if (!isObject(metadata) || !isObject(metadata.countries)) {
- throw new Error(`[libphonenumber-js] \`metadata\` argument was passed but it's not a valid metadata. Must be an object having \`.countries\` child object property. Got ${isObject(metadata) ? 'an object of shape: { ' + Object.keys(metadata).join(', ') + ' }' : 'a ' + typeOf(metadata) + ': ' + metadata}.`)
- }
- }
- const typeOf = _ => typeof _
- export function getExtPrefix(country, metadata) {
- metadata = new Metadata(metadata)
- if (metadata.hasCountry(country)) {
- return metadata.country(country).ext()
- }
- return DEFAULT_EXT_PREFIX
- }
- export function getCountryCallingCode(country, metadata) {
- metadata = new Metadata(metadata)
- if (metadata.hasCountry(country)) {
- return metadata.country(country).countryCallingCode()
- }
- throw new Error(`Unknown country: ${country}`)
- }
- export function isSupportedCountry(country, metadata) {
-
-
- return metadata.countries.hasOwnProperty(country)
- }
- function setVersion(metadata) {
- const { version } = metadata
- if (typeof version === 'number') {
- this.v1 = version === 1
- this.v2 = version === 2
- this.v3 = version === 3
- this.v4 = version === 4
- } else {
- if (!version) {
- this.v1 = true
- } else if (compare(version, V3) === -1) {
- this.v2 = true
- } else if (compare(version, V4) === -1) {
- this.v3 = true
- } else {
- this.v4 = true
- }
- }
- }
|