Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Created September 29, 2015 12:35
Show Gist options
  • Save Muzietto/62c3fc5a1c285f091654 to your computer and use it in GitHub Desktop.
Save Muzietto/62c3fc5a1c285f091654 to your computer and use it in GitHub Desktop.
google/i18n/libphonenumber - node.js script to prepare single-country metadata files
/*
SCLEXE - single-country libphonenumber executable
Author: Marco Faustinelli ([email protected])
Web: http://faustinelli.net/
http://faustinelli.wordpress.com/
Version: 1.0
The MIT License - Copyright (c) 2015 SCLEXE Project
*/
var fs = require('fs');
var vm = require('vm');
var util = require('util');
var companyIsoCode = process.argv[2];
var googleMetadataIsoCode = (companyIsoCode === 'UK') ? 'GB' : companyIsoCode;
var metadatafilename = process.argv[3];
var outputfilename = process.argv[4];
var context = {
goog: {
provide: function(){}
},
i18n: {
phonenumbers: {
metadata: {}
}
}
};
var metadata = fs.readFileSync(metadatafilename);
vm.runInNewContext(metadata, context);
var countryCode = 0;
for (var cc in context.i18n.phonenumbers.metadata.countryCodeToRegionCodeMap) {
if (context.i18n.phonenumbers.metadata.countryCodeToRegionCodeMap[cc].indexOf(googleMetadataIsoCode) !== -1) {
countryCode = cc;
break;
}
}
if (countryCode === 0) {
console.error('cannot set countryCode');
process.exit(1);
}
var countryToMetadata = {};
countryToMetadata[companyIsoCode] = context.i18n.phonenumbers.metadata.countryToMetadata[googleMetadataIsoCode];
var countryCodeToRegionCodeMap = {};
var cctrcmcc = context.i18n.phonenumbers.metadata.countryCodeToRegionCodeMap[countryCode];
cctrcmcc = cctrcmcc.map(function(ic) { return ic.replace(googleMetadataIsoCode, companyIsoCode); });
countryCodeToRegionCodeMap[countryCode] = cctrcmcc;
var output = util.format("\
goog.provide('i18n.phonenumbers.metadata');\n\
i18n.phonenumbers.metadata.countryCodeToRegionCodeMap = %j;\n\
i18n.phonenumbers.metadata.countryToMetadata = %j;\
", countryCodeToRegionCodeMap, countryToMetadata);
fs.writeFileSync(outputfilename, output);
@Muzietto
Copy link
Author

Starting from the whole metadata of the original libphonenumber project and an ISO code for a given country, this node.js script writes a file containing the sole metadata for that country.
Due to some ancient fawlty decision, I have to map isocode 'GB' into the value 'UK' used in the legacy code I am dealing with.
This script is invoked from ant project build.xml.

With many thanks to @gabrielelana for connecting lots of dots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment