1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports["default"] = _default;
|
7 |
|
8 | var _metadata = _interopRequireDefault(require("./metadata"));
|
9 |
|
10 | var _format_ = _interopRequireDefault(require("./format_"));
|
11 |
|
12 | var _getNumberType = _interopRequireDefault(require("./helpers/getNumberType"));
|
13 |
|
14 | var _checkNumberLength = _interopRequireDefault(require("./helpers/checkNumberLength"));
|
15 |
|
16 | var _getCountryCallingCode = _interopRequireDefault(require("./getCountryCallingCode"));
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
19 |
|
20 | // This function is copy-pasted from
|
21 | // https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js
|
22 | // It hasn't been tested.
|
23 | // Carriers codes aren't part of this library.
|
24 | // Send a PR if you want to add them.
|
25 | var REGION_CODE_FOR_NON_GEO_ENTITY = '001';
|
26 | /**
|
27 | * The prefix that needs to be inserted in front of a Colombian landline number
|
28 | * when dialed from a mobile phone in Colombia.
|
29 | */
|
30 |
|
31 | var COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = '3';
|
32 | /**
|
33 | * Returns a number formatted in such a way that it can be dialed from a mobile
|
34 | * phone in a specific region. If the number cannot be reached from the region
|
35 | * (e.g. some countries block toll-free numbers from being called outside of the
|
36 | * country), the method returns an empty string.
|
37 | *
|
38 | * @param {object} number - a `parse()`d phone number to be formatted.
|
39 | * @param {string} from_country - the region where the call is being placed.
|
40 | * @param {boolean} with_formatting - whether the number should be returned with
|
41 | * formatting symbols, such as spaces and dashes.
|
42 | * @return {string}
|
43 | */
|
44 |
|
45 | function _default(number, from_country, with_formatting, metadata) {
|
46 | metadata = new _metadata["default"](metadata); // Validate `from_country`.
|
47 |
|
48 | if (!metadata.hasCountry(from_country)) {
|
49 | throw new Error("Unknown country: ".concat(from_country));
|
50 | } // Not using the extension, as that part cannot normally be dialed
|
51 | // together with the main number.
|
52 |
|
53 |
|
54 | number = {
|
55 | phone: number.phone,
|
56 | country: number.country
|
57 | };
|
58 | var number_type = (0, _getNumberType["default"])(number, undefined, metadata.metadata);
|
59 | var is_valid_number = number_type === number;
|
60 | var formatted_number;
|
61 |
|
62 | if (country === from_country) {
|
63 | var is_fixed_line_or_mobile = number_type === 'FIXED_LINE' || number_type === 'MOBILE' || number_type === 'FIXED_LINE_OR_MOBILE'; // Carrier codes may be needed in some countries. We handle this here.
|
64 |
|
65 | if (country === 'CO' && number_type === 'FIXED_LINE') {
|
66 | formatted_number = formatNationalNumberWithCarrierCode(number, COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
|
67 | } else if (country == 'BR' && is_fixed_line_or_mobile) {
|
68 | formatted_number = carrierCode ? formatNationalNumberWithPreferredCarrierCode(number) : // Brazilian fixed line and mobile numbers need to be dialed with a
|
69 | // carrier code when called within Brazil. Without that, most of the
|
70 | // carriers won't connect the call. Because of that, we return an
|
71 | // empty string here.
|
72 | '';
|
73 | } else if ((0, _getCountryCallingCode["default"])(country, metadata.metadata) === '1') {
|
74 | // For NANPA countries, we output international format for numbers that
|
75 | // can be dialed internationally, since that always works, except for
|
76 | // numbers which might potentially be short numbers, which are always
|
77 | // dialled in national format.
|
78 | // Select country for `checkNumberLength()`.
|
79 | metadata.country(country);
|
80 |
|
81 | if (can_be_internationally_dialled(number) && (0, _checkNumberLength["default"])(number.phone, metadata) !== 'TOO_SHORT') {
|
82 | formatted_number = (0, _format_["default"])(number, 'INTERNATIONAL', metadata.metadata);
|
83 | } else {
|
84 | formatted_number = (0, _format_["default"])(number, 'NATIONAL', metadata.metadata);
|
85 | }
|
86 | } else {
|
87 | // For non-geographic countries, Mexican and Chilean fixed line and
|
88 | // mobile numbers, we output international format for numbers that can be
|
89 | // dialed internationally, as that always works.
|
90 | if ((country === REGION_CODE_FOR_NON_GEO_ENTITY || // MX fixed line and mobile numbers should always be formatted in
|
91 | // international format, even when dialed within MX. For national
|
92 | // format to work, a carrier code needs to be used, and the correct
|
93 | // carrier code depends on if the caller and callee are from the
|
94 | // same local area. It is trickier to get that to work correctly than
|
95 | // using international format, which is tested to work fine on all
|
96 | // carriers.
|
97 | //
|
98 | // CL fixed line numbers need the national prefix when dialing in the
|
99 | // national format, but don't have it when used for display. The
|
100 | // reverse is true for mobile numbers. As a result, we output them in
|
101 | // the international format to make it work.
|
102 | //
|
103 | // UZ mobile and fixed-line numbers have to be formatted in
|
104 | // international format or prefixed with special codes like 03, 04
|
105 | // (for fixed-line) and 05 (for mobile) for dialling successfully
|
106 | // from mobile devices. As we do not have complete information on
|
107 | // special codes and to be consistent with formatting across all
|
108 | // phone types we return the number in international format here.
|
109 | //
|
110 | (country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile) && can_be_internationally_dialled(number)) {
|
111 | formatted_number = (0, _format_["default"])(number, 'INTERNATIONAL');
|
112 | } else {
|
113 | formatted_number = (0, _format_["default"])(number, 'NATIONAL');
|
114 | }
|
115 | }
|
116 | } else if (is_valid_number && can_be_internationally_dialled(number)) {
|
117 | // We assume that short numbers are not diallable from outside their region,
|
118 | // so if a number is not a valid regular length phone number, we treat it as
|
119 | // if it cannot be internationally dialled.
|
120 | return with_formatting ? (0, _format_["default"])(number, 'INTERNATIONAL', metadata.metadata) : (0, _format_["default"])(number, 'E.164', metadata.metadata);
|
121 | }
|
122 |
|
123 | if (!with_formatting) {
|
124 | return diallable_chars(formatted_number);
|
125 | }
|
126 |
|
127 | return formatted_number;
|
128 | }
|
129 |
|
130 | function can_be_internationally_dialled(number) {
|
131 | return true;
|
132 | }
|
133 | /**
|
134 | * A map that contains characters that are essential when dialling. That means
|
135 | * any of the characters in this map must not be removed from a number when
|
136 | * dialling, otherwise the call will not reach the intended destination.
|
137 | */
|
138 |
|
139 |
|
140 | var DIALLABLE_CHARACTERS = {
|
141 | '0': '0',
|
142 | '1': '1',
|
143 | '2': '2',
|
144 | '3': '3',
|
145 | '4': '4',
|
146 | '5': '5',
|
147 | '6': '6',
|
148 | '7': '7',
|
149 | '8': '8',
|
150 | '9': '9',
|
151 | '+': '+',
|
152 | '*': '*',
|
153 | '#': '#'
|
154 | };
|
155 |
|
156 | function diallable_chars(formatted_number) {
|
157 | var result = '';
|
158 | var i = 0;
|
159 |
|
160 | while (i < formatted_number.length) {
|
161 | var character = formatted_number[i];
|
162 |
|
163 | if (DIALLABLE_CHARACTERS[character]) {
|
164 | result += character;
|
165 | }
|
166 |
|
167 | i++;
|
168 | }
|
169 |
|
170 | return result;
|
171 | }
|
172 |
|
173 | function getPreferredDomesticCarrierCodeOrDefault() {
|
174 | throw new Error('carrier codes are not part of this library');
|
175 | }
|
176 |
|
177 | function formatNationalNumberWithCarrierCode() {
|
178 | throw new Error('carrier codes are not part of this library');
|
179 | }
|
180 | /**
|
181 | * Formats a phone number in national format for dialing using the carrier as
|
182 | * specified in the preferred_domestic_carrier_code field of the PhoneNumber
|
183 | * object passed in. If that is missing, use the {@code fallbackCarrierCode}
|
184 | * passed in instead. If there is no {@code preferred_domestic_carrier_code},
|
185 | * and the {@code fallbackCarrierCode} contains an empty string, return the
|
186 | * number in national format without any carrier code.
|
187 | *
|
188 | * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier
|
189 | * code passed in should take precedence over the number's
|
190 | * {@code preferred_domestic_carrier_code} when formatting.
|
191 | *
|
192 | * @param {i18n.phonenumbers.PhoneNumber} number the phone number to be
|
193 | * formatted.
|
194 | * @param {string} fallbackCarrierCode the carrier selection code to be used, if
|
195 | * none is found in the phone number itself.
|
196 | * @return {string} the formatted phone number in national format for dialing
|
197 | * using the number's preferred_domestic_carrier_code, or the
|
198 | * {@code fallbackCarrierCode} passed in if none is found.
|
199 | */
|
200 |
|
201 |
|
202 | function formatNationalNumberWithPreferredCarrierCode(number) {
|
203 | return formatNationalNumberWithCarrierCode(number, carrierCode);
|
204 | }
|
205 | //# sourceMappingURL=formatNumberForMobileDialing.js.map |
\ | No newline at end of file |