-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
50 lines (40 loc) · 1.5 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { formatDistance } from 'date-fns';
import { defaultErrorHandler, formatAnyError } from 'f61ui/errors';
import { datetimeRFC3339 } from 'f61ui/types';
export function unrecognizedValue(value: never): never {
throw new Error(`Unrecognized value: ${value}`);
}
let uniqueDomIdCounter = 0;
export function uniqueDomId(): number {
return ++uniqueDomIdCounter;
}
export function relativeDateFormat(dateIso: datetimeRFC3339): string {
return formatDistance(new Date(dateIso), new Date(), { addSuffix: true, includeSeconds: true });
}
export function formatDistance2(a: datetimeRFC3339, b: datetimeRFC3339): string {
return formatDistance(new Date(a), new Date(b), { includeSeconds: true });
}
// - cannot "await" unless "await" sits in a function that itself is "async"
// - even if you catch all exceptions in "async" func, tslint no-floating-promises still
// complains if your *caller* calls this function whose promise will never reject
// - therefore this hack was made to please tslint
export function shouldAlwaysSucceed(prom: Promise<any>): void {
prom.then(
() => {
/* noop */
},
(innerErr) => {
const outerErr = new Error(
'shouldAlwaysSucceed: problem in error branch?: ' + formatAnyError(innerErr),
);
defaultErrorHandler(outerErr);
},
);
}
export function focusRetainer(logicThatMessesUpFocus: () => void) {
const activeElementBefore = document.activeElement;
logicThatMessesUpFocus();
if (activeElementBefore instanceof HTMLElement) {
activeElementBefore.focus();
}
}