Skip to content

Commit

Permalink
Add CSV support
Browse files Browse the repository at this point in the history
Running `fetch.js` now also generates `mcc-mnc-list.csv` file.

Signed-off-by: Prashanth Pai <prashanth.pai@plivo.com>
  • Loading branch information
ppai-plivo committed Apr 9, 2020
1 parent fc9684e commit 7ea1907
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
16 changes: 13 additions & 3 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const csvstringify = require('csv-stringify/lib/sync')

const WIKI_URL = 'https://en.wikipedia.org/wiki/Mobile_country_code';
const WIKI_URL_REGIONS = [
Expand All @@ -15,7 +16,8 @@ const WIKI_URL_REGIONS = [
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_7xx_(South_America)'
];

const MCC_MNC_OUTPUT_FILE = path.join( __dirname, 'mcc-mnc-list.json');
const MCC_MNC_OUTPUT_FILE_JSON = path.join( __dirname, 'mcc-mnc-list.json');
const MCC_MNC_OUTPUT_FILE_CSV = path.join( __dirname, 'mcc-mnc-list.csv');
const STATUS_CODES_OUTPUT_FILE = path.join( __dirname, 'status-codes.json');


Expand Down Expand Up @@ -174,11 +176,19 @@ function collect (resolve, from, records, statusCodes, globals) {
}

function writeData(records, statusCodes) {
fs.writeFile( MCC_MNC_OUTPUT_FILE, JSON.stringify( records, null, 2 ), err => {
fs.writeFile( MCC_MNC_OUTPUT_FILE_JSON, JSON.stringify( records, null, 2 ), err => {
if ( err ) {
throw err;
}
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE );
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE_JSON );
console.log( 'Total ' + records.length + ' records' );
});

fs.writeFile( MCC_MNC_OUTPUT_FILE_CSV, csvstringify( records , {header: true} ), err => {
if ( err ) {
throw err;
}
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE_CSV );
console.log( 'Total ' + records.length + ' records' );
});

Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "MIT",
"devDependencies": {
"ava": "^2.4.0",
"csv-stringify": "^5.3.6",
"eslint": "^6.8.0",
"jsdom": "^16.2.2"
},
Expand Down

0 comments on commit 7ea1907

Please sign in to comment.