From dd9a63a8db7ebc07dcef5fafb728223f7da69e8f Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sat, 8 May 2021 00:58:28 +0100 Subject: [PATCH] Add typing to formatDistance.ts. --- src/{formatDistance.js => formatDistance.ts} | 6 +++--- src/main.js | 2 +- src/utils.ts | 11 +++++++---- 3 files changed, 11 insertions(+), 8 deletions(-) rename src/{formatDistance.js => formatDistance.ts} (69%) diff --git a/src/formatDistance.js b/src/formatDistance.ts similarity index 69% rename from src/formatDistance.js rename to src/formatDistance.ts index 8f888a2..7dfb796 100644 --- a/src/formatDistance.js +++ b/src/formatDistance.ts @@ -1,8 +1,8 @@ import { de, enGB, fr, it, nl, ru } from 'date-fns/locale' import { formatDistance as formatDistanceReal} from 'date-fns'; -import { pageState } from "./globalPageState.ts"; +import { pageState } from "./globalPageState"; -function getLocale() { +function getLocale(): any { return { "en": enGB, "fr": fr, @@ -13,6 +13,6 @@ function getLocale() { }[pageState.language]; } -export function formatDistance(d1, d2) { +export function formatDistance(d1: Date, d2: Date): string { return formatDistanceReal(d1, d2, { locale: getLocale() }); } \ No newline at end of file diff --git a/src/main.js b/src/main.js index 220d2a4..0e67bfd 100644 --- a/src/main.js +++ b/src/main.js @@ -22,7 +22,7 @@ import { makeElement } from "./htmlUtils"; import { pageState } from "./globalPageState.ts"; // Translations -import { formatDistance } from './formatDistance.js'; +import { formatDistance } from './formatDistance'; import i18next from 'i18next'; import translations from './translations/index.mjs' diff --git a/src/utils.ts b/src/utils.ts index 0849011..404e5d4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,11 +2,14 @@ export function removeDoubleSlash(str: string): string { return str.replace(/\/\/+/g, "/"); } -export const getObjectKeys = (obj: Record) => Object.getOwnPropertyNames(obj); -export const objectToMap = (obj: Record) => new Map(Object.entries(obj)); -export const sortedObjectMap = (obj: Record) => new Map(Object.entries(obj).sort()); +export const getObjectKeys = + (obj: Record): string[] => Object.getOwnPropertyNames(obj); +export const objectToMap = + (obj: Record): Map => new Map(Object.entries(obj)); +export const sortedObjectMap = + (obj: Record): Map => new Map(Object.entries(obj).sort()); -export function getKeyByObjectPropertyValue(map: Record, searchValue: any) { +export function getKeyByObjectPropertyValue(map: Record, searchValue: unknown): string { for (const key of getObjectKeys(map)) { if ((map as any)[key] === searchValue) return key;