mirror of
https://github.com/Expand-sys/ccashfrontend
synced 2025-12-19 16:12:14 +11:00
201 lines
No EOL
8.5 KiB
JavaScript
201 lines
No EOL
8.5 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports["default"] = formatNumber;
|
|
|
|
var _matchesEntirely = _interopRequireDefault(require("./helpers/matchesEntirely"));
|
|
|
|
var _formatNationalNumberUsingFormat = _interopRequireDefault(require("./helpers/formatNationalNumberUsingFormat"));
|
|
|
|
var _metadata = _interopRequireWildcard(require("./metadata"));
|
|
|
|
var _getIddPrefix = _interopRequireDefault(require("./helpers/getIddPrefix"));
|
|
|
|
var _RFC = require("./helpers/RFC3966");
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
|
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
|
|
var DEFAULT_OPTIONS = {
|
|
formatExtension: function formatExtension(formattedNumber, extension, metadata) {
|
|
return "".concat(formattedNumber).concat(metadata.ext()).concat(extension);
|
|
} // Formats a phone number
|
|
//
|
|
// Example use cases:
|
|
//
|
|
// ```js
|
|
// formatNumber('8005553535', 'RU', 'INTERNATIONAL')
|
|
// formatNumber('8005553535', 'RU', 'INTERNATIONAL', metadata)
|
|
// formatNumber({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL')
|
|
// formatNumber({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', metadata)
|
|
// formatNumber('+78005553535', 'NATIONAL')
|
|
// formatNumber('+78005553535', 'NATIONAL', metadata)
|
|
// ```
|
|
//
|
|
|
|
};
|
|
|
|
function formatNumber(input, format, options, metadata) {
|
|
// Apply default options.
|
|
if (options) {
|
|
options = _objectSpread({}, DEFAULT_OPTIONS, options);
|
|
} else {
|
|
options = DEFAULT_OPTIONS;
|
|
}
|
|
|
|
metadata = new _metadata["default"](metadata);
|
|
|
|
if (input.country && input.country !== '001') {
|
|
// Validate `input.country`.
|
|
if (!metadata.hasCountry(input.country)) {
|
|
throw new Error("Unknown country: ".concat(input.country));
|
|
}
|
|
|
|
metadata.country(input.country);
|
|
} else if (input.countryCallingCode) {
|
|
metadata.selectNumberingPlan(input.countryCallingCode);
|
|
} else return input.phone || '';
|
|
|
|
var countryCallingCode = metadata.countryCallingCode();
|
|
var nationalNumber = options.v2 ? input.nationalNumber : input.phone; // This variable should have been declared inside `case`s
|
|
// but Babel has a bug and it says "duplicate variable declaration".
|
|
|
|
var number;
|
|
|
|
switch (format) {
|
|
case 'NATIONAL':
|
|
// Legacy argument support.
|
|
// (`{ country: ..., phone: '' }`)
|
|
if (!nationalNumber) {
|
|
return '';
|
|
}
|
|
|
|
number = formatNationalNumber(nationalNumber, input.carrierCode, 'NATIONAL', metadata, options);
|
|
return addExtension(number, input.ext, metadata, options.formatExtension);
|
|
|
|
case 'INTERNATIONAL':
|
|
// Legacy argument support.
|
|
// (`{ country: ..., phone: '' }`)
|
|
if (!nationalNumber) {
|
|
return "+".concat(countryCallingCode);
|
|
}
|
|
|
|
number = formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata, options);
|
|
number = "+".concat(countryCallingCode, " ").concat(number);
|
|
return addExtension(number, input.ext, metadata, options.formatExtension);
|
|
|
|
case 'E.164':
|
|
// `E.164` doesn't define "phone number extensions".
|
|
return "+".concat(countryCallingCode).concat(nationalNumber);
|
|
|
|
case 'RFC3966':
|
|
return (0, _RFC.formatRFC3966)({
|
|
number: "+".concat(countryCallingCode).concat(nationalNumber),
|
|
ext: input.ext
|
|
});
|
|
// For reference, here's Google's IDD formatter:
|
|
// https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546
|
|
// Not saying that this IDD formatter replicates it 1:1, but it seems to work.
|
|
// Who would even need to format phone numbers in IDD format anyway?
|
|
|
|
case 'IDD':
|
|
if (!options.fromCountry) {
|
|
return; // throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.')
|
|
}
|
|
|
|
var formattedNumber = formatIDD(nationalNumber, input.carrierCode, countryCallingCode, options.fromCountry, metadata);
|
|
return addExtension(formattedNumber, input.ext, metadata, options.formatExtension);
|
|
|
|
default:
|
|
throw new Error("Unknown \"format\" argument passed to \"formatNumber()\": \"".concat(format, "\""));
|
|
}
|
|
}
|
|
|
|
function formatNationalNumber(number, carrierCode, formatAs, metadata, options) {
|
|
var format = chooseFormatForNumber(metadata.formats(), number);
|
|
|
|
if (!format) {
|
|
return number;
|
|
}
|
|
|
|
return (0, _formatNationalNumberUsingFormat["default"])(number, format, {
|
|
useInternationalFormat: formatAs === 'INTERNATIONAL',
|
|
withNationalPrefix: format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && options && options.nationalPrefix === false ? false : true,
|
|
carrierCode: carrierCode,
|
|
metadata: metadata
|
|
});
|
|
}
|
|
|
|
function chooseFormatForNumber(availableFormats, nationalNnumber) {
|
|
for (var _iterator = availableFormats, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|
var _ref;
|
|
|
|
if (_isArray) {
|
|
if (_i >= _iterator.length) break;
|
|
_ref = _iterator[_i++];
|
|
} else {
|
|
_i = _iterator.next();
|
|
if (_i.done) break;
|
|
_ref = _i.value;
|
|
}
|
|
|
|
var format = _ref;
|
|
|
|
// Validate leading digits
|
|
if (format.leadingDigitsPatterns().length > 0) {
|
|
// The last leading_digits_pattern is used here, as it is the most detailed
|
|
var lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]; // If leading digits don't match then move on to the next phone number format
|
|
|
|
if (nationalNnumber.search(lastLeadingDigitsPattern) !== 0) {
|
|
continue;
|
|
}
|
|
} // Check that the national number matches the phone number format regular expression
|
|
|
|
|
|
if ((0, _matchesEntirely["default"])(nationalNnumber, format.pattern())) {
|
|
return format;
|
|
}
|
|
}
|
|
}
|
|
|
|
function addExtension(formattedNumber, ext, metadata, formatExtension) {
|
|
return ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber;
|
|
}
|
|
|
|
function formatIDD(nationalNumber, carrierCode, countryCallingCode, fromCountry, metadata) {
|
|
var fromCountryCallingCode = (0, _metadata.getCountryCallingCode)(fromCountry, metadata.metadata); // When calling within the same country calling code.
|
|
|
|
if (fromCountryCallingCode === countryCallingCode) {
|
|
var formattedNumber = formatNationalNumber(nationalNumber, carrierCode, 'NATIONAL', metadata); // For NANPA regions, return the national format for these regions
|
|
// but prefix it with the country calling code.
|
|
|
|
if (countryCallingCode === '1') {
|
|
return countryCallingCode + ' ' + formattedNumber;
|
|
} // If regions share a country calling code, the country calling code need
|
|
// not be dialled. This also applies when dialling within a region, so this
|
|
// if clause covers both these cases. Technically this is the case for
|
|
// dialling from La Reunion to other overseas departments of France (French
|
|
// Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover
|
|
// this edge case for now and for those cases return the version including
|
|
// country calling code. Details here:
|
|
// http://www.petitfute.com/voyage/225-info-pratiques-reunion
|
|
//
|
|
|
|
|
|
return formattedNumber;
|
|
}
|
|
|
|
var iddPrefix = (0, _getIddPrefix["default"])(fromCountry, undefined, metadata.metadata);
|
|
|
|
if (iddPrefix) {
|
|
return "".concat(iddPrefix, " ").concat(countryCallingCode, " ").concat(formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata));
|
|
}
|
|
}
|
|
//# sourceMappingURL=format_.js.map
|