libphonenumber-js 1.10.24 → 1.10.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CODE_OF_CONDUCT.md +78 -0
- package/README.md +16 -6
- package/build/PhoneNumber.js +6 -0
- package/build/PhoneNumber.js.map +1 -1
- package/build/PhoneNumberMatcher.js +53 -62
- package/build/PhoneNumberMatcher.js.map +1 -1
- package/build/PhoneNumberMatcher.test.js +8 -0
- package/build/PhoneNumberMatcher.test.js.map +1 -1
- package/build/findNumbers/Leniency.js +60 -50
- package/build/findNumbers/Leniency.js.map +1 -1
- package/build/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +75 -0
- package/build/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
- package/build/format.js +1 -0
- package/build/format.js.map +1 -1
- package/build/helpers/RFC3966.test.js +3 -1
- package/build/helpers/RFC3966.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.js +44 -4
- package/build/helpers/extractCountryCallingCode.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.test.js +2 -0
- package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/build/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +86 -0
- package/build/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
- package/build/helpers/extractPhoneContext.js +97 -0
- package/build/helpers/extractPhoneContext.js.map +1 -0
- package/build/helpers/extractPhoneContext.test.js +72 -0
- package/build/helpers/extractPhoneContext.test.js.map +1 -0
- package/build/parse.js +19 -9
- package/build/parse.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.d.ts +3 -3
- package/custom.d.ts +4 -4
- package/es6/PhoneNumber.js +6 -0
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/PhoneNumberMatcher.js +53 -62
- package/es6/PhoneNumberMatcher.js.map +1 -1
- package/es6/PhoneNumberMatcher.test.js +8 -0
- package/es6/PhoneNumberMatcher.test.js.map +1 -1
- package/es6/findNumbers/Leniency.js +56 -50
- package/es6/findNumbers/Leniency.js.map +1 -1
- package/es6/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +66 -0
- package/es6/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
- package/es6/format.js +1 -1
- package/es6/format.js.map +1 -1
- package/es6/helpers/RFC3966.test.js +3 -1
- package/es6/helpers/RFC3966.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.js +44 -4
- package/es6/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.test.js +2 -0
- package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +70 -0
- package/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
- package/es6/helpers/extractPhoneContext.js +82 -0
- package/es6/helpers/extractPhoneContext.js.map +1 -0
- package/es6/helpers/extractPhoneContext.test.js +66 -0
- package/es6/helpers/extractPhoneContext.test.js.map +1 -0
- package/es6/parse.js +20 -10
- package/es6/parse.js.map +1 -1
- package/examples.mobile.json.d.ts +1 -1
- package/index.d.ts +3 -3
- package/metadata.full.json +1 -1
- package/metadata.full.json.d.ts +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.d.ts +1 -1
- package/metadata.max.json.js +1 -1
- package/metadata.min.json +1 -1
- package/metadata.min.json.d.ts +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.d.ts +1 -1
- package/metadata.mobile.json.js +1 -1
- package/min/index.d.ts +3 -3
- package/package.json +4 -4
- package/source/AsYouTypeFormatter.PatternMatcher.d.ts +1 -1
- package/source/PhoneNumber.js +6 -0
- package/source/PhoneNumberMatcher.js +55 -58
- package/source/PhoneNumberMatcher.test.js +4 -0
- package/source/findNumbers/Leniency.js +281 -271
- package/source/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +64 -0
- package/source/format.js +1 -1
- package/source/helpers/RFC3966.test.js +3 -0
- package/source/helpers/extractCountryCallingCode.js +48 -3
- package/source/helpers/extractCountryCallingCode.test.js +2 -0
- package/source/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +74 -0
- package/source/helpers/extractPhoneContext.js +103 -0
- package/source/helpers/extractPhoneContext.test.js +104 -0
- package/source/parse.js +18 -8
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import parsePhoneNumber from '../parsePhoneNumber.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Matches a phone number object against a phone number string.
|
|
5
|
+
* @param {string} phoneNumberString
|
|
6
|
+
* @param {PhoneNumber} phoneNumber
|
|
7
|
+
* @param {object} metadata — Metadata JSON
|
|
8
|
+
* @return {'INVALID_NUMBER'|'NO_MATCH'|'SHORT_NSN_MATCH'|'NSN_MATCH'|'EXACT_MATCH'}
|
|
9
|
+
*/
|
|
10
|
+
export default function matchPhoneNumberStringAgainstPhoneNumber(phoneNumberString, phoneNumber, metadata) {
|
|
11
|
+
// Parse `phoneNumberString`.
|
|
12
|
+
let phoneNumberStringContainsCallingCode = true
|
|
13
|
+
let parsedPhoneNumber = parsePhoneNumber(phoneNumberString, metadata)
|
|
14
|
+
if (!parsedPhoneNumber) {
|
|
15
|
+
// If `phoneNumberString` didn't contain a country calling code
|
|
16
|
+
// then substitute it with the `phoneNumber`'s country calling code.
|
|
17
|
+
phoneNumberStringContainsCallingCode = false
|
|
18
|
+
parsedPhoneNumber = parsePhoneNumber(phoneNumberString, { defaultCallingCode: phoneNumber.countryCallingCode }, metadata)
|
|
19
|
+
}
|
|
20
|
+
if (!parsedPhoneNumber) {
|
|
21
|
+
return 'INVALID_NUMBER'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Check that the extensions match.
|
|
25
|
+
if (phoneNumber.ext) {
|
|
26
|
+
if (parsedPhoneNumber.ext !== phoneNumber.ext) {
|
|
27
|
+
return 'NO_MATCH'
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
if (parsedPhoneNumber.ext) {
|
|
31
|
+
return 'NO_MATCH'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Check that country calling codes match.
|
|
36
|
+
if (phoneNumberStringContainsCallingCode) {
|
|
37
|
+
if (phoneNumber.countryCallingCode !== parsedPhoneNumber.countryCallingCode) {
|
|
38
|
+
return 'NO_MATCH'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Check if the whole numbers match.
|
|
43
|
+
if (phoneNumber.number === parsedPhoneNumber.number) {
|
|
44
|
+
if (phoneNumberStringContainsCallingCode) {
|
|
45
|
+
return 'EXACT_MATCH'
|
|
46
|
+
} else {
|
|
47
|
+
return 'NSN_MATCH'
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Check if one national number is a "suffix" of the other.
|
|
52
|
+
if (
|
|
53
|
+
phoneNumber.nationalNumber.indexOf(parsedPhoneNumber.nationalNumber) === 0 ||
|
|
54
|
+
parsedPhoneNumber.nationalNumber.indexOf(phoneNumber.nationalNumber) === 0
|
|
55
|
+
) {
|
|
56
|
+
// "A SHORT_NSN_MATCH occurs if there is a difference because of the
|
|
57
|
+
// presence or absence of an 'Italian leading zero', the presence or
|
|
58
|
+
// absence of an extension, or one NSN being a shorter variant of the
|
|
59
|
+
// other."
|
|
60
|
+
return 'SHORT_NSN_MATCH'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return 'NO_MATCH'
|
|
64
|
+
}
|
package/source/format.js
CHANGED
|
@@ -128,7 +128,7 @@ function formatNationalNumber(number, carrierCode, formatAs, metadata, options)
|
|
|
128
128
|
)
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
function chooseFormatForNumber(availableFormats, nationalNnumber) {
|
|
131
|
+
export function chooseFormatForNumber(availableFormats, nationalNnumber) {
|
|
132
132
|
for (const format of availableFormats) {
|
|
133
133
|
// Validate leading digits.
|
|
134
134
|
// The test case for "else path" could be found by searching for
|
|
@@ -13,7 +13,7 @@ import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'
|
|
|
13
13
|
* @param {string} [country] — Default country.
|
|
14
14
|
* @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
|
|
15
15
|
* @param {object} metadata
|
|
16
|
-
* @return {object} `{ countryCallingCode: string?, number: string }`
|
|
16
|
+
* @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
|
|
17
17
|
* @example
|
|
18
18
|
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
|
|
19
19
|
* extractCountryCallingCode('2133734253', 'US', null, metadata)
|
|
@@ -31,6 +31,8 @@ export default function extractCountryCallingCode(
|
|
|
31
31
|
return {}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
let isNumberWithIddPrefix
|
|
35
|
+
|
|
34
36
|
// If this is not an international phone number,
|
|
35
37
|
// then either extract an "IDD" prefix, or extract a
|
|
36
38
|
// country calling code from a number by autocorrecting it
|
|
@@ -46,6 +48,7 @@ export default function extractCountryCallingCode(
|
|
|
46
48
|
// convert the number to international one
|
|
47
49
|
// for subsequent parsing.
|
|
48
50
|
if (numberWithoutIDD && numberWithoutIDD !== number) {
|
|
51
|
+
isNumberWithIddPrefix = true
|
|
49
52
|
number = '+' + numberWithoutIDD
|
|
50
53
|
} else {
|
|
51
54
|
// Check to see if the number starts with the country calling code
|
|
@@ -64,12 +67,17 @@ export default function extractCountryCallingCode(
|
|
|
64
67
|
)
|
|
65
68
|
if (countryCallingCode) {
|
|
66
69
|
return {
|
|
70
|
+
countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
|
|
67
71
|
countryCallingCode,
|
|
68
72
|
number: shorterNumber
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
}
|
|
72
|
-
return {
|
|
76
|
+
return {
|
|
77
|
+
// No need to set it to `UNSPECIFIED`. It can be just `undefined`.
|
|
78
|
+
// countryCallingCodeSource: 'UNSPECIFIED',
|
|
79
|
+
number
|
|
80
|
+
}
|
|
73
81
|
}
|
|
74
82
|
}
|
|
75
83
|
|
|
@@ -95,6 +103,7 @@ export default function extractCountryCallingCode(
|
|
|
95
103
|
if (metadata.hasCallingCode(countryCallingCode)) {
|
|
96
104
|
metadata.selectNumberingPlan(countryCallingCode)
|
|
97
105
|
return {
|
|
106
|
+
countryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',
|
|
98
107
|
countryCallingCode,
|
|
99
108
|
number: number.slice(i)
|
|
100
109
|
}
|
|
@@ -103,4 +112,40 @@ export default function extractCountryCallingCode(
|
|
|
103
112
|
}
|
|
104
113
|
|
|
105
114
|
return {}
|
|
106
|
-
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The possible values for the returned `countryCallingCodeSource` are:
|
|
118
|
+
//
|
|
119
|
+
// Copy-pasted from:
|
|
120
|
+
// https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto
|
|
121
|
+
//
|
|
122
|
+
// // The source from which the country_code is derived. This is not set in the
|
|
123
|
+
// // general parsing method, but in the method that parses and keeps raw_input.
|
|
124
|
+
// // New fields could be added upon request.
|
|
125
|
+
// enum CountryCodeSource {
|
|
126
|
+
// // Default value returned if this is not set, because the phone number was
|
|
127
|
+
// // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will
|
|
128
|
+
// // return false if this is the case.
|
|
129
|
+
// UNSPECIFIED = 0;
|
|
130
|
+
//
|
|
131
|
+
// // The country_code is derived based on a phone number with a leading "+",
|
|
132
|
+
// // e.g. the French number "+33 1 42 68 53 00".
|
|
133
|
+
// FROM_NUMBER_WITH_PLUS_SIGN = 1;
|
|
134
|
+
//
|
|
135
|
+
// // The country_code is derived based on a phone number with a leading IDD,
|
|
136
|
+
// // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
|
|
137
|
+
// FROM_NUMBER_WITH_IDD = 5;
|
|
138
|
+
//
|
|
139
|
+
// // The country_code is derived based on a phone number without a leading
|
|
140
|
+
// // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
|
|
141
|
+
// // supplied as France.
|
|
142
|
+
// FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
|
|
143
|
+
//
|
|
144
|
+
// // The country_code is derived NOT based on the phone number itself, but
|
|
145
|
+
// // from the defaultCountry parameter provided in the parsing function by the
|
|
146
|
+
// // clients. This happens mostly for numbers written in the national format
|
|
147
|
+
// // (without country code). For example, this would be set when parsing the
|
|
148
|
+
// // French number "01 42 68 53 00", when defaultCountry is supplied as
|
|
149
|
+
// // France.
|
|
150
|
+
// FROM_DEFAULT_COUNTRY = 20;
|
|
151
|
+
// }
|
|
@@ -4,11 +4,13 @@ import metadata from '../../metadata.min.json' assert { type: 'json' }
|
|
|
4
4
|
describe('extractCountryCallingCode', () => {
|
|
5
5
|
it('should extract country calling code from a number', () => {
|
|
6
6
|
extractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({
|
|
7
|
+
countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
|
|
7
8
|
countryCallingCode: '7',
|
|
8
9
|
number: '8005553535'
|
|
9
10
|
})
|
|
10
11
|
|
|
11
12
|
extractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({
|
|
13
|
+
countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
|
|
12
14
|
countryCallingCode: '7',
|
|
13
15
|
number: '800'
|
|
14
16
|
})
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import extractPhoneContext, {
|
|
2
|
+
isPhoneContextValid,
|
|
3
|
+
PLUS_SIGN,
|
|
4
|
+
RFC3966_PREFIX_,
|
|
5
|
+
RFC3966_PHONE_CONTEXT_,
|
|
6
|
+
RFC3966_ISDN_SUBADDRESS_
|
|
7
|
+
} from './extractPhoneContext.js'
|
|
8
|
+
|
|
9
|
+
import ParseError from '../ParseError.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} numberToParse
|
|
13
|
+
* @param {string} nationalNumber
|
|
14
|
+
* @return {}
|
|
15
|
+
*/
|
|
16
|
+
export default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, {
|
|
17
|
+
extractFormattedPhoneNumber
|
|
18
|
+
}) {
|
|
19
|
+
const phoneContext = extractPhoneContext(numberToParse)
|
|
20
|
+
if (!isPhoneContextValid(phoneContext)) {
|
|
21
|
+
throw new ParseError('NOT_A_NUMBER')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let phoneNumberString
|
|
25
|
+
|
|
26
|
+
if (phoneContext === null) {
|
|
27
|
+
// Extract a possible number from the string passed in.
|
|
28
|
+
// (this strips leading characters that could not be the start of a phone number)
|
|
29
|
+
phoneNumberString = extractFormattedPhoneNumber(numberToParse) || ''
|
|
30
|
+
} else {
|
|
31
|
+
phoneNumberString = ''
|
|
32
|
+
|
|
33
|
+
// If the phone context contains a phone number prefix, we need to capture
|
|
34
|
+
// it, whereas domains will be ignored.
|
|
35
|
+
if (phoneContext.charAt(0) === PLUS_SIGN) {
|
|
36
|
+
phoneNumberString += phoneContext
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Now append everything between the "tel:" prefix and the phone-context.
|
|
40
|
+
// This should include the national number, an optional extension or
|
|
41
|
+
// isdn-subaddress component. Note we also handle the case when "tel:" is
|
|
42
|
+
// missing, as we have seen in some of the phone number inputs.
|
|
43
|
+
// In that case, we append everything from the beginning.
|
|
44
|
+
const indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_)
|
|
45
|
+
let indexOfNationalNumber
|
|
46
|
+
// RFC 3966 "tel:" prefix is preset at this stage because
|
|
47
|
+
// `isPhoneContextValid()` requires it to be present.
|
|
48
|
+
/* istanbul ignore else */
|
|
49
|
+
if (indexOfRfc3966Prefix >= 0) {
|
|
50
|
+
indexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length
|
|
51
|
+
} else {
|
|
52
|
+
indexOfNationalNumber = 0
|
|
53
|
+
}
|
|
54
|
+
const indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_)
|
|
55
|
+
phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Delete the isdn-subaddress and everything after it if it is present.
|
|
59
|
+
// Note extension won't appear at the same time with isdn-subaddress
|
|
60
|
+
// according to paragraph 5.3 of the RFC3966 spec.
|
|
61
|
+
const indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_)
|
|
62
|
+
if (indexOfIsdn > 0) {
|
|
63
|
+
phoneNumberString = phoneNumberString.substring(0, indexOfIsdn)
|
|
64
|
+
}
|
|
65
|
+
// If both phone context and isdn-subaddress are absent but other
|
|
66
|
+
// parameters are present, the parameters are left in nationalNumber.
|
|
67
|
+
// This is because we are concerned about deleting content from a potential
|
|
68
|
+
// number string when there is no strong evidence that the number is
|
|
69
|
+
// actually written in RFC3966.
|
|
70
|
+
|
|
71
|
+
if (phoneNumberString !== '') {
|
|
72
|
+
return phoneNumberString
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// When phone numbers are written in `RFC3966` format — `"tel:+12133734253"` —
|
|
2
|
+
// they can have their "calling code" part written separately in a `phone-context` parameter.
|
|
3
|
+
// Example: `"tel:12133734253;phone-context=+1"`.
|
|
4
|
+
// This function parses the full phone number from the local number and the `phone-context`
|
|
5
|
+
// when the `phone-context` contains a `+` sign.
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
VALID_DIGITS,
|
|
9
|
+
// PLUS_CHARS
|
|
10
|
+
} from '../constants.js'
|
|
11
|
+
|
|
12
|
+
export const PLUS_SIGN = '+'
|
|
13
|
+
|
|
14
|
+
const RFC3966_VISUAL_SEPARATOR_ = '[\\-\\.\\(\\)]?'
|
|
15
|
+
|
|
16
|
+
const RFC3966_PHONE_DIGIT_ = '(' + '[' + VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')'
|
|
17
|
+
|
|
18
|
+
const RFC3966_GLOBAL_NUMBER_DIGITS_ =
|
|
19
|
+
'^' +
|
|
20
|
+
'\\' +
|
|
21
|
+
PLUS_SIGN +
|
|
22
|
+
RFC3966_PHONE_DIGIT_ +
|
|
23
|
+
'*' +
|
|
24
|
+
'[' + VALID_DIGITS + ']' +
|
|
25
|
+
RFC3966_PHONE_DIGIT_ +
|
|
26
|
+
'*' +
|
|
27
|
+
'$'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Regular expression of valid global-number-digits for the phone-context
|
|
31
|
+
* parameter, following the syntax defined in RFC3966.
|
|
32
|
+
*/
|
|
33
|
+
const RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g')
|
|
34
|
+
|
|
35
|
+
// In this port of Google's library, we don't accept alpha characters in phone numbers.
|
|
36
|
+
// const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS
|
|
37
|
+
const ALPHANUM_ = VALID_DIGITS
|
|
38
|
+
|
|
39
|
+
const RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\-)*[' + ALPHANUM_ + '])*'
|
|
40
|
+
|
|
41
|
+
const VALID_ALPHA_ = 'a-zA-Z'
|
|
42
|
+
const RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\-)*[' + ALPHANUM_ + '])*'
|
|
43
|
+
|
|
44
|
+
const RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\.)*' + RFC3966_TOPLABEL_ + '\\.?$'
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Regular expression of valid domainname for the phone-context parameter,
|
|
48
|
+
* following the syntax defined in RFC3966.
|
|
49
|
+
*/
|
|
50
|
+
const RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g')
|
|
51
|
+
|
|
52
|
+
export const RFC3966_PREFIX_ = 'tel:'
|
|
53
|
+
export const RFC3966_PHONE_CONTEXT_ = ';phone-context='
|
|
54
|
+
export const RFC3966_ISDN_SUBADDRESS_ = ';isub='
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Extracts the value of the phone-context parameter of `numberToExtractFrom`,
|
|
58
|
+
* following the syntax defined in RFC3966.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} numberToExtractFrom
|
|
61
|
+
* @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.
|
|
62
|
+
*/
|
|
63
|
+
export default function extractPhoneContext(numberToExtractFrom) {
|
|
64
|
+
const indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_)
|
|
65
|
+
// If no phone-context parameter is present
|
|
66
|
+
if (indexOfPhoneContext < 0) {
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length
|
|
71
|
+
// If phone-context parameter is empty
|
|
72
|
+
if (phoneContextStart >= numberToExtractFrom.length) {
|
|
73
|
+
return ''
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart)
|
|
77
|
+
// If phone-context is not the last parameter
|
|
78
|
+
if (phoneContextEnd >= 0) {
|
|
79
|
+
return numberToExtractFrom.substring(phoneContextStart, phoneContextEnd)
|
|
80
|
+
} else {
|
|
81
|
+
return numberToExtractFrom.substring(phoneContextStart)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns whether the value of phoneContext follows the syntax defined in RFC3966.
|
|
87
|
+
*
|
|
88
|
+
* @param {string|null} phoneContext
|
|
89
|
+
* @return {boolean}
|
|
90
|
+
*/
|
|
91
|
+
export function isPhoneContextValid(phoneContext) {
|
|
92
|
+
if (phoneContext === null) {
|
|
93
|
+
return true
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (phoneContext.length === 0) {
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Does phone-context value match pattern of global-number-digits or domainname.
|
|
101
|
+
return RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) ||
|
|
102
|
+
RFC3966_DOMAINNAME_PATTERN_.test(phoneContext)
|
|
103
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import parsePhoneNumber_ from '../parsePhoneNumber.js'
|
|
2
|
+
import PhoneNumber from '../PhoneNumber.js'
|
|
3
|
+
import metadata from '../../metadata.min.json' assert { type: 'json' }
|
|
4
|
+
|
|
5
|
+
function parsePhoneNumber(...parameters) {
|
|
6
|
+
parameters.push(metadata)
|
|
7
|
+
return parsePhoneNumber_.apply(this, parameters)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('extractPhoneContext', function() {
|
|
11
|
+
it('should parse RFC 3966 phone number URIs', function() {
|
|
12
|
+
// context = ";phone-context=" descriptor
|
|
13
|
+
// descriptor = domainname / global-number-digits
|
|
14
|
+
|
|
15
|
+
const NZ_NUMBER = new PhoneNumber('64', '33316005', metadata)
|
|
16
|
+
|
|
17
|
+
// Valid global-phone-digits
|
|
18
|
+
expectPhoneNumbersToBeEqual(
|
|
19
|
+
parsePhoneNumber('tel:033316005;phone-context=+64'),
|
|
20
|
+
NZ_NUMBER
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
expectPhoneNumbersToBeEqual(
|
|
24
|
+
parsePhoneNumber('tel:033316005;phone-context=+64;{this isn\'t part of phone-context anymore!}'),
|
|
25
|
+
NZ_NUMBER
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const nzFromPhoneContext = new PhoneNumber('64', '3033316005', metadata)
|
|
29
|
+
expectPhoneNumbersToBeEqual(
|
|
30
|
+
parsePhoneNumber('tel:033316005;phone-context=+64-3'),
|
|
31
|
+
nzFromPhoneContext
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
const brFromPhoneContext = new PhoneNumber('55', '5033316005', metadata)
|
|
35
|
+
expectPhoneNumbersToBeEqual(
|
|
36
|
+
parsePhoneNumber('tel:033316005;phone-context=+(555)'),
|
|
37
|
+
brFromPhoneContext
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const usFromPhoneContext = new PhoneNumber('1', '23033316005', metadata)
|
|
41
|
+
expectPhoneNumbersToBeEqual(
|
|
42
|
+
parsePhoneNumber('tel:033316005;phone-context=+-1-2.3()'),
|
|
43
|
+
usFromPhoneContext
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
// Valid domainname.
|
|
47
|
+
expectPhoneNumbersToBeEqual(
|
|
48
|
+
parsePhoneNumber('tel:033316005;phone-context=abc.nz', 'NZ'),
|
|
49
|
+
NZ_NUMBER
|
|
50
|
+
)
|
|
51
|
+
expectPhoneNumbersToBeEqual(
|
|
52
|
+
parsePhoneNumber('tel:033316005;phone-context=www.PHONE-numb3r.com', 'NZ'),
|
|
53
|
+
NZ_NUMBER
|
|
54
|
+
)
|
|
55
|
+
expectPhoneNumbersToBeEqual(
|
|
56
|
+
parsePhoneNumber('tel:033316005;phone-context=a', 'NZ'),
|
|
57
|
+
NZ_NUMBER
|
|
58
|
+
)
|
|
59
|
+
expectPhoneNumbersToBeEqual(
|
|
60
|
+
parsePhoneNumber('tel:033316005;phone-context=3phone.J.', 'NZ'),
|
|
61
|
+
NZ_NUMBER
|
|
62
|
+
)
|
|
63
|
+
expectPhoneNumbersToBeEqual(
|
|
64
|
+
parsePhoneNumber('tel:033316005;phone-context=a--z', 'NZ'),
|
|
65
|
+
NZ_NUMBER
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
// Should strip ISDN subaddress.
|
|
69
|
+
expectPhoneNumbersToBeEqual(
|
|
70
|
+
parsePhoneNumber('tel:033316005;isub=/@;phone-context=+64', 'NZ'),
|
|
71
|
+
NZ_NUMBER
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
// // Should support incorrectly-written RFC 3966 phone numbers:
|
|
75
|
+
// // the ones written without a `tel:` prefix.
|
|
76
|
+
// expectPhoneNumbersToBeEqual(
|
|
77
|
+
// parsePhoneNumber('033316005;phone-context=+64', 'NZ'),
|
|
78
|
+
// NZ_NUMBER
|
|
79
|
+
// )
|
|
80
|
+
|
|
81
|
+
// Invalid descriptor.
|
|
82
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=')
|
|
83
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+')
|
|
84
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=64')
|
|
85
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=++64')
|
|
86
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+abc')
|
|
87
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=.')
|
|
88
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=3phone')
|
|
89
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a-.nz')
|
|
90
|
+
expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a{b}c')
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
function expectToThrowForInvalidPhoneContext(string) {
|
|
95
|
+
expect(parsePhoneNumber(string)).to.be.undefined
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function expectPhoneNumbersToBeEqual(phoneNumber1, phoneNumber2) {
|
|
99
|
+
if (!phoneNumber1 || !phoneNumber2) {
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
return phoneNumber1.number === phoneNumber2.number &&
|
|
103
|
+
phoneNumber1.ext === phoneNumber2.ext
|
|
104
|
+
}
|
package/source/parse.js
CHANGED
|
@@ -17,13 +17,14 @@ import extractExtension from './helpers/extension/extractExtension.js'
|
|
|
17
17
|
import parseIncompletePhoneNumber from './parseIncompletePhoneNumber.js'
|
|
18
18
|
import getCountryCallingCode from './getCountryCallingCode.js'
|
|
19
19
|
import { isPossibleNumber } from './isPossible.js'
|
|
20
|
-
import { parseRFC3966 } from './helpers/RFC3966.js'
|
|
20
|
+
// import { parseRFC3966 } from './helpers/RFC3966.js'
|
|
21
21
|
import PhoneNumber from './PhoneNumber.js'
|
|
22
22
|
import matchesEntirely from './helpers/matchesEntirely.js'
|
|
23
23
|
import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
|
|
24
24
|
import extractNationalNumber from './helpers/extractNationalNumber.js'
|
|
25
25
|
import stripIddPrefix from './helpers/stripIddPrefix.js'
|
|
26
26
|
import getCountryByCallingCode from './helpers/getCountryByCallingCode.js'
|
|
27
|
+
import extractFormattedPhoneNumberFromPossibleRfc3966NumberUri from './helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js'
|
|
27
28
|
|
|
28
29
|
// We don't allow input strings for parsing to be longer than 250 chars.
|
|
29
30
|
// This prevents malicious input from consuming CPU.
|
|
@@ -96,6 +97,7 @@ export default function parse(text, options, metadata) {
|
|
|
96
97
|
country,
|
|
97
98
|
nationalNumber,
|
|
98
99
|
countryCallingCode,
|
|
100
|
+
countryCallingCodeSource,
|
|
99
101
|
carrierCode
|
|
100
102
|
} = parsePhoneNumber(
|
|
101
103
|
formattedPhoneNumber,
|
|
@@ -154,6 +156,7 @@ export default function parse(text, options, metadata) {
|
|
|
154
156
|
if (ext) {
|
|
155
157
|
phoneNumber.ext = ext
|
|
156
158
|
}
|
|
159
|
+
phoneNumber.__countryCallingCodeSource = countryCallingCodeSource
|
|
157
160
|
return phoneNumber
|
|
158
161
|
}
|
|
159
162
|
|
|
@@ -229,11 +232,14 @@ function extractFormattedPhoneNumber(text, extract, throwOnError) {
|
|
|
229
232
|
* @return {object} `{ ?number, ?ext }`.
|
|
230
233
|
*/
|
|
231
234
|
function parseInput(text, v2, extract) {
|
|
232
|
-
// Parse RFC 3966 phone number URI.
|
|
233
|
-
if (text && text.indexOf('tel:') === 0) {
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
let number = extractFormattedPhoneNumber(text, extract, v2)
|
|
235
|
+
// // Parse RFC 3966 phone number URI.
|
|
236
|
+
// if (text && text.indexOf('tel:') === 0) {
|
|
237
|
+
// return parseRFC3966(text)
|
|
238
|
+
// }
|
|
239
|
+
// let number = extractFormattedPhoneNumber(text, extract, v2)
|
|
240
|
+
let number = extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(text, {
|
|
241
|
+
extractFormattedPhoneNumber: (text) => extractFormattedPhoneNumber(text, extract, v2)
|
|
242
|
+
})
|
|
237
243
|
// If the phone number is not viable, then abort.
|
|
238
244
|
if (!number) {
|
|
239
245
|
return {}
|
|
@@ -282,7 +288,7 @@ function parsePhoneNumber(
|
|
|
282
288
|
metadata
|
|
283
289
|
) {
|
|
284
290
|
// Extract calling code from phone number.
|
|
285
|
-
let { countryCallingCode, number } = extractCountryCallingCode(
|
|
291
|
+
let { countryCallingCodeSource, countryCallingCode, number } = extractCountryCallingCode(
|
|
286
292
|
parseIncompletePhoneNumber(formattedPhoneNumber),
|
|
287
293
|
defaultCountry,
|
|
288
294
|
defaultCallingCode,
|
|
@@ -313,7 +319,10 @@ function parsePhoneNumber(
|
|
|
313
319
|
else return {}
|
|
314
320
|
|
|
315
321
|
if (!number) {
|
|
316
|
-
return {
|
|
322
|
+
return {
|
|
323
|
+
countryCallingCodeSource,
|
|
324
|
+
countryCallingCode
|
|
325
|
+
}
|
|
317
326
|
}
|
|
318
327
|
|
|
319
328
|
const {
|
|
@@ -350,6 +359,7 @@ function parsePhoneNumber(
|
|
|
350
359
|
return {
|
|
351
360
|
country,
|
|
352
361
|
countryCallingCode,
|
|
362
|
+
countryCallingCodeSource,
|
|
353
363
|
nationalNumber,
|
|
354
364
|
carrierCode
|
|
355
365
|
}
|