1
0
Fork 0

Add typing to formatDistance.ts.

This commit is contained in:
Kitteh 2021-05-08 00:58:28 +01:00
parent 5a4df25547
commit dd9a63a8db
3 changed files with 11 additions and 8 deletions

View file

@ -1,8 +1,8 @@
import { de, enGB, fr, it, nl, ru } from 'date-fns/locale' import { de, enGB, fr, it, nl, ru } from 'date-fns/locale'
import { formatDistance as formatDistanceReal} from 'date-fns'; import { formatDistance as formatDistanceReal} from 'date-fns';
import { pageState } from "./globalPageState.ts"; import { pageState } from "./globalPageState";
function getLocale() { function getLocale(): any {
return { return {
"en": enGB, "en": enGB,
"fr": fr, "fr": fr,
@ -13,6 +13,6 @@ function getLocale() {
}[pageState.language]; }[pageState.language];
} }
export function formatDistance(d1, d2) { export function formatDistance(d1: Date, d2: Date): string {
return formatDistanceReal(d1, d2, { locale: getLocale() }); return formatDistanceReal(d1, d2, { locale: getLocale() });
} }

View file

@ -22,7 +22,7 @@ import { makeElement } from "./htmlUtils";
import { pageState } from "./globalPageState.ts"; import { pageState } from "./globalPageState.ts";
// Translations // Translations
import { formatDistance } from './formatDistance.js'; import { formatDistance } from './formatDistance';
import i18next from 'i18next'; import i18next from 'i18next';
import translations from './translations/index.mjs' import translations from './translations/index.mjs'

View file

@ -2,11 +2,14 @@ export function removeDoubleSlash(str: string): string {
return str.replace(/\/\/+/g, "/"); return str.replace(/\/\/+/g, "/");
} }
export const getObjectKeys = (obj: Record<string, unknown>) => Object.getOwnPropertyNames(obj); export const getObjectKeys =
export const objectToMap = (obj: Record<string, unknown>) => new Map(Object.entries(obj)); (obj: Record<string, unknown>): string[] => Object.getOwnPropertyNames(obj);
export const sortedObjectMap = (obj: Record<string, unknown>) => new Map(Object.entries(obj).sort()); export const objectToMap =
(obj: Record<string, unknown>): Map<any, any> => new Map(Object.entries(obj));
export const sortedObjectMap =
(obj: Record<string, unknown>): Map<any, any> => new Map(Object.entries(obj).sort());
export function getKeyByObjectPropertyValue(map: Record<string, unknown>, searchValue: any) { export function getKeyByObjectPropertyValue(map: Record<string, unknown>, searchValue: unknown): string {
for (const key of getObjectKeys(map)) { for (const key of getObjectKeys(map)) {
if ((map as any)[key] === searchValue) if ((map as any)[key] === searchValue)
return key; return key;