@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
27 lines (26 loc) • 1.41 kB
JavaScript
const makeComponents = (components) => {
const result = components === null || components === void 0 ? void 0 : components.reduce((obj, i) => {
return Object.assign(Object.assign({}, obj), { [i.types[0]]: { long_name: i.long_name, short_name: i.short_name } });
}, {});
return result;
};
export const makeAddress = (place) => {
var _a, _b;
const { address_components, formatted_address, geometry } = place;
const components = makeComponents(address_components);
if (!components)
return null;
const { street_number, route, locality: city, sublocality_level_1: subcity, administrative_area_level_1: state, postal_code: postalCode } = components;
const streetNumber = street_number ? street_number.short_name : '';
const street = route ? route.long_name : '';
const addressCity = city || subcity;
return {
street: `${streetNumber} ${street}`.trim(),
city: addressCity ? addressCity.long_name : '',
state: state ? state.short_name : '',
postal_code: postalCode ? postalCode.short_name : '',
lat: ((_a = geometry === null || geometry === void 0 ? void 0 : geometry.location) === null || _a === void 0 ? void 0 : _a.lat()) || 0,
lng: ((_b = geometry === null || geometry === void 0 ? void 0 : geometry.location) === null || _b === void 0 ? void 0 : _b.lng()) || 0,
formatted_address
};
};