src/postalCityMap.js
// load the file format in to memory
load the file format in to memory
load the file format in to memory
function loadTable(cc){
  var m = {};
  var paths = {
    base:       path.resolve(__dirname, `data/${cc}.tsv`),
    override:   path.resolve(__dirname, `data/${cc}.override.tsv`)
  };
  var rows = [];
  if( fs.existsSync(paths.base) ){
    rows = rows.concat( parse( paths.base ) );
  }
  if( fs.existsSync(paths.override) ){
    rows = rows.concat( parse( paths.override, Number.MAX_SAFE_INTEGER ) );
  }
  rows.forEach(row => {
    const postalcode = normalizePostcode(row.postalcode);
    if( !m.hasOwnProperty(postalcode) ){ m[postalcode] = []; }
    m[postalcode].push({
      wofid: row.wofid.replace(/\s/g, ''),
      name: row.name.trim(),
      abbr: row.abbr && row.abbr.length ? row.abbr.trim() : undefined,
      placetype: row.placetype && row.placetype.length ? row.placetype.trim() : undefined,
      weight: row.weight ? parseInt(row.weight, 10) : 0
    });
  });
  for( var postalcode in m ){
    m[postalcode] = m[postalcode].filter(locality => {
      if( 'string' !== typeof locality.wofid || !locality.wofid.length ){ return false; }
      if( 'string' !== typeof locality.name  || !locality.name.length ){ return false; }
      return true;
    });
    m[postalcode] = stable(m[postalcode], (a, b) => b.weight - a.weight);
  }
  return m;
}
function loadTable ( cc ) { var m = { } ; var paths = { base : path . resolve ( __dirname , ` ${ cc } ` ) , override : path . resolve ( __dirname , ` ${ cc } ` ) } ; var rows = [ ] ; if ( fs . existsSync ( paths . base ) ) { rows = rows . concat ( parse ( paths . base ) ) ; } if ( fs . existsSync ( paths . override ) ) { rows = rows . concat ( parse ( paths . override , Number . MAX_SAFE_INTEGER ) ) ; } rows . forEach ( row => { const postalcode = normalizePostcode ( row . postalcode ) ; if ( ! m . hasOwnProperty ( postalcode ) ) { m [ postalcode ] = [ ] ; } m [ postalcode ] . push ( { wofid : row . wofid . replace ( / \s / g , '' ) , name : row . name . trim ( ) , abbr : row . abbr && row . abbr . length ? row . abbr . trim ( ) : undefined , placetype : row . placetype && row . placetype . length ? row . placetype . trim ( ) : undefined , weight : row . weight ? parseInt ( row . weight , 10 ) : 0 } ) ; } ) ; for ( var postalcode in m ) { m [ postalcode ] = m [ postalcode ] . filter ( locality => { if ( 'string' !== typeof locality . wofid || ! locality . wofid . length ) { return false ; } if ( 'string' !== typeof locality . name || ! locality . name . length ) { return false ; } return true ; } ) ; m [ postalcode ] = stable ( m [ postalcode ] , ( a , b ) => b . weight - a . weight ) ; } return m ; }
load the file format in to memory
load the file format in to memory
// each table can have multiple files, the base file is generated by // scripts and should not be manually edited, the '*.override.tsv' // files are human edited files which take priority. // load files from disk and parse contents // load base file // load override file // generate map // post-processing // remove any invalid records // re-sort the records by weight DESC in case they were provided out-of-order