UNPKG

6.29 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = formatCompleteNumber;
7exports.canFormatCompleteNumber = canFormatCompleteNumber;
8
9var _checkNumberLength = _interopRequireDefault(require("./helpers/checkNumberLength"));
10
11var _parseDigits = _interopRequireDefault(require("./helpers/parseDigits"));
12
13var _formatNationalNumberUsingFormat = _interopRequireDefault(require("./helpers/formatNationalNumberUsingFormat"));
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
16
17function formatCompleteNumber(state, format, _ref) {
18 var metadata = _ref.metadata,
19 shouldTryNationalPrefixFormattingRule = _ref.shouldTryNationalPrefixFormattingRule,
20 getSeparatorAfterNationalPrefix = _ref.getSeparatorAfterNationalPrefix;
21 var matcher = new RegExp("^(?:".concat(format.pattern(), ")$"));
22
23 if (matcher.test(state.nationalSignificantNumber)) {
24 return formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(state, format, {
25 metadata: metadata,
26 shouldTryNationalPrefixFormattingRule: shouldTryNationalPrefixFormattingRule,
27 getSeparatorAfterNationalPrefix: getSeparatorAfterNationalPrefix
28 });
29 }
30}
31
32function canFormatCompleteNumber(nationalSignificantNumber, metadata) {
33 return (0, _checkNumberLength["default"])(nationalSignificantNumber, metadata) === 'IS_POSSIBLE';
34}
35
36function formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(state, format, _ref2) {
37 var metadata = _ref2.metadata,
38 shouldTryNationalPrefixFormattingRule = _ref2.shouldTryNationalPrefixFormattingRule,
39 getSeparatorAfterNationalPrefix = _ref2.getSeparatorAfterNationalPrefix;
40 // `format` has already been checked for `nationalPrefix` requirement.
41 var nationalSignificantNumber = state.nationalSignificantNumber,
42 international = state.international,
43 nationalPrefix = state.nationalPrefix,
44 carrierCode = state.carrierCode; // Format the number with using `national_prefix_formatting_rule`.
45 // If the resulting formatted number is a valid formatted number, then return it.
46 //
47 // Google's AsYouType formatter is different in a way that it doesn't try
48 // to format using the "national prefix formatting rule", and instead it
49 // simply prepends a national prefix followed by a " " character.
50 // This code does that too, but as a fallback.
51 // The reason is that "national prefix formatting rule" may use parentheses,
52 // which wouldn't be included has it used the simpler Google's way.
53 //
54
55 if (shouldTryNationalPrefixFormattingRule(format)) {
56 var formattedNumber = formatNationalNumber(state, format, {
57 useNationalPrefixFormattingRule: true,
58 getSeparatorAfterNationalPrefix: getSeparatorAfterNationalPrefix,
59 metadata: metadata
60 });
61
62 if (formattedNumber) {
63 return formattedNumber;
64 }
65 } // Format the number without using `national_prefix_formatting_rule`.
66
67
68 return formatNationalNumber(state, format, {
69 useNationalPrefixFormattingRule: false,
70 getSeparatorAfterNationalPrefix: getSeparatorAfterNationalPrefix,
71 metadata: metadata
72 });
73}
74
75function formatNationalNumber(state, format, _ref3) {
76 var metadata = _ref3.metadata,
77 useNationalPrefixFormattingRule = _ref3.useNationalPrefixFormattingRule,
78 getSeparatorAfterNationalPrefix = _ref3.getSeparatorAfterNationalPrefix;
79 var formattedNationalNumber = (0, _formatNationalNumberUsingFormat["default"])(state.nationalSignificantNumber, format, {
80 carrierCode: state.carrierCode,
81 useInternationalFormat: state.international,
82 withNationalPrefix: useNationalPrefixFormattingRule,
83 metadata: metadata
84 });
85
86 if (!useNationalPrefixFormattingRule) {
87 if (state.nationalPrefix) {
88 // If a national prefix was extracted, then just prepend it,
89 // followed by a " " character.
90 formattedNationalNumber = state.nationalPrefix + getSeparatorAfterNationalPrefix(format) + formattedNationalNumber;
91 } else if (state.complexPrefixBeforeNationalSignificantNumber) {
92 formattedNationalNumber = state.complexPrefixBeforeNationalSignificantNumber + ' ' + formattedNationalNumber;
93 }
94 }
95
96 if (isValidFormattedNationalNumber(formattedNationalNumber, state)) {
97 return formattedNationalNumber;
98 }
99} // Check that the formatted phone number contains exactly
100// the same digits that have been input by the user.
101// For example, when "0111523456789" is input for `AR` country,
102// the extracted `this.nationalSignificantNumber` is "91123456789",
103// which means that the national part of `this.digits` isn't simply equal to
104// `this.nationalPrefix` + `this.nationalSignificantNumber`.
105//
106// Also, a `format` can add extra digits to the `this.nationalSignificantNumber`
107// being formatted via `metadata[country].national_prefix_transform_rule`.
108// For example, for `VI` country, it prepends `340` to the national number,
109// and if this check hasn't been implemented, then there would be a bug
110// when `340` "area coude" is "duplicated" during input for `VI` country:
111// https://github.com/catamphetamine/libphonenumber-js/issues/318
112//
113// So, all these "gotchas" are filtered out.
114//
115// In the original Google's code, the comments say:
116// "Check that we didn't remove nor add any extra digits when we matched
117// this formatting pattern. This usually happens after we entered the last
118// digit during AYTF. Eg: In case of MX, we swallow mobile token (1) when
119// formatted but AYTF should retain all the number entered and not change
120// in order to match a format (of same leading digits and length) display
121// in that way."
122// "If it's the same (i.e entered number and format is same), then it's
123// safe to return this in formatted number as nothing is lost / added."
124// Otherwise, don't use this format.
125// https://github.com/google/libphonenumber/commit/3e7c1f04f5e7200f87fb131e6f85c6e99d60f510#diff-9149457fa9f5d608a11bb975c6ef4bc5
126// https://github.com/google/libphonenumber/commit/3ac88c7106e7dcb553bcc794b15f19185928a1c6#diff-2dcb77e833422ee304da348b905cde0b
127//
128
129
130function isValidFormattedNationalNumber(formattedNationalNumber, state) {
131 return (0, _parseDigits["default"])(formattedNationalNumber) === state.getNationalDigits();
132}
133//# sourceMappingURL=AsYouTypeFormatter.complete.js.map
\No newline at end of file