dtable-utils
Version:
dtable common utils
204 lines (197 loc) • 7.23 kB
JavaScript
import _typeof from '@babel/runtime/helpers/typeof';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { GEOLOCATION_FORMAT } from '../constants/column.js';
import '../constants/filter/filter-column-options.js';
import '../constants/filter/filter-modifier.js';
import '../constants/filter/filter-predicate.js';
import '../constants/filter/filter-is-within.js';
import '../constants/formula.js';
import '../constants/sort.js';
import '../constants/group.js';
import { LAT_DIRECTION_TYPE, LNG_DIRECTION_TYPE, DMS_SPLITTER_TYPE } from '../constants/geolocation.js';
import { isNumber } from '../number.js';
var provinceReg = /.+省|.+自治区|.+特别行政区|北京市|天津市|上海市|重庆市|安徽|福建|甘肃|广东|广西|贵州|海南|河北|河南|黑龙江|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|四川|西藏|新疆|云南|浙江|北京|上海|天津|重庆/;
var cityReg = /.+自治州|[^市]+市|.+盟|.+地区|.+区划/;
var districtReg = /(.+市|.+县|.+旗|.+区)/;
var LNG_LAT_REG = /^-?([1-9]\d*\.\d+|0\.\d+|[1-9]\d*|0)$/;
var DMS_DEG_REG = new RegExp("(\\d+(?:\\.\\d+)?)".concat(DMS_SPLITTER_TYPE.DEG));
var DMS_MIN_REG = new RegExp("(\\d+(?:\\.\\d+)?)".concat(DMS_SPLITTER_TYPE.MIN));
var DMS_SEC_REG = new RegExp("(\\d+(?:\\.\\d+)?)".concat(DMS_SPLITTER_TYPE.SEC));
var parseDMS = function parseDMS(dms, direction) {
if (typeof dms !== 'string') {
return null;
}
var degMatches = dms.match(DMS_DEG_REG);
var minMatches = dms.match(DMS_MIN_REG);
var secMatches = dms.match(DMS_SEC_REG);
var strDeg = degMatches && degMatches[1];
var strMin = minMatches && minMatches[1];
var strSec = secMatches && secMatches[1];
if (!strDeg && !strMin && !strSec) {
// invalid DMS
return null;
}
var deg = Number.parseFloat(strDeg);
var min = Number.parseFloat(strMin);
var sec = Number.parseFloat(strSec);
if (!isNumber(deg) && !isNumber(min) && !isNumber(sec)) {
return null;
}
deg = deg || 0;
min = min || 0;
sec = sec || 0;
var decimal = deg + min / 60 + sec / 3600;
var fixedDecimal = parseFloat(decimal.toFixed(6));
if (direction === LAT_DIRECTION_TYPE.S || direction === LNG_DIRECTION_TYPE.W) {
return -Math.abs(fixedDecimal);
}
return fixedDecimal;
};
/**
* Parse LatLng-DMS string to degree
* e.g. 'N30°50′50.55″, E30.50′50.55″'
* @param {string} strLatLng
* @returns { lat, lng }, object
*/
var parseLatLngDMS = function parseLatLngDMS(strLatLngDMS) {
if (typeof strLatLngDMS !== 'string') {
return {};
}
var _strLatLngDMS$split$m = strLatLngDMS.split(',').map(function (str) {
return str ? str.trim() : '';
}),
_strLatLngDMS$split$m2 = _slicedToArray(_strLatLngDMS$split$m, 2),
strLatDMS = _strLatLngDMS$split$m2[0],
strLngDMS = _strLatLngDMS$split$m2[1];
if (!strLatDMS || !strLngDMS) {
// invalid LatLng DMS
return {};
}
var latDirection = strLatDMS[0];
var lngDirection = strLngDMS[0];
if (!latDirection || !lngDirection || latDirection !== LAT_DIRECTION_TYPE.N && latDirection !== LAT_DIRECTION_TYPE.S || lngDirection !== LNG_DIRECTION_TYPE.E && lngDirection !== LNG_DIRECTION_TYPE.W) {
// invalid lat/lng direction
return {};
}
var strLat = strLatDMS.substring(1);
var strLng = strLngDMS.substring(1);
var lat = parseDMS(strLat, latDirection);
var lng = parseDMS(strLng, lngDirection);
if (!isNumber(lat) || !isNumber(lng)) {
return {};
}
return {
lat: lat,
lng: lng
};
};
/**
* Parse LatLng-DMS string to degree
* e.g. '30.50, 30.50'
* @param {string} strLatLng
* @returns { lat, lng }, object
*/
var parseLngLatDegree = function parseLngLatDegree(strLngLat) {
// match floating point numbers
// ^[1-9]\d*\.\d+$ --> floating point type that does not start with 0
// ^0\.\d+$ --> floating point type starting with 0
// ^[1-9]\d*$ --> non-zero integer
// 0
if (typeof strLngLat !== 'string') {
return {};
}
var _strLngLat$split$map = strLngLat.split(',').map(function (str) {
return str ? str.trim() : '';
}),
_strLngLat$split$map2 = _slicedToArray(_strLngLat$split$map, 2),
strLng = _strLngLat$split$map2[0],
strLat = _strLngLat$split$map2[1];
if (!strLng || !strLat || !strLng.match(LNG_LAT_REG) || !strLat.match(LNG_LAT_REG)) {
return {};
}
var numLng = Number(strLng);
var numLat = Number(strLat);
if (!isNumber(numLng) || !isNumber(numLat)) {
return {};
}
return {
lat: numLat,
lng: numLng
};
};
/**
* Parse LatLng-DMS/LngLat-Degree string to degree
* @param {string} strLatLng
* @returns { lat, lng }, object
*/
var parseLatLng = function parseLatLng(strLatLng) {
if (strLatLng && (strLatLng.includes(LAT_DIRECTION_TYPE.N) && strLatLng.includes(LNG_DIRECTION_TYPE.E) || strLatLng.includes(LAT_DIRECTION_TYPE.S) && strLatLng.includes(LNG_DIRECTION_TYPE.W))) {
return parseLatLngDMS(strLatLng);
}
return parseLngLatDegree(strLatLng);
};
var formatTextToGeolocation = function formatTextToGeolocation(value, columnData) {
// compatible with the old version, the old version data may be null or undefined
var _ref = columnData || {},
_ref$geo_format = _ref.geo_format,
geo_format = _ref$geo_format === void 0 ? GEOLOCATION_FORMAT.GEOLOCATION : _ref$geo_format;
var cellValue = value || '';
if (cellValue.length < 3) {
return {};
}
if (geo_format === GEOLOCATION_FORMAT.LNG_LAT) {
return parseLatLng(cellValue);
}
var matchedProvince = cellValue.match(provinceReg);
var province = '';
var city = '';
var district = '';
var detail = '';
if (matchedProvince) {
province = matchedProvince[0];
cellValue = cellValue.slice(matchedProvince.index + province.length);
}
var matchedCity = cellValue.match(cityReg);
if (matchedCity) {
city = matchedCity[0];
cellValue = cellValue.slice(matchedCity.index + city.length);
} else if (province.includes('北京') || province.includes('天津') || province.includes('重庆') || province.includes('上海')) {
city = province;
}
var matchedDistrict = cellValue.match(districtReg);
if (matchedDistrict) {
district = matchedDistrict[0];
cellValue = cellValue.slice(matchedDistrict.index + district.length);
}
detail = cellValue;
return {
province: province,
city: city,
district: district,
detail: detail
};
};
/**
* Formats latitude and longitude values to the specified precision (6 decimal places)
* @param {object} point - { lng, lat }
* @returns {object} normalized point - { lng, lat }
*/
var normalizeMapPoint = function normalizeMapPoint(point) {
if (!point || _typeof(point) !== 'object') return {
lng: null,
lat: null
};
var _ref2 = point || {},
lng = _ref2.lng,
lat = _ref2.lat;
var digits = 6;
var formatNumber = function formatNumber(v) {
var num = isNumber(v) ? v : parseFloat(String(v).trim());
return isNumber(num) ? parseFloat(num.toFixed(digits)) : null;
};
return {
lng: formatNumber(lng),
lat: formatNumber(lat)
};
};
export { formatTextToGeolocation, normalizeMapPoint, parseDMS, parseLatLng, parseLatLngDMS, parseLngLatDegree };