src/usePostalCity.js
/**
  This function allows overriding the locality returned
  by the Point-in-Polygon query with a different locality record
  which was mapped from the postalcode using a lookup table.

  If the Point-in-Polygon query returned a valid locality then
  it is added as an alias for the 'Postal City'.
**/
This function allows overriding the locality returned
by the Point-in-Polygon query with a different locality record
which was mapped from the postalcode using a lookup table.

If the Point-in-Polygon query returned a valid locality then
it is added as an alias for the 'Postal City'.
This function allows overriding the locality returned by the Point - in - Polygon query with a different locality record which was mapped from the postalcode using a lookup table . If the Point - in - Polygon query returned a valid locality then it is added as an alias for the ' Postal City ' .
function usePostalCity( result, doc ){
  const postalcode = getPostalCode(doc);
  if( !postalcode || !postalcode.length ){ return; }
  const countrycode = getCountryCode(result);
  if( !countrycode || countrycode.length !== 3 ){ return; }
  var localities = postalCityMap.lookup(countrycode, postalcode);
  if( !Array.isArray(localities) || !localities.length ){ return; }
  try {
    if( doc.parent.hasOwnProperty('locality') && doc.parent.locality.length &&
        doc.parent.hasOwnProperty('locality_id') && doc.parent.locality_id.length &&
        doc.parent.hasOwnProperty('locality_a') && doc.parent.locality_a.length ){
      localities = localities.concat(
        doc.parent.locality.map((_, i) => {
          return {
            name:   doc.parent.locality[i],
            wofid:  doc.parent.locality_id[i],
            abbr:   doc.parent.locality_a[i]
          };
        })
      );
    }
    doc.clearParent('locality');
    localities = localities.filter((locality, index, self) => {
      return index === self.findIndex(t => t.wofid === locality.wofid);
    });
    localities.forEach(locality => {
      doc.addParent('locality', locality.name, locality.wofid, locality.abbr);
    });
  }
  catch (err) {
    logger.warn('invalid value', {
      centroid: doc.getCentroid(),
      result: {
        type: 'locality',
        values: localities
      }
    });
  }
}
function usePostalCity ( result , doc ) { const postalcode = getPostalCode ( doc ) ; if ( ! postalcode || ! postalcode . length ) { return ; } const countrycode = getCountryCode ( result ) ; if ( ! countrycode || countrycode . length !== 3 ) { return ; } var localities = postalCityMap . lookup ( countrycode , postalcode ) ; if ( ! Array . isArray ( localities ) || ! localities . length ) { return ; } try { if ( doc . parent . hasOwnProperty ( 'locality' ) && doc . parent . locality . length && doc . parent . hasOwnProperty ( 'locality_id' ) && doc . parent . locality_id . length && doc . parent . hasOwnProperty ( 'locality_a' ) && doc . parent . locality_a . length ) { localities = localities . concat ( doc . parent . locality . map ( ( _ , i ) => { return { name : doc . parent . locality [ i ] , wofid : doc . parent . locality_id [ i ] , abbr : doc . parent . locality_a [ i ] } ; } ) ) ; } doc . clearParent ( 'locality' ) ; localities = localities . filter ( ( locality , index , self ) => { return index === self . findIndex ( t => t . wofid === locality . wofid ) ; } ) ; localities . forEach ( locality => { doc . addParent ( 'locality' , locality . name , locality . wofid , locality . abbr ) ; } ) ; } catch ( err ) { logger . warn ( 'invalid value' , { centroid : doc . getCentroid ( ) , result : { type : 'locality' , values : localities } } ) ; } }
This function allows overriding the locality returned
by the Point-in-Polygon query with a different locality record
which was mapped from the postalcode using a lookup table.
This function allows overriding the locality returned by the Point - in - Polygon query with a different locality record which was mapped from the postalcode using a lookup table .
// find record postal code // check we have a valid postal code // find record country code // check we have a valid country code // look up the 'postal city' locality // check the localities list is valid // we will use the first value instead of the PIP locality // save the existing locality as an alias // note: this would be better using getters, but the model does not // currently offer a 'getParent' method. // append the existing localities on to the postal city localities array // remove the existing locality info // deduplicate localities // add all localities, using the first matching postal locality as the default // and then indexing all other localities as aliases (including any existing localities) // note: addParent can throw an error if, for example, name is an empty string