From 5c3f09ec2cc531131a97af39e4d8099adbfccff1 Mon Sep 17 00:00:00 2001
From: muerwre <gotham48@gmail.com>
Date: Thu, 21 Feb 2019 12:05:10 +0700
Subject: [PATCH] typed some utils

---
 src/utils/{clusterIcon.js => clusterIcon.ts} |  4 ++--
 src/utils/{format.js => format.ts}           |  8 ++++---
 src/utils/{geom.js => geom.ts}               | 22 +++++++++++---------
 src/utils/gpx.ts                             |  3 +--
 src/utils/{history.js => history.ts}         | 19 ++++++++++++-----
 5 files changed, 34 insertions(+), 22 deletions(-)
 rename src/utils/{clusterIcon.js => clusterIcon.ts} (78%)
 rename src/utils/{format.js => format.ts} (76%)
 rename src/utils/{geom.js => geom.ts} (56%)
 rename src/utils/{history.js => history.ts} (68%)

diff --git a/src/utils/clusterIcon.js b/src/utils/clusterIcon.ts
similarity index 78%
rename from src/utils/clusterIcon.js
rename to src/utils/clusterIcon.ts
index 7d45b89..87aaaaa 100644
--- a/src/utils/clusterIcon.js
+++ b/src/utils/clusterIcon.ts
@@ -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: `
     <div class="custom-marker-cluster">
       <span>${cluster.getChildCount()}</span>          
diff --git a/src/utils/format.js b/src/utils/format.ts
similarity index 76%
rename from src/utils/format.js
rename to src/utils/format.ts
index e12db6d..7777f17 100644
--- a/src/utils/format.js
+++ b/src/utils/format.ts
@@ -1,11 +1,13 @@
 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'];
 
-export const toHours = (info) => {
-  const hrs = parseInt(Number(info), 10);
+export const toHours = (info: number): string => {
+  const hrs = parseInt(String(info), 10);
   const min = Math.round((Number(info) - hrs) * 60);
   const lmin = min < 10 ? '0' + min : min;
   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) || ''))
+);
diff --git a/src/utils/geom.js b/src/utils/geom.ts
similarity index 56%
rename from src/utils/geom.js
rename to src/utils/geom.ts
index 09bf17f..24ce715 100644
--- a/src/utils/geom.js
+++ b/src/utils/geom.ts
@@ -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)),
   lng: (l2.lng + ((l1.lng - l2.lng) / 2))
 });
 
-export const deg2rad = deg => ((deg * Math.PI) / 180);
-export const rad2deg = rad => ((rad / Math.PI) * 180);
+export const deg2rad = (deg: number): number => ((deg * Math.PI) / 180);
+export const rad2deg = (rad: number): number => ((rad / Math.PI) * 180);
 
-window.rad2deg = rad2deg;
-
-export const findDistance = (t1, n1, t2, n2) => {
+export const findDistance = (t1: number, n1: number, t2: number, n2: number): number => {
   // convert coordinates to radians
   const lat1 = deg2rad(t1);
   const lon1 = deg2rad(n1);
@@ -23,12 +26,11 @@ export const findDistance = (t1, n1, t2, n2) => {
   const a = (Math.sin(dlat / 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 dm = c * 3961; // great circle distance in miles
   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);
 };
 
-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'
+);
diff --git a/src/utils/gpx.ts b/src/utils/gpx.ts
index 6d873d0..db71a08 100644
--- a/src/utils/gpx.ts
+++ b/src/utils/gpx.ts
@@ -5,7 +5,6 @@ export interface IRoutePoint {
   lng: number,
 }
 
-
 interface IGPXSticker {
   latlng: IRoutePoint,
   text?: string,
@@ -45,7 +44,7 @@ export const getGPXString = ({ route, title, stickers }: IGetGPXString): string
     </gpx>
 `);
 
-export const downloadGPXTrack = ({ track, title }: { track: string, title?: string }) => (
+export const downloadGPXTrack = ({ track, title }: { track: string, title?: string }): void => (
   saveAs(
     new Blob([track], { type: 'application/gpx+xml;charset=utf-8' }),
     `${title || 'track'}.gpx`
diff --git a/src/utils/history.js b/src/utils/history.ts
similarity index 68%
rename from src/utils/history.js
rename to src/utils/history.ts
index 8094dac..d85a771 100644
--- a/src/utils/history.js
+++ b/src/utils/history.ts
@@ -1,8 +1,16 @@
 import { history } from '$redux/store';
 
-export const getPath = () => (window.location && window.location.pathname);
-export const pushPath = url => history.push(url);
-export const replacePath = url => history.replace(url);
+interface IUrlData {
+  path: string,
+  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()) => {
   const [, path, mode] = url.split('/');
@@ -31,14 +39,15 @@ export const pushLoaderState = state => {
 };
 
 export const pushNetworkInitError = () => {
-  document.getElementById('loader-error').style.opacity = 1;
+  document.getElementById('loader-error').style.opacity = String(1);
 };
 
 export const copyToClipboard = str => {
   const el = document.createElement('textarea');
   el.value = str;
   el.setAttribute('readonly', '');
-  el.style = { position: 'absolute', left: '-9999px' };
+  el.style.position = 'absolute';
+  el.style.left = '-9999px';
   document.body.appendChild(el);
   el.select();
   document.execCommand('copy');