typed some utils

This commit is contained in:
muerwre 2019-02-21 12:05:10 +07:00
parent cf6866240b
commit 5c3f09ec2c
5 changed files with 34 additions and 22 deletions

View file

@ -1,6 +1,6 @@
import { divIcon } from 'leaflet'; import { DivIcon, divIcon } from 'leaflet';
export const clusterIcon = cluster => divIcon({ export const clusterIcon = (cluster): DivIcon => divIcon({
html: ` html: `
<div class="custom-marker-cluster"> <div class="custom-marker-cluster">
<span>${cluster.getChildCount()}</span> <span>${cluster.getChildCount()}</span>

View file

@ -1,11 +1,13 @@
const ru = [' ','\\.',',',':','\\?','#','Я','я','Ю','ю','Ч','ч','Ш','ш','Щ','щ','Ж','ж','А','а','Б','б','В','в','Г','г','Д','д','Е','е','Ё','ё','З','з','И','и','Й','й','К','к','Л','л','М','м','Н','н', 'О','о','П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ы','ы','Ь','ь','Ъ','ъ','Э','э']; const ru = [' ','\\.',',',':','\\?','#','Я','я','Ю','ю','Ч','ч','Ш','ш','Щ','щ','Ж','ж','А','а','Б','б','В','в','Г','г','Д','д','Е','е','Ё','ё','З','з','И','и','Й','й','К','к','Л','л','М','м','Н','н', 'О','о','П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ы','ы','Ь','ь','Ъ','ъ','Э','э'];
const en = ['_','','','','','','Ya','ya','Yu','yu','Ch','ch','Sh','sh','Sh','sh','Zh','zh','A','a','B','b','V','v','G','g','D','d','E','e','E','e','Z','z','I','i','J','j','K','k','L','l','M','m','N','n', 'O','o','P','p','R','r','S','s','T','t','U','u','F','f','H','h','C','c','Y','y','','','','','E', 'e']; const en = ['_','','','','','','Ya','ya','Yu','yu','Ch','ch','Sh','sh','Sh','sh','Zh','zh','A','a','B','b','V','v','G','g','D','d','E','e','E','e','Z','z','I','i','J','j','K','k','L','l','M','m','N','n', 'O','o','P','p','R','r','S','s','T','t','U','u','F','f','H','h','C','c','Y','y','','','','','E', 'e'];
export const toHours = (info) => { export const toHours = (info: number): string => {
const hrs = parseInt(Number(info), 10); const hrs = parseInt(String(info), 10);
const min = Math.round((Number(info) - hrs) * 60); const min = Math.round((Number(info) - hrs) * 60);
const lmin = min < 10 ? '0' + min : min; const lmin = min < 10 ? '0' + min : min;
return `${hrs}:${lmin}`; return `${hrs}:${lmin}`;
}; };
export const toTranslit = string => ru.reduce((text, el, i) => (text.replace(new RegExp(ru[i], 'g'), en[i])), (String(string) || '')); export const toTranslit = (string: string): string => (
ru.reduce((text, el, i) => (text.replace(new RegExp(ru[i], 'g'), en[i])), (String(string) || ''))
);

View file

@ -1,14 +1,17 @@
export const middleCoord = (l1, l2) => ({ interface ILatLng {
lat: number,
lng: number,
}
export const middleCoord = (l1: ILatLng, l2: ILatLng): ILatLng => ({
lat: (l2.lat + ((l1.lat - l2.lat) / 2)), lat: (l2.lat + ((l1.lat - l2.lat) / 2)),
lng: (l2.lng + ((l1.lng - l2.lng) / 2)) lng: (l2.lng + ((l1.lng - l2.lng) / 2))
}); });
export const deg2rad = deg => ((deg * Math.PI) / 180); export const deg2rad = (deg: number): number => ((deg * Math.PI) / 180);
export const rad2deg = rad => ((rad / Math.PI) * 180); export const rad2deg = (rad: number): number => ((rad / Math.PI) * 180);
window.rad2deg = rad2deg; export const findDistance = (t1: number, n1: number, t2: number, n2: number): number => {
export const findDistance = (t1, n1, t2, n2) => {
// convert coordinates to radians // convert coordinates to radians
const lat1 = deg2rad(t1); const lat1 = deg2rad(t1);
const lon1 = deg2rad(n1); const lon1 = deg2rad(n1);
@ -23,12 +26,11 @@ export const findDistance = (t1, n1, t2, n2) => {
const a = (Math.sin(dlat / 2) ** 2) + const a = (Math.sin(dlat / 2) ** 2) +
(Math.cos(lat1) * Math.cos(lat2) * (Math.sin(dlon / 2) ** 2)); (Math.cos(lat1) * Math.cos(lat2) * (Math.sin(dlon / 2) ** 2));
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // great circle distance in radians const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // great circle distance in radians
// const dm = c * 3961; // great circle distance in miles
const dk = c * 6373; // great circle distance in km const dk = c * 6373; // great circle distance in km
// round the results down to the nearest 1/1000
// const mi = round(dm);
return (Math.round(dk * 1000) / 1000); return (Math.round(dk * 1000) / 1000);
}; };
export const getLabelDirection = angle => (((angle % Math.PI) >= -(Math.PI / 2) && (angle % Math.PI) <= (Math.PI / 2)) ? 'left' : 'right'); export const getLabelDirection = (angle: number): 'left' | 'right' => (
((angle % Math.PI) >= -(Math.PI / 2) && (angle % Math.PI) <= (Math.PI / 2)) ? 'left' : 'right'
);

View file

@ -5,7 +5,6 @@ export interface IRoutePoint {
lng: number, lng: number,
} }
interface IGPXSticker { interface IGPXSticker {
latlng: IRoutePoint, latlng: IRoutePoint,
text?: string, text?: string,
@ -45,7 +44,7 @@ export const getGPXString = ({ route, title, stickers }: IGetGPXString): string
</gpx> </gpx>
`); `);
export const downloadGPXTrack = ({ track, title }: { track: string, title?: string }) => ( export const downloadGPXTrack = ({ track, title }: { track: string, title?: string }): void => (
saveAs( saveAs(
new Blob([track], { type: 'application/gpx+xml;charset=utf-8' }), new Blob([track], { type: 'application/gpx+xml;charset=utf-8' }),
`${title || 'track'}.gpx` `${title || 'track'}.gpx`

View file

@ -1,8 +1,16 @@
import { history } from '$redux/store'; import { history } from '$redux/store';
export const getPath = () => (window.location && window.location.pathname); interface IUrlData {
export const pushPath = url => history.push(url); path: string,
export const replacePath = url => history.replace(url); mode: 'edit' | '',
host: string,
hash: string,
protocol: 'http' | 'https',
}
export const getPath = (): string => (window.location && window.location.pathname);
export const pushPath = (url: string): string => history.push(url);
export const replacePath = (url: string): string => history.replace(url);
export const getUrlData = (url = getPath()) => { export const getUrlData = (url = getPath()) => {
const [, path, mode] = url.split('/'); const [, path, mode] = url.split('/');
@ -31,14 +39,15 @@ export const pushLoaderState = state => {
}; };
export const pushNetworkInitError = () => { export const pushNetworkInitError = () => {
document.getElementById('loader-error').style.opacity = 1; document.getElementById('loader-error').style.opacity = String(1);
}; };
export const copyToClipboard = str => { export const copyToClipboard = str => {
const el = document.createElement('textarea'); const el = document.createElement('textarea');
el.value = str; el.value = str;
el.setAttribute('readonly', ''); el.setAttribute('readonly', '');
el.style = { position: 'absolute', left: '-9999px' }; el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el); document.body.appendChild(el);
el.select(); el.select();
document.execCommand('copy'); document.execCommand('copy');