ingeniuscliq-core 0.5.37 → 0.5.38
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/dist/components/common/form/FormPhone.d.ts +3 -1
- package/dist/components/common/form/FormPhone.d.ts.map +1 -1
- package/dist/components/common/form/FormPhone.js +24 -4
- package/dist/node_modules/libphonenumber-js/es6/ParseError.js +34 -0
- package/dist/node_modules/libphonenumber-js/es6/PhoneNumber.js +194 -0
- package/dist/node_modules/libphonenumber-js/es6/constants.js +32 -0
- package/dist/node_modules/libphonenumber-js/es6/format.js +208 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/RFC3966.js +17 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/applyInternationalSeparatorStyle.js +37 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/checkNumberLength.js +82 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extension/createExtensionPattern.js +96 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extension/extractExtension.js +31 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extractCountryCallingCode.js +142 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +47 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +66 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extractNationalNumber.js +117 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +103 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/extractPhoneContext.js +82 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/formatNationalNumberUsingFormat.js +32 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/getCountryByCallingCode.js +23 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/getCountryByNationalNumber.js +81 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/getIddPrefix.js +27 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/getNumberType.js +86 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/getPossibleCountriesForNumber.js +29 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/isViablePhoneNumber.js +78 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/matchesEntirely.js +13 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/parseDigits.js +82 -0
- package/dist/node_modules/libphonenumber-js/es6/helpers/stripIddPrefix.js +31 -0
- package/dist/node_modules/libphonenumber-js/es6/isPossible.js +78 -0
- package/dist/node_modules/libphonenumber-js/es6/isValid.js +58 -0
- package/dist/node_modules/libphonenumber-js/es6/isValidPhoneNumber.js +22 -0
- package/dist/node_modules/libphonenumber-js/es6/metadata.js +6 -1
- package/dist/node_modules/libphonenumber-js/es6/normalizeArguments.js +70 -0
- package/dist/node_modules/libphonenumber-js/es6/parse.js +326 -0
- package/dist/node_modules/libphonenumber-js/es6/parseIncompletePhoneNumber.js +62 -0
- package/dist/node_modules/libphonenumber-js/es6/parsePhoneNumberWithError_.js +15 -0
- package/dist/node_modules/libphonenumber-js/es6/parsePhoneNumber_.js +29 -0
- package/dist/node_modules/libphonenumber-js/min/exports/isValidPhoneNumber.js +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import Metadata from '../metadata.js';
|
|
2
|
+
|
|
3
|
+
function checkNumberLength(nationalNumber, country, metadata) {
|
|
4
|
+
return checkNumberLengthForType(nationalNumber, country, undefined, metadata);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Checks whether a number is possible for a certain `country` based on the number length.
|
|
8
|
+
//
|
|
9
|
+
// This function is not supported by metadata generated with ancient versions of
|
|
10
|
+
// `libphonenumber-js` (before version `1.0.18`) which didn't include "possible lengths".
|
|
11
|
+
//
|
|
12
|
+
// There was also a known issue with `checkNumberLength()` function:
|
|
13
|
+
// if a number is possible only in a certain `country` among several `countries`
|
|
14
|
+
// that share the same "country calling code", that function would check
|
|
15
|
+
// the possibility of the phone number only in the "main" `country` for the "country calling code"
|
|
16
|
+
// and would not check if it's actually be possible in the speciifc `country`.
|
|
17
|
+
//
|
|
18
|
+
// For example, "+1310xxxx" numbers are valid in Canada.
|
|
19
|
+
// However, they are not possible in the US due to being too short.
|
|
20
|
+
// Since Canada and the US share the same country calling code — "+1" —
|
|
21
|
+
// `checkNumberLength()` function used to return not "IS_POSSIBLE" for "+1310xxxx" numbers.
|
|
22
|
+
//
|
|
23
|
+
// In such cases, when using "/max" metadata, `isValid()` could output `true`
|
|
24
|
+
// but at the same time `isPossible()` could output `false`, which was contradictory.
|
|
25
|
+
//
|
|
26
|
+
// See https://issuetracker.google.com/issues/335892662 for the discusson in Google's issues.
|
|
27
|
+
//
|
|
28
|
+
// The solution suggested by Google was implemented: an optional `country` argument
|
|
29
|
+
// was added to `checkNumberLength()` function. If present, that `country` will be used
|
|
30
|
+
// to check phone number length for that specific `country` rather than the "main" country
|
|
31
|
+
// for the shared "country calling code".
|
|
32
|
+
//
|
|
33
|
+
function checkNumberLengthForType(nationalNumber, country, type, metadata) {
|
|
34
|
+
// If the exact `country` is specified, it's no necessarily already selected in `metadata`.
|
|
35
|
+
// Most likely, in cases when there're multiple countries corresponding to the same
|
|
36
|
+
// "country calling code", the "main" country for that "country calling code" will be selected.
|
|
37
|
+
if (country) {
|
|
38
|
+
metadata = new Metadata(metadata.metadata);
|
|
39
|
+
metadata.selectNumberingPlan(country);
|
|
40
|
+
}
|
|
41
|
+
var type_info = metadata.type(type);
|
|
42
|
+
|
|
43
|
+
// There should always be "<possiblePengths/>" set for every type element.
|
|
44
|
+
// This is declared in the XML schema.
|
|
45
|
+
// For size efficiency, where a sub-description (e.g. fixed-line)
|
|
46
|
+
// has the same "<possiblePengths/>" as the "general description", this is missing,
|
|
47
|
+
// so we fall back to the "general description". Where no numbers of the type
|
|
48
|
+
// exist at all, there is one possible length (-1) which is guaranteed
|
|
49
|
+
// not to match the length of any real phone number.
|
|
50
|
+
var possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths();
|
|
51
|
+
// let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()
|
|
52
|
+
|
|
53
|
+
// Metadata before version `1.0.18` didn't contain `possible_lengths`.
|
|
54
|
+
if (!possible_lengths) {
|
|
55
|
+
return 'IS_POSSIBLE';
|
|
56
|
+
}
|
|
57
|
+
var actual_length = nationalNumber.length;
|
|
58
|
+
|
|
59
|
+
// In `libphonenumber-js` all "local-only" formats are dropped for simplicity.
|
|
60
|
+
// // This is safe because there is never an overlap beween the possible lengths
|
|
61
|
+
// // and the local-only lengths; this is checked at build time.
|
|
62
|
+
// if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)
|
|
63
|
+
// {
|
|
64
|
+
// return 'IS_POSSIBLE_LOCAL_ONLY'
|
|
65
|
+
// }
|
|
66
|
+
|
|
67
|
+
var minimum_length = possible_lengths[0];
|
|
68
|
+
if (minimum_length === actual_length) {
|
|
69
|
+
return 'IS_POSSIBLE';
|
|
70
|
+
}
|
|
71
|
+
if (minimum_length > actual_length) {
|
|
72
|
+
return 'TOO_SHORT';
|
|
73
|
+
}
|
|
74
|
+
if (possible_lengths[possible_lengths.length - 1] < actual_length) {
|
|
75
|
+
return 'TOO_LONG';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// We skip the first element since we've already checked it.
|
|
79
|
+
return possible_lengths.indexOf(actual_length, 1) >= 0 ? 'IS_POSSIBLE' : 'INVALID_LENGTH';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { checkNumberLengthForType, checkNumberLength as default };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { VALID_DIGITS } from '../../constants.js';
|
|
2
|
+
|
|
3
|
+
// The RFC 3966 format for extensions.
|
|
4
|
+
var RFC3966_EXTN_PREFIX = ';ext=';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Helper method for constructing regular expressions for parsing. Creates
|
|
8
|
+
* an expression that captures up to max_length digits.
|
|
9
|
+
* @return {string} RegEx pattern to capture extension digits.
|
|
10
|
+
*/
|
|
11
|
+
var getExtensionDigitsPattern = function getExtensionDigitsPattern(maxLength) {
|
|
12
|
+
return "([".concat(VALID_DIGITS, "]{1,").concat(maxLength, "})");
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Helper initialiser method to create the regular-expression pattern to match
|
|
17
|
+
* extensions.
|
|
18
|
+
* Copy-pasted from Google's `libphonenumber`:
|
|
19
|
+
* https://github.com/google/libphonenumber/blob/55b2646ec9393f4d3d6661b9c82ef9e258e8b829/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L766
|
|
20
|
+
* @return {string} RegEx pattern to capture extensions.
|
|
21
|
+
*/
|
|
22
|
+
function createExtensionPattern(purpose) {
|
|
23
|
+
// We cap the maximum length of an extension based on the ambiguity of the way
|
|
24
|
+
// the extension is prefixed. As per ITU, the officially allowed length for
|
|
25
|
+
// extensions is actually 40, but we don't support this since we haven't seen real
|
|
26
|
+
// examples and this introduces many false interpretations as the extension labels
|
|
27
|
+
// are not standardized.
|
|
28
|
+
/** @type {string} */
|
|
29
|
+
var extLimitAfterExplicitLabel = '20';
|
|
30
|
+
/** @type {string} */
|
|
31
|
+
var extLimitAfterLikelyLabel = '15';
|
|
32
|
+
/** @type {string} */
|
|
33
|
+
var extLimitAfterAmbiguousChar = '9';
|
|
34
|
+
/** @type {string} */
|
|
35
|
+
var extLimitWhenNotSure = '6';
|
|
36
|
+
|
|
37
|
+
/** @type {string} */
|
|
38
|
+
var possibleSeparatorsBetweenNumberAndExtLabel = "[ \xA0\\t,]*";
|
|
39
|
+
// Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas.
|
|
40
|
+
/** @type {string} */
|
|
41
|
+
var possibleCharsAfterExtLabel = "[:\\.\uFF0E]?[ \xA0\\t,-]*";
|
|
42
|
+
/** @type {string} */
|
|
43
|
+
var optionalExtnSuffix = "#?";
|
|
44
|
+
|
|
45
|
+
// Here the extension is called out in more explicit way, i.e mentioning it obvious
|
|
46
|
+
// patterns like "ext.".
|
|
47
|
+
/** @type {string} */
|
|
48
|
+
var explicitExtLabels = "(?:e?xt(?:ensi(?:o\u0301?|\xF3))?n?|\uFF45?\uFF58\uFF54\uFF4E?|\u0434\u043E\u0431|anexo)";
|
|
49
|
+
// One-character symbols that can be used to indicate an extension, and less
|
|
50
|
+
// commonly used or more ambiguous extension labels.
|
|
51
|
+
/** @type {string} */
|
|
52
|
+
var ambiguousExtLabels = "(?:[x\uFF58#\uFF03~\uFF5E]|int|\uFF49\uFF4E\uFF54)";
|
|
53
|
+
// When extension is not separated clearly.
|
|
54
|
+
/** @type {string} */
|
|
55
|
+
var ambiguousSeparator = "[- ]+";
|
|
56
|
+
// This is the same as possibleSeparatorsBetweenNumberAndExtLabel, but not matching
|
|
57
|
+
// comma as extension label may have it.
|
|
58
|
+
/** @type {string} */
|
|
59
|
+
var possibleSeparatorsNumberExtLabelNoComma = "[ \xA0\\t]*";
|
|
60
|
+
// ",," is commonly used for auto dialling the extension when connected. First
|
|
61
|
+
// comma is matched through possibleSeparatorsBetweenNumberAndExtLabel, so we do
|
|
62
|
+
// not repeat it here. Semi-colon works in Iphone and Android also to pop up a
|
|
63
|
+
// button with the extension number following.
|
|
64
|
+
/** @type {string} */
|
|
65
|
+
var autoDiallingAndExtLabelsFound = "(?:,{2}|;)";
|
|
66
|
+
|
|
67
|
+
/** @type {string} */
|
|
68
|
+
var rfcExtn = RFC3966_EXTN_PREFIX + getExtensionDigitsPattern(extLimitAfterExplicitLabel);
|
|
69
|
+
/** @type {string} */
|
|
70
|
+
var explicitExtn = possibleSeparatorsBetweenNumberAndExtLabel + explicitExtLabels + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterExplicitLabel) + optionalExtnSuffix;
|
|
71
|
+
/** @type {string} */
|
|
72
|
+
var ambiguousExtn = possibleSeparatorsBetweenNumberAndExtLabel + ambiguousExtLabels + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterAmbiguousChar) + optionalExtnSuffix;
|
|
73
|
+
/** @type {string} */
|
|
74
|
+
var americanStyleExtnWithSuffix = ambiguousSeparator + getExtensionDigitsPattern(extLimitWhenNotSure) + "#";
|
|
75
|
+
|
|
76
|
+
/** @type {string} */
|
|
77
|
+
var autoDiallingExtn = possibleSeparatorsNumberExtLabelNoComma + autoDiallingAndExtLabelsFound + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterLikelyLabel) + optionalExtnSuffix;
|
|
78
|
+
/** @type {string} */
|
|
79
|
+
var onlyCommasExtn = possibleSeparatorsNumberExtLabelNoComma + "(?:,)+" + possibleCharsAfterExtLabel + getExtensionDigitsPattern(extLimitAfterAmbiguousChar) + optionalExtnSuffix;
|
|
80
|
+
|
|
81
|
+
// The first regular expression covers RFC 3966 format, where the extension is added
|
|
82
|
+
// using ";ext=". The second more generic where extension is mentioned with explicit
|
|
83
|
+
// labels like "ext:". In both the above cases we allow more numbers in extension than
|
|
84
|
+
// any other extension labels. The third one captures when single character extension
|
|
85
|
+
// labels or less commonly used labels are used. In such cases we capture fewer
|
|
86
|
+
// extension digits in order to reduce the chance of falsely interpreting two
|
|
87
|
+
// numbers beside each other as a number + extension. The fourth one covers the
|
|
88
|
+
// special case of American numbers where the extension is written with a hash
|
|
89
|
+
// at the end, such as "- 503#". The fifth one is exclusively for extension
|
|
90
|
+
// autodialling formats which are used when dialling and in this case we accept longer
|
|
91
|
+
// extensions. The last one is more liberal on the number of commas that acts as
|
|
92
|
+
// extension labels, so we have a strict cap on the number of digits in such extensions.
|
|
93
|
+
return rfcExtn + "|" + explicitExtn + "|" + ambiguousExtn + "|" + americanStyleExtnWithSuffix + "|" + autoDiallingExtn + "|" + onlyCommasExtn;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { createExtensionPattern as default };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import createExtensionPattern from './createExtensionPattern.js';
|
|
2
|
+
|
|
3
|
+
// Regexp of all known extension prefixes used by different regions followed by
|
|
4
|
+
// 1 or more valid digits, for use when parsing.
|
|
5
|
+
var EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i');
|
|
6
|
+
|
|
7
|
+
// Strips any extension (as in, the part of the number dialled after the call is
|
|
8
|
+
// connected, usually indicated with extn, ext, x or similar) from the end of
|
|
9
|
+
// the number, and returns it.
|
|
10
|
+
function extractExtension(number) {
|
|
11
|
+
var start = number.search(EXTN_PATTERN);
|
|
12
|
+
if (start < 0) {
|
|
13
|
+
return {};
|
|
14
|
+
}
|
|
15
|
+
// If we find a potential extension, and the number preceding this is a viable
|
|
16
|
+
// number, we assume it is an extension.
|
|
17
|
+
var numberWithoutExtension = number.slice(0, start);
|
|
18
|
+
var matches = number.match(EXTN_PATTERN);
|
|
19
|
+
var i = 1;
|
|
20
|
+
while (i < matches.length) {
|
|
21
|
+
if (matches[i]) {
|
|
22
|
+
return {
|
|
23
|
+
number: numberWithoutExtension,
|
|
24
|
+
ext: matches[i]
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
i++;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { extractExtension as default };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import stripIddPrefix from './stripIddPrefix.js';
|
|
2
|
+
import extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js';
|
|
3
|
+
import Metadata from '../metadata.js';
|
|
4
|
+
import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a phone number digits (possibly with a `+`)
|
|
8
|
+
* into a calling code and the rest phone number digits.
|
|
9
|
+
* The "rest phone number digits" could include
|
|
10
|
+
* a national prefix, carrier code, and national
|
|
11
|
+
* (significant) number.
|
|
12
|
+
* @param {string} number — Phone number digits (possibly with a `+`).
|
|
13
|
+
* @param {string} [country] — Country.
|
|
14
|
+
* @param {string} [defaultCountry] — Default country.
|
|
15
|
+
* @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).
|
|
16
|
+
* @param {object} metadata
|
|
17
|
+
* @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
|
|
18
|
+
* @example
|
|
19
|
+
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
|
|
20
|
+
* extractCountryCallingCode('2133734253', null, 'US', null, metadata)
|
|
21
|
+
* extractCountryCallingCode('2133734253', null, null, '1', metadata)
|
|
22
|
+
* extractCountryCallingCode('+12133734253', null, null, null, metadata)
|
|
23
|
+
* extractCountryCallingCode('+12133734253', null, 'RU', null, metadata)
|
|
24
|
+
*/
|
|
25
|
+
function extractCountryCallingCode(number, country, defaultCountry, defaultCallingCode, metadata) {
|
|
26
|
+
if (!number) {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
var isNumberWithIddPrefix;
|
|
30
|
+
|
|
31
|
+
// If this is not an international phone number,
|
|
32
|
+
// then either extract an "IDD" prefix, or extract a
|
|
33
|
+
// country calling code from a number by autocorrecting it
|
|
34
|
+
// by prepending a leading `+` in cases when it starts
|
|
35
|
+
// with the country calling code.
|
|
36
|
+
// https://wikitravel.org/en/International_dialling_prefix
|
|
37
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
38
|
+
if (number[0] !== '+') {
|
|
39
|
+
// Convert an "out-of-country" dialing phone number
|
|
40
|
+
// to a proper international phone number.
|
|
41
|
+
var numberWithoutIDD = stripIddPrefix(number, defaultCountry, defaultCallingCode, metadata);
|
|
42
|
+
// If an IDD prefix was stripped then
|
|
43
|
+
// convert the number to international one
|
|
44
|
+
// for subsequent parsing.
|
|
45
|
+
if (numberWithoutIDD && numberWithoutIDD !== number) {
|
|
46
|
+
isNumberWithIddPrefix = true;
|
|
47
|
+
number = '+' + numberWithoutIDD;
|
|
48
|
+
} else {
|
|
49
|
+
// Check to see if the number starts with the country calling code
|
|
50
|
+
// for the default country. If so, we remove the country calling code,
|
|
51
|
+
// and do some checks on the validity of the number before and after.
|
|
52
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
53
|
+
if (defaultCountry || defaultCallingCode) {
|
|
54
|
+
var _extractCountryCallin = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode, metadata),
|
|
55
|
+
countryCallingCode = _extractCountryCallin.countryCallingCode,
|
|
56
|
+
shorterNumber = _extractCountryCallin.number;
|
|
57
|
+
if (countryCallingCode) {
|
|
58
|
+
return {
|
|
59
|
+
countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
|
|
60
|
+
countryCallingCode: countryCallingCode,
|
|
61
|
+
number: shorterNumber
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
// No need to set it to `UNSPECIFIED`. It can be just `undefined`.
|
|
67
|
+
// countryCallingCodeSource: 'UNSPECIFIED',
|
|
68
|
+
number: number
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// `number` can only be international at this point.
|
|
74
|
+
|
|
75
|
+
// Fast abortion: country codes do not begin with a '0'
|
|
76
|
+
if (number[1] === '0') {
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
79
|
+
metadata = new Metadata(metadata);
|
|
80
|
+
|
|
81
|
+
// The thing with country phone codes
|
|
82
|
+
// is that they are orthogonal to each other
|
|
83
|
+
// i.e. there's no such country phone code A
|
|
84
|
+
// for which country phone code B exists
|
|
85
|
+
// where B starts with A.
|
|
86
|
+
// Therefore, while scanning digits,
|
|
87
|
+
// if a valid country code is found,
|
|
88
|
+
// that means that it is the country code.
|
|
89
|
+
//
|
|
90
|
+
var i = 2;
|
|
91
|
+
while (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
|
|
92
|
+
var _countryCallingCode = number.slice(1, i);
|
|
93
|
+
if (metadata.hasCallingCode(_countryCallingCode)) {
|
|
94
|
+
metadata.selectNumberingPlan(_countryCallingCode);
|
|
95
|
+
return {
|
|
96
|
+
countryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',
|
|
97
|
+
countryCallingCode: _countryCallingCode,
|
|
98
|
+
number: number.slice(i)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
return {};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// The possible values for the returned `countryCallingCodeSource` are:
|
|
107
|
+
//
|
|
108
|
+
// Copy-pasted from:
|
|
109
|
+
// https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto
|
|
110
|
+
//
|
|
111
|
+
// // The source from which the country_code is derived. This is not set in the
|
|
112
|
+
// // general parsing method, but in the method that parses and keeps raw_input.
|
|
113
|
+
// // New fields could be added upon request.
|
|
114
|
+
// enum CountryCodeSource {
|
|
115
|
+
// // Default value returned if this is not set, because the phone number was
|
|
116
|
+
// // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will
|
|
117
|
+
// // return false if this is the case.
|
|
118
|
+
// UNSPECIFIED = 0;
|
|
119
|
+
//
|
|
120
|
+
// // The country_code is derived based on a phone number with a leading "+",
|
|
121
|
+
// // e.g. the French number "+33 1 42 68 53 00".
|
|
122
|
+
// FROM_NUMBER_WITH_PLUS_SIGN = 1;
|
|
123
|
+
//
|
|
124
|
+
// // The country_code is derived based on a phone number with a leading IDD,
|
|
125
|
+
// // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
|
|
126
|
+
// FROM_NUMBER_WITH_IDD = 5;
|
|
127
|
+
//
|
|
128
|
+
// // The country_code is derived based on a phone number without a leading
|
|
129
|
+
// // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
|
|
130
|
+
// // supplied as France.
|
|
131
|
+
// FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
|
|
132
|
+
//
|
|
133
|
+
// // The country_code is derived NOT based on the phone number itself, but
|
|
134
|
+
// // from the defaultCountry parameter provided in the parsing function by the
|
|
135
|
+
// // clients. This happens mostly for numbers written in the national format
|
|
136
|
+
// // (without country code). For example, this would be set when parsing the
|
|
137
|
+
// // French number "01 42 68 53 00", when defaultCountry is supplied as
|
|
138
|
+
// // France.
|
|
139
|
+
// FROM_DEFAULT_COUNTRY = 20;
|
|
140
|
+
// }
|
|
141
|
+
|
|
142
|
+
export { extractCountryCallingCode as default };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Metadata, { getCountryCallingCode } from '../metadata.js';
|
|
2
|
+
import matchesEntirely from './matchesEntirely.js';
|
|
3
|
+
import extractNationalNumber from './extractNationalNumber.js';
|
|
4
|
+
import checkNumberLength from './checkNumberLength.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sometimes some people incorrectly input international phone numbers
|
|
8
|
+
* without the leading `+`. This function corrects such input.
|
|
9
|
+
* @param {string} number — Phone number digits.
|
|
10
|
+
* @param {string} [country] — Exact country of the phone number.
|
|
11
|
+
* @param {string} [defaultCountry]
|
|
12
|
+
* @param {string} [defaultCallingCode]
|
|
13
|
+
* @param {object} metadata
|
|
14
|
+
* @return {object} `{ countryCallingCode: string?, number: string }`.
|
|
15
|
+
*/
|
|
16
|
+
function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode, metadata) {
|
|
17
|
+
var countryCallingCode = defaultCountry ? getCountryCallingCode(defaultCountry, metadata) : defaultCallingCode;
|
|
18
|
+
if (number.indexOf(countryCallingCode) === 0) {
|
|
19
|
+
metadata = new Metadata(metadata);
|
|
20
|
+
metadata.selectNumberingPlan(defaultCountry, countryCallingCode);
|
|
21
|
+
var possibleShorterNumber = number.slice(countryCallingCode.length);
|
|
22
|
+
var _extractNationalNumbe = extractNationalNumber(possibleShorterNumber, country, metadata),
|
|
23
|
+
possibleShorterNationalNumber = _extractNationalNumbe.nationalNumber;
|
|
24
|
+
var _extractNationalNumbe2 = extractNationalNumber(number, country, metadata),
|
|
25
|
+
nationalNumber = _extractNationalNumbe2.nationalNumber;
|
|
26
|
+
|
|
27
|
+
// If the number was not valid before but is valid now,
|
|
28
|
+
// or if it was too long before, we consider the number
|
|
29
|
+
// with the country calling code stripped to be a better result
|
|
30
|
+
// and keep that instead.
|
|
31
|
+
// For example, in Germany (+49), `49` is a valid area code,
|
|
32
|
+
// so if a number starts with `49`, it could be both a valid
|
|
33
|
+
// national German number or an international number without
|
|
34
|
+
// a leading `+`.
|
|
35
|
+
if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) && matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || checkNumberLength(nationalNumber, country, metadata) === 'TOO_LONG') {
|
|
36
|
+
return {
|
|
37
|
+
countryCallingCode: countryCallingCode,
|
|
38
|
+
number: possibleShorterNumber
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
number: number
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { extractCountryCallingCodeFromInternationalNumberWithoutPlusSign as default };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import extractPhoneContext, { isPhoneContextValid, PLUS_SIGN, RFC3966_PREFIX_, RFC3966_PHONE_CONTEXT_, RFC3966_ISDN_SUBADDRESS_ } from './extractPhoneContext.js';
|
|
2
|
+
import ParseError from '../ParseError.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} numberToParse
|
|
6
|
+
* @param {string} nationalNumber
|
|
7
|
+
* @return {}
|
|
8
|
+
*/
|
|
9
|
+
function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, _ref) {
|
|
10
|
+
var extractFormattedPhoneNumber = _ref.extractFormattedPhoneNumber;
|
|
11
|
+
var phoneContext = extractPhoneContext(numberToParse);
|
|
12
|
+
if (!isPhoneContextValid(phoneContext)) {
|
|
13
|
+
throw new ParseError('NOT_A_NUMBER');
|
|
14
|
+
}
|
|
15
|
+
var phoneNumberString;
|
|
16
|
+
if (phoneContext === null) {
|
|
17
|
+
// Extract a possible number from the string passed in.
|
|
18
|
+
// (this strips leading characters that could not be the start of a phone number)
|
|
19
|
+
phoneNumberString = extractFormattedPhoneNumber(numberToParse) || '';
|
|
20
|
+
} else {
|
|
21
|
+
phoneNumberString = '';
|
|
22
|
+
|
|
23
|
+
// If the phone context contains a phone number prefix, we need to capture
|
|
24
|
+
// it, whereas domains will be ignored.
|
|
25
|
+
if (phoneContext.charAt(0) === PLUS_SIGN) {
|
|
26
|
+
phoneNumberString += phoneContext;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Now append everything between the "tel:" prefix and the phone-context.
|
|
30
|
+
// This should include the national number, an optional extension or
|
|
31
|
+
// isdn-subaddress component. Note we also handle the case when "tel:" is
|
|
32
|
+
// missing, as we have seen in some of the phone number inputs.
|
|
33
|
+
// In that case, we append everything from the beginning.
|
|
34
|
+
var indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_);
|
|
35
|
+
var indexOfNationalNumber;
|
|
36
|
+
// RFC 3966 "tel:" prefix is preset at this stage because
|
|
37
|
+
// `isPhoneContextValid()` requires it to be present.
|
|
38
|
+
/* istanbul ignore else */
|
|
39
|
+
if (indexOfRfc3966Prefix >= 0) {
|
|
40
|
+
indexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length;
|
|
41
|
+
} else {
|
|
42
|
+
indexOfNationalNumber = 0;
|
|
43
|
+
}
|
|
44
|
+
var indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_);
|
|
45
|
+
phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Delete the isdn-subaddress and everything after it if it is present.
|
|
49
|
+
// Note extension won't appear at the same time with isdn-subaddress
|
|
50
|
+
// according to paragraph 5.3 of the RFC3966 spec.
|
|
51
|
+
var indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_);
|
|
52
|
+
if (indexOfIsdn > 0) {
|
|
53
|
+
phoneNumberString = phoneNumberString.substring(0, indexOfIsdn);
|
|
54
|
+
}
|
|
55
|
+
// If both phone context and isdn-subaddress are absent but other
|
|
56
|
+
// parameters are present, the parameters are left in nationalNumber.
|
|
57
|
+
// This is because we are concerned about deleting content from a potential
|
|
58
|
+
// number string when there is no strong evidence that the number is
|
|
59
|
+
// actually written in RFC3966.
|
|
60
|
+
|
|
61
|
+
if (phoneNumberString !== '') {
|
|
62
|
+
return phoneNumberString;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { extractFormattedPhoneNumberFromPossibleRfc3966NumberUri as default };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js';
|
|
2
|
+
import matchesEntirely from './matchesEntirely.js';
|
|
3
|
+
import checkNumberLength from './checkNumberLength.js';
|
|
4
|
+
import getCountryByCallingCode from './getCountryByCallingCode.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Strips national prefix and carrier code from a complete phone number.
|
|
8
|
+
* The difference from the non-"FromCompleteNumber" function is that
|
|
9
|
+
* it won't extract national prefix if the resultant number is too short
|
|
10
|
+
* to be a complete number for the selected phone numbering plan.
|
|
11
|
+
* @param {string} number — Complete phone number digits.
|
|
12
|
+
* @param {string?} country — Country, if known.
|
|
13
|
+
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
|
|
14
|
+
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
|
|
15
|
+
*/
|
|
16
|
+
function extractNationalNumber(number, country, metadata) {
|
|
17
|
+
// Parsing national prefixes and carrier codes
|
|
18
|
+
// is only required for local phone numbers
|
|
19
|
+
// but some people don't understand that
|
|
20
|
+
// and sometimes write international phone numbers
|
|
21
|
+
// with national prefixes (or maybe even carrier codes).
|
|
22
|
+
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
|
|
23
|
+
// Google's original library forgives such mistakes
|
|
24
|
+
// and so does this library, because it has been requested:
|
|
25
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/127
|
|
26
|
+
var _extractNationalNumbe = extractNationalNumberFromPossiblyIncompleteNumber(number, metadata),
|
|
27
|
+
carrierCode = _extractNationalNumbe.carrierCode,
|
|
28
|
+
nationalNumber = _extractNationalNumbe.nationalNumber;
|
|
29
|
+
if (nationalNumber !== number) {
|
|
30
|
+
if (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {
|
|
31
|
+
// Don't strip the national prefix.
|
|
32
|
+
return {
|
|
33
|
+
nationalNumber: number
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// Check the national (significant) number length after extracting national prefix and carrier code.
|
|
37
|
+
// Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
|
|
38
|
+
if (metadata.numberingPlan.possibleLengths()) {
|
|
39
|
+
// If an exact `country` is not specified, attempt to detect it from the assumed national number.
|
|
40
|
+
if (!country) {
|
|
41
|
+
country = getCountryByCallingCode(metadata.numberingPlan.callingCode(), {
|
|
42
|
+
nationalNumber: nationalNumber,
|
|
43
|
+
metadata: metadata
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The number remaining after stripping the national prefix and carrier code
|
|
48
|
+
// should be long enough to have a possible length for the country.
|
|
49
|
+
// Otherwise, don't strip the national prefix and carrier code,
|
|
50
|
+
// since the original number could be a valid number.
|
|
51
|
+
// This check has been copy-pasted "as is" from Google's original library:
|
|
52
|
+
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
|
|
53
|
+
// It doesn't check for the "possibility" of the original `number`.
|
|
54
|
+
// I guess it's fine not checking that one. It works as is anyway.
|
|
55
|
+
if (!isPossibleIncompleteNationalNumber(nationalNumber, country, metadata)) {
|
|
56
|
+
// Don't strip the national prefix.
|
|
57
|
+
return {
|
|
58
|
+
nationalNumber: number
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
nationalNumber: nationalNumber,
|
|
65
|
+
carrierCode: carrierCode
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// In some countries, the same digit could be a national prefix
|
|
70
|
+
// or a leading digit of a valid phone number.
|
|
71
|
+
// For example, in Russia, national prefix is `8`,
|
|
72
|
+
// and also `800 555 35 35` is a valid number
|
|
73
|
+
// in which `8` is not a national prefix, but the first digit
|
|
74
|
+
// of a national (significant) number.
|
|
75
|
+
// Same's with Belarus:
|
|
76
|
+
// `82004910060` is a valid national (significant) number,
|
|
77
|
+
// but `2004910060` is not.
|
|
78
|
+
// To support such cases (to prevent the code from always stripping
|
|
79
|
+
// national prefix), a condition is imposed: a national prefix
|
|
80
|
+
// is not extracted when the original number is "viable" and the
|
|
81
|
+
// resultant number is not, a "viable" national number being the one
|
|
82
|
+
// that matches `national_number_pattern`.
|
|
83
|
+
function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {
|
|
84
|
+
// The equivalent in Google's code is:
|
|
85
|
+
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
|
|
86
|
+
if (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) && !matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
// This "is possible" national number (length) check has been commented out
|
|
90
|
+
// because it's superceded by the (effectively) same check done in the
|
|
91
|
+
// `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.
|
|
92
|
+
// In other words, why run the same check twice if it could only be run once.
|
|
93
|
+
// // Check the national (significant) number length after extracting national prefix and carrier code.
|
|
94
|
+
// // Fixes a minor "weird behavior" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
|
|
95
|
+
// // (Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature).
|
|
96
|
+
// if (metadata.possibleLengths()) {
|
|
97
|
+
// if (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&
|
|
98
|
+
// !isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {
|
|
99
|
+
// return false
|
|
100
|
+
// }
|
|
101
|
+
// }
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
function isPossibleIncompleteNationalNumber(nationalNumber, country, metadata) {
|
|
105
|
+
switch (checkNumberLength(nationalNumber, country, metadata)) {
|
|
106
|
+
case 'TOO_SHORT':
|
|
107
|
+
case 'INVALID_LENGTH':
|
|
108
|
+
// This library ignores "local-only" phone numbers (for simplicity).
|
|
109
|
+
// See the readme for more info on what are "local-only" phone numbers.
|
|
110
|
+
// case 'IS_POSSIBLE_LOCAL_ONLY':
|
|
111
|
+
return false;
|
|
112
|
+
default:
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { extractNationalNumber as default };
|