libphonenumber-metadata-generator
Version:
Metadata generator for `libphonenumber-js`
82 lines (66 loc) • 3.35 kB
JavaScript
// Minifies metadata: converts objects to positional arrays
// and removes example phone numbers.
export default function minify(metadata) {
var countries = {};
for (var _i = 0, _Object$keys = Object.keys(metadata.countries); _i < _Object$keys.length; _i++) {
var countryCode = _Object$keys[_i];
countries[countryCode] = minifyTelephoneNumberingPlan(metadata.countries[countryCode]);
}
var nonGeographic = {};
for (var _i2 = 0, _Object$keys2 = Object.keys(metadata.nonGeographic); _i2 < _Object$keys2.length; _i2++) {
var callingCode = _Object$keys2[_i2];
nonGeographic[callingCode] = minifyTelephoneNumberingPlan(metadata.nonGeographic[callingCode]);
}
return {
version: metadata.version,
country_calling_codes: metadata.country_calling_codes,
countries: countries,
nonGeographic: nonGeographic
};
}
function minifyTelephoneNumberingPlan(country) {
// When changing this array also change getters in `./metadata.js`
var country_array = [country.phone_code, country.idd_prefix, country.national_number_pattern, country.possible_lengths, // country.possible_lengths_local,
country.formats && country.formats.map(function (format) {
// When changing this array also change getters in `./metadata.js`
var format_array = [format.pattern, format.format, format.leading_digits_patterns, format.national_prefix_formatting_rule, format.national_prefix_is_optional_when_formatting, // format.domestic_carrier_code_formatting_rule,
format.international_format];
return trimArray(format_array);
}), country.national_prefix, country.national_prefix_formatting_rule, country.national_prefix_for_parsing, country.national_prefix_transform_rule, country.national_prefix_is_optional_when_formatting, country.leading_digits];
if (country.types) {
var types_array = [// These are common
country.types.fixed_line, country.types.mobile, country.types.toll_free, country.types.premium_rate, country.types.personal_number, // These are less common
country.types.voicemail, country.types.uan, country.types.pager, country.types.voip, country.types.shared_cost].map(function (type) {
return type && trimArray([type.pattern, type.possible_lengths // type.possible_lengths_local
]);
});
country_array.push(trimArray(types_array));
} else {
country_array.push(null);
}
country_array.push(country.default_idd_prefix);
country_array.push(country.ext); // // Currently, there're no countries having
// // `domestic_carrier_code_formatting_rule` at the root level.
// country_array.push(country.domestic_carrier_code_formatting_rule)
return trimArray(country_array);
} // Empty strings are not considered "empty".
function isEmpty(value) {
return value === undefined || value === null || value === false || Array.isArray(value) && value.length === 0;
} // Removes trailing empty values from an `array`
function trimArray(array) {
// First, trim any empty elements.
while (array.length > 0 && isEmpty(array[array.length - 1])) {
array.pop();
} // Then replace all remaining empty elements with `0`
// and also `true` with `1`.
return array.map(function (element) {
if (isEmpty(element)) {
return 0;
}
if (element === true) {
return 1;
}
return element;
});
}
//# sourceMappingURL=minify.js.map