@synotech/utils
Version:
a collection of utilities for internal use
172 lines (166 loc) • 4.74 kB
text/typescript
export interface AddressInterface {
address: string;
displayName: string;
country: string;
countryName: string;
state: string;
stateName: string;
town: string;
municipality: string;
location: {
latitude: number;
longitude: number;
};
}
/**
* This function returns a well structured json object
* @module getGooglePlace
* @param {place} place - a stringified object in string format
* @return {object} {AddressInterface} a well structured address object
*
*
* @example
*
* getGooglePlace({
"name": "places/ChIJTVIU4hvSeh4RRSHZyqpaM7g",
"id": "ChIJTVIU4hvSeh4RRSHZyqpaM7g",
"types": [
"premise"
],
"formattedAddress": "125 Church Rd, Walmer, Gqeberha, 6065, South Africa",
"addressComponents": [
{
"longText": "125",
"shortText": "125",
"types": [
"street_number"
],
"languageCode": "en-US"
},
{
"longText": "Church Road",
"shortText": "Church Rd",
"types": [
"route"
],
"languageCode": "en"
},
{
"longText": "Walmer",
"shortText": "Walmer",
"types": [
"sublocality_level_1",
"sublocality",
"political"
],
"languageCode": "en"
},
{
"longText": "Gqeberha",
"shortText": "Gqeberha",
"types": [
"locality",
"political"
],
"languageCode": "en"
},
{
"longText": "Nelson Mandela Bay Metropolitan Municipality",
"shortText": "Nelson Mandela Bay Metropolitan Municipality",
"types": [
"administrative_area_level_2",
"political"
],
"languageCode": "en"
},
{
"longText": "Eastern Cape",
"shortText": "EC",
"types": [
"administrative_area_level_1",
"political"
],
"languageCode": "en"
},
{
"longText": "South Africa",
"shortText": "ZA",
"types": [
"country",
"political"
],
"languageCode": "en"
},
{
"longText": "6065",
"shortText": "6065",
"types": [
"postal_code"
],
"languageCode": "en-US"
}
],
"location": {
"latitude": -33.9783681,
"longitude": 25.5750946
},
"viewport": {
"low": {
"latitude": -33.979975672885232,
"longitude": 25.573859654533269
},
"high": {
"latitude": -33.977277712302232,
"longitude": 25.576557615116275
}
},
"googleMapsUri": "https://maps.google.com/?cid=13273052216385151301",
"utcOffsetMinutes": 120,
"adrFormatAddress": "\u003cspan class=\"street-address\"\u003e125 Church Rd\u003c/span\u003e, \u003cspan class=\"extended-address\"\u003eWalmer\u003c/span\u003e, \u003cspan class=\"locality\"\u003eGqeberha\u003c/span\u003e, \u003cspan class=\"postal-code\"\u003e6065\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eSouth Africa\u003c/span\u003e",
"iconMaskBaseUri": "https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet",
"iconBackgroundColor": "#7B9EB0",
"displayName": {
"text": "125 Church Rd"
},
"primaryTypeDisplayName": {
"text": "Compound building",
"languageCode": "en-US"
},
"primaryType": "premise",
"shortFormattedAddress": "125 Church Rd, Walmer, Gqeberha"
})
*
*/
export const googlePlaceAddressFormatter = (place: any): AddressInterface => {
let country, state, municipality, town;
try {
country = place?.addressComponents?.find((o: any) =>
o.types.includes('country')
);
state = place?.addressComponents?.find((o: any) =>
o.types.includes('administrative_area_level_1')
);
municipality = place?.addressComponents?.find((o: any) =>
o.types.includes('administrative_area_level_2')
);
town = place?.addressComponents?.find((o: any) =>
o.types.includes('locality')
);
} catch (error) {
// @ts-ignore
}
const address = place?.formattedAddress;
const displayName = place?.displayName;
const location = place?.location;
return {
address,
displayName,
...(country && { country: country.shortText }),
...(country && { countryName: country.longText }),
...(state && { state: state.shortText }),
...(state && { stateName: state.longText }),
...(town && { town: town.longText }),
...(municipality && { municipality: municipality.shortText }),
location,
};
};