/** * And so should not contain and platforma specific imports. */ export function getValueMapFromList( list: T[], key: K, valueKey: V, filterUndefined: boolean = true ): Record { if (filterUndefined) { list = list.filter( (f) => (f[valueKey] as unknown) !== undefined && (f[key] as unknown) !== undefined ); } return list.reduce((acc, f) => { const keyValue = String(f[key]); const value = f[valueKey]; acc[keyValue] = value; return acc; }, {} as Record); } export function getRandomString(): string { const randomNumber = Math.random().toString(36).slice(2, 8); const currentTime = Date.now().toString(36); return `${randomNumber}-${currentTime}`; } export async function sleep(durationMilliseconds: number = 1000) { return new Promise((r) => setTimeout(() => r(null), durationMilliseconds)); } export function getMapFromList( list: T[], name: K ): Record { const acc: Record = {}; for (const t of list) { const key = t[name]; if (key === undefined) { continue; } acc[String(key)] = t; } return acc; } export function getDefaultMapFromList( list: T[] | string[], defaultValue: D, name?: K ): Record { const acc: Record = {}; if (typeof list[0] === 'string') { for (const l of list as string[]) { acc[l] = defaultValue; } return acc; } if (!name) { return {}; } for (const l of list as T[]) { const key = String(l[name]); acc[key] = defaultValue; } return acc; } export function getListFromMap(map: Record): T[] { return Object.keys(map).map((n) => map[n]); } export function getIsNullOrUndef(value: unknown): boolean { return value === null || value === undefined; } export function titleCase(phrase: string): string { return phrase .split(' ') .map((word) => { const wordLower = word.toLowerCase(); if (['and', 'an', 'a', 'from', 'by', 'on'].includes(wordLower)) { return wordLower; } return wordLower[0].toUpperCase() + wordLower.slice(1); }) .join(' '); } export function invertMap(map: Record): Record { const keys = Object.keys(map); const inverted: Record = {}; for (const key of keys) { const val = map[key]; inverted[val] = key; } return inverted; } export function time(func: (...args: K[]) => T, ...args: K[]): T { const name = func.name; console.time(name); const stuff = func(...args); console.timeEnd(name); return stuff; } export async function timeAsync( func: (...args: K[]) => Promise, ...args: K[] ): Promise { const name = func.name; console.time(name); const stuff = await func(...args); console.timeEnd(name); return stuff; }