wix-style-react
Version:
wix-style-react
91 lines (78 loc) • 2.99 kB
JavaScript
/*eslint camelcase: off*/
import isUndefined from 'lodash/isUndefined';
export var includes = function includes(arr, value) {
return Boolean(arr && arr.find(function (item) {
return item === value;
})); // we compare only primitives
};
var locationFuncOrValue = function locationFuncOrValue(locationProp) {
return typeof locationProp === 'function' ? locationProp() : locationProp;
};
var getFormattedStreetAddress = function getFormattedStreetAddress(address) {
try {
if (!address || !document) {
return undefined;
}
var wrapperElement = document.createElement('div');
wrapperElement.innerHTML = address;
var addressElement = wrapperElement.querySelector('.street-address');
return addressElement ? addressElement.textContent : undefined;
} catch (e) {
return undefined;
}
};
export function google2address(google) {
var components = {};
google.address_components.forEach(function (_ref) {
var types = _ref.types,
long_name = _ref.long_name,
short_name = _ref.short_name;
types.forEach(function (type) {
components[type] = { long_name: long_name, short_name: short_name };
});
});
var locality = components.locality || components.sublocality || components.postal_town;
var _google$geometry$loca = google.geometry.location,
lat = _google$geometry$loca.lat,
lng = _google$geometry$loca.lng;
var result = {
formatted: google.formatted_address,
formattedStreetAddress: getFormattedStreetAddress(google.adr_address),
latLng: {
lat: locationFuncOrValue(lat),
lng: locationFuncOrValue(lng)
},
approximate: !includes(google.types, 'street_address') && !includes(google.types, 'premise'),
city: locality && locality.long_name,
state: components.administrative_area_level_1 && components.administrative_area_level_1.short_name,
country: components.country && components.country.long_name,
countryCode: components.country && components.country.short_name,
street: components.route && components.route.long_name,
number: components.street_number && components.street_number.long_name,
postalCode: components.postal_code && components.postal_code.long_name,
subpremise: components.subpremise && components.subpremise.long_name
};
for (var key in result) {
if (isUndefined(result[key])) {
delete result[key];
}
}
return result;
}
export var trySetStreetNumberIfNotReceived = function trySetStreetNumberIfNotReceived(google, inputValue) {
var addressParts = inputValue.match(/^\d+[ -/]*\d*[^\D]/);
var hasStreetNumber = google.address_components.some(function (address) {
return address.types.some(function (t) {
return t === 'street_number';
});
});
if (hasStreetNumber || !addressParts) {
return google;
}
google.address_components.unshift({
long_name: addressParts.join(),
short_name: addressParts.join(),
types: ['street_number']
});
return google;
};