Skip to content

Commit

Permalink
For each region, include metadata of main country for calling code
Browse files Browse the repository at this point in the history
cf. #23
  • Loading branch information
dwbruhn committed Sep 2, 2016
1 parent 690f15a commit 5aefd66
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function writeFile(regionCode, countryToMetadata, countryCodeToRegionCodeMap) {
return fs.writeFileSync(outpath + 'metadata_'+regionCode+'.js', output.join(';\n'));
}

var regionCodeToCountryCodeMap = {};
var regionCodeToCountryCodeMap = {}; // construct map from territory codes ('region code') to country calling codes ('country code')
for (var countryCode in context.i18n.phonenumbers.metadata.countryCodeToRegionCodeMap) {
context.i18n.phonenumbers.metadata.countryCodeToRegionCodeMap[countryCode].forEach(function(regionCode) {
if (!regionCodeToCountryCodeMap[regionCode]) {
Expand All @@ -32,16 +32,30 @@ for (var countryCode in context.i18n.phonenumbers.metadata.countryCodeToRegionCo
});
}

// Grab the appropriate data.
// Grab the appropriate data, which includes the metadata for:
// - the region (e.g. CC)
// - the main country for the same calling code (e.g. AU, which shares calling code 61 with CC)
//
// Note: 'countryCode' = country calling code (e.g. 61 for AU)
// 'regionCode' = ISO 3166-1 alpha-2 (e.g. AU)

var countryToMetadata, countryCodeToRegionCodeMap;
for (var regionCode in context.i18n.phonenumbers.metadata.countryToMetadata) {
if (!Number.isNaN(parseInt(regionCode))) { continue; }

countryToMetadata = {};
var countryCode = regionCodeToCountryCodeMap[regionCode],
// get main country (first in the array) for the shared country calling code and include its metadata
mainCountryForCode = context.i18n.phonenumbers.metadata.countryCodeToRegionCodeMap[countryCode][0],
regionCodesToAdd = (regionCode === mainCountryForCode) ? [regionCode] : [mainCountryForCode, regionCode];

countryCodeToRegionCodeMap = {};
countryCodeToRegionCodeMap[countryCode] = regionCodesToAdd; // populate country code to region code map

countryToMetadata[regionCode] = context.i18n.phonenumbers.metadata.countryToMetadata[regionCode];
countryCodeToRegionCodeMap[regionCodeToCountryCodeMap[regionCode]] = [regionCode];
// add metadata for each relevant region code
countryToMetadata = {};
regionCodesToAdd.forEach(function (regionCodeToAdd) {
countryToMetadata[regionCodeToAdd] = context.i18n.phonenumbers.metadata.countryToMetadata[regionCodeToAdd];
});

writeFile(regionCode, countryToMetadata, countryCodeToRegionCodeMap);
}
Expand Down

0 comments on commit 5aefd66

Please sign in to comment.