dtable-utils
Version:
dtable common utils
139 lines (134 loc) • 5.04 kB
JavaScript
import { GROUP_GEOLOCATION_GRANULARITY } from '../constants/group.js';
import { GEOLOCATION_FORMAT } from '../constants/column.js';
import { isNumber } from '../number.js';
import { DMS_SPLITTER_TYPE, LAT_DIRECTION_TYPE, LNG_DIRECTION_TYPE } from '../constants/geolocation.js';
/**
* Convert decimal to DMS
* @param {number} lat
* @param {bool} isLat Latitude: N/S, Longitude: E/W
* @returns { dms, direction }, object
*/
var convertToDMS = function convertToDMS(decimal) {
var isLat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (!isNumber(decimal) && typeof decimal !== 'string') return {};
var numDecimal = typeof decimal === 'string' ? Number.parseFloat(decimal) : decimal;
if (!isNumber(numDecimal)) return {};
var degrees = Math.floor(Math.abs(numDecimal));
var minutesDecimal = Number.parseFloat(((Math.abs(numDecimal) - degrees) * 60).toFixed(8));
var minutes = Math.floor(minutesDecimal);
var seconds = Number.parseFloat(((minutesDecimal - minutes) * 60).toFixed(8));
var direction = '';
if (isLat) {
direction = numDecimal >= 0 ? LAT_DIRECTION_TYPE.N : LAT_DIRECTION_TYPE.S;
} else {
direction = numDecimal >= 0 ? LNG_DIRECTION_TYPE.E : LNG_DIRECTION_TYPE.W;
}
return {
dms: "".concat(degrees).concat(DMS_SPLITTER_TYPE.DEG).concat(minutes).concat(DMS_SPLITTER_TYPE.MIN).concat(seconds).concat(DMS_SPLITTER_TYPE.SEC),
direction: direction
};
};
var getLatLngDisplayString = function getLatLngDisplayString(lat, lng) {
var _convertToDMS = convertToDMS(lat, true),
lat_dms = _convertToDMS.dms,
lat_direction = _convertToDMS.direction;
var _convertToDMS2 = convertToDMS(lng),
lng_dms = _convertToDMS2.dms,
lng_direction = _convertToDMS2.direction;
if (!lat_dms || !lng_dms) return '';
return "".concat(lat_direction).concat(lat_dms, ", ").concat(lng_direction).concat(lng_dms);
};
/**
* Get formatted geolocation
* @param {object} loc
* @param {object} formats , e.g. { geo_format, ... }
* @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$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;
return getLatLngDisplayString(lat, 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 { convertToDMS, getGeolocationByGranularity, getGeolocationDisplayString, getLatLngDisplayString };