dtable-utils
Version:
dtable common utils
105 lines (101 loc) • 3.6 kB
JavaScript
import { GROUP_GEOLOCATION_GRANULARITY } from '../constants/group.js';
import { GEOLOCATION_FORMAT } from '../constants/column.js';
import { isValidPosition } from '../validate/geolocation.js';
/**
* Get formatted geolocation
* @param {object} loc
* @param {object} formats , e.g. { geo_format, ... }
* @param {bool} isBaiduMap Determine the way to connect latitude and longitude. Default as true
* @param {string} hyphen Used as a connector between province, city, district and detail. Default as empty string
* @returns formatted geolocation, string
*/
var getGeolocationDisplayString = function getGeolocationDisplayString(loc, formats) {
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref$isBaiduMap = _ref.isBaiduMap,
isBaiduMap = _ref$isBaiduMap === void 0 ? true : _ref$isBaiduMap,
_ref$hyphen = _ref.hyphen,
hyphen = _ref$hyphen === void 0 ? '' : _ref$hyphen;
if (!loc) return '';
var _ref2 = formats || {},
geo_format = _ref2.geo_format;
switch (geo_format) {
case GEOLOCATION_FORMAT.LNG_LAT:
{
var lng = loc.lng,
lat = loc.lat;
if (!isValidPosition(lng, lat)) return '';
return isBaiduMap ? "".concat(lng, ", ").concat(lat) : "".concat(lat, ", ").concat(lng);
}
case GEOLOCATION_FORMAT.COUNTRY_REGION:
{
var country_region = loc.country_region;
return country_region || '';
}
case GEOLOCATION_FORMAT.PROVINCE:
{
var province = loc.province;
return province || '';
}
case GEOLOCATION_FORMAT.PROVINCE_CITY:
{
var _province = loc.province,
city = loc.city;
return "".concat(_province || '').concat(hyphen).concat(city || '').trim();
}
case GEOLOCATION_FORMAT.PROVINCE_CITY_DISTRICT:
{
var _province2 = loc.province,
_city = loc.city,
district = loc.district;
return "".concat(_province2 || '').concat(hyphen).concat(_city || '').concat(hyphen).concat(district || '').trim();
}
case GEOLOCATION_FORMAT.MAP_SELECTION:
{
var address = loc.address,
title = loc.title;
return "".concat(address || '').concat(hyphen).concat(title || '').trim();
}
default:
{
// default as 'geolocation'
var _province3 = loc.province,
_city2 = loc.city,
_district = loc.district,
detail = loc.detail;
if (!_province3 && !_city2 && !_district && !detail) return '';
return "".concat(_province3 || '').concat(hyphen).concat(_city2 || '').concat(hyphen).concat(_district || '').concat(hyphen).concat(detail || '').trim();
}
}
};
/**
* Get geolocation by granularity
* @param {object} geolocation e.g. { province, ... }
* @param {string} granularity
* @returns geolocation string
*/
var getGeolocationByGranularity = function getGeolocationByGranularity(geolocation, granularity) {
if (!geolocation) return '';
var province = geolocation.province,
city = geolocation.city,
district = geolocation.district,
country_region = geolocation.country_region;
switch (granularity) {
case GROUP_GEOLOCATION_GRANULARITY.CITY:
{
return city || '';
}
case GROUP_GEOLOCATION_GRANULARITY.DISTRICT:
{
return district || '';
}
case GROUP_GEOLOCATION_GRANULARITY.COUNTRY:
{
return country_region || '';
}
default:
{
return province || '';
}
}
};
export { getGeolocationByGranularity, getGeolocationDisplayString };