Debian Codegraph @

frontend/src/stores/format.ts

raw
1 import { format } from "d3-format";
2 import { derived } from "svelte/store";
3
4 import type { FormatterContext } from "../format";
5 import {
6 dateFormat,
7 formatter_context,
8 localeFormatter,
9 replaceNumbers,
10 timeFilterDateFormat,
11 } from "../format";
12 import { incognito, interval, locale, precisions } from ".";
13
14 const short_format = format(".3s");
15
16 /** Render a number to a short string, for example for the y-axis of a line chart. */
17 export const short = derived(
18 incognito,
19 ($incognito): ((number: number | { valueOf(): number }) => string) =>
20 $incognito ? (n) => replaceNumbers(short_format(n)) : short_format,
21 );
22
23 /** Format a number for which the currency is not known. */
24 export const num = derived(locale, ($locale) => localeFormatter($locale));
25
26 /** Formatting context for currencies. */
27 export const ctx = derived(
28 [incognito, locale, precisions],
29 ([$incognito, $locale, $precisions]): FormatterContext =>
30 formatter_context($incognito, $locale, $precisions),
31 );
32
33 export const currentDateFormat = derived(interval, (val) => dateFormat[val]);
34 export const currentTimeFilterDateFormat = derived(
35 interval,
36 (val) => timeFilterDateFormat[val],
37 );