libphonenumber-metadata-generator
Version:
Metadata generator for `libphonenumber-js`
110 lines (100 loc) • 3.1 kB
JavaScript
export default function compress(metadata) {
const countries = {}
for (const countryCode of Object.keys(metadata.countries)) {
countries[countryCode] = compressNumberingPlan(metadata.countries[countryCode])
}
const nonGeographic = {}
for (const callingCode of Object.keys(metadata.nonGeographic)) {
nonGeographic[callingCode] = compressNumberingPlan(metadata.nonGeographic[callingCode])
}
return {
version: metadata.version,
country_calling_codes: metadata.country_calling_codes,
countries,
nonGeographic
}
}
function compressNumberingPlan(country) {
// When changing this array also change getters in `./metadata.js`
const country_array = [
country.phone_code,
country.idd_prefix,
country.national_number_pattern,
country.possible_lengths,
// country.possible_lengths_local,
country.formats && country.formats.map((format) => {
// When changing this array also change getters in `./metadata.js`
const 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) {
const 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((type) => 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((element) => {
if (isEmpty(element)) {
return 0
}
if (element === true) {
return 1
}
return element
})
}