UNPKG

dtable-utils

Version:

dtable common utils

146 lines (139 loc) 5.31 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var group = require('../constants/group.js'); var column = require('../constants/column.js'); var number = require('../number.js'); var geolocation = require('../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 (!number.isNumber(decimal) && typeof decimal !== 'string') return {}; var numDecimal = typeof decimal === 'string' ? Number.parseFloat(decimal) : decimal; if (!number.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 ? geolocation.LAT_DIRECTION_TYPE.N : geolocation.LAT_DIRECTION_TYPE.S; } else { direction = numDecimal >= 0 ? geolocation.LNG_DIRECTION_TYPE.E : geolocation.LNG_DIRECTION_TYPE.W; } return { dms: "".concat(degrees).concat(geolocation.DMS_SPLITTER_TYPE.DEG).concat(minutes).concat(geolocation.DMS_SPLITTER_TYPE.MIN).concat(seconds).concat(geolocation.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 column.GEOLOCATION_FORMAT.LNG_LAT: { var lng = loc.lng, lat = loc.lat; return getLatLngDisplayString(lat, lng); } case column.GEOLOCATION_FORMAT.COUNTRY_REGION: { var country_region = loc.country_region; return country_region || ''; } case column.GEOLOCATION_FORMAT.PROVINCE: { var province = loc.province; return province || ''; } case column.GEOLOCATION_FORMAT.PROVINCE_CITY: { var _province = loc.province, city = loc.city; return "".concat(_province || '').concat(hyphen).concat(city || '').trim(); } case column.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 column.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.GROUP_GEOLOCATION_GRANULARITY.CITY: { return city || ''; } case group.GROUP_GEOLOCATION_GRANULARITY.DISTRICT: { return district || ''; } case group.GROUP_GEOLOCATION_GRANULARITY.COUNTRY: { return country_region || ''; } default: { return province || ''; } } }; exports.convertToDMS = convertToDMS; exports.getGeolocationByGranularity = getGeolocationByGranularity; exports.getGeolocationDisplayString = getGeolocationDisplayString; exports.getLatLngDisplayString = getLatLngDisplayString;