Query and display weather data for all country

This commit is contained in:
Guntis Smaukstelis
2023-12-24 23:54:16 +02:00
parent 7305718c34
commit a48b9df91f
35 changed files with 193 additions and 63 deletions
+47
View File
@@ -0,0 +1,47 @@
import moment from "moment";
export function formatDateString(str: string): string {
const date = moment(new Date(str));
switch (str.length) {
// year: 2023
case 4: return date.format("yyyy");
// year-month: 2023-06
case 7: return date.format("yyyy-MM");
// year-month-day: 2023-06-23
case 10: return date.format("yyyy-MM-DD");
// year-month-day hour:minute 2023-06-23T23:59
default: return date.format("HH:mm");
}
}
export interface QueryResult {
query: DataQuery;
result: {[key: string]: any};
}
export interface DataQuery {
cities: string[];
field: string;
granularity: string;
key: string;
}
export function isQueryResult(variable: any): variable is QueryResult {
return variable
&& variable.query
&& isDataQuery(variable.query)
&& variable.result
&& (
typeof variable.result === "object"
&& !Array.isArray(variable.result)
&& variable.result !== null
);
}
export function isDataQuery(variable: any): variable is DataQuery {
return variable &&
Array.isArray(variable.cities) &&
typeof variable.field === 'string' &&
typeof variable.granularity === 'string' &&
typeof variable.key === 'string';
}