MDK Logo

Utils

CSV and file download helpers, plus time-range and zoned-day helpers

Export helpers for turning rows into downloadable files, and helpers for working with time ranges and zoned days.

Package

@tetherto/mdk-ui-foundation

Utilities

@tetherto/mdk-ui-foundation

Import the public APIs on this page from @tetherto/mdk-ui-foundation.

allowedGranularities

The granularities that make sense for a range of ms: hourly up to a month, daily up to three years, weekly from two weeks, monthly from two months.

Function

(ms: number) => Granularity[]

downloadCsv

Downloads rows of objects as a CSV file (see toCsv for the columns).

Function

(rows: readonly Row[], filename = 'export.csv', columns?: readonly (keyof Row & string)[]) => void

downloadJson

Downloads data as a pretty-printed JSON file.

Function

(data: unknown, filename = 'export.json') => void

downloadText

Saves text as a file named filename through the browser's download. Like downloadCsv and downloadJson, it takes the content first.

Function

(text: string, filename = 'export.txt', type = 'text/plain') => void

periodRange

A period's [start, end) in ms. Rolling presets end at now, rounded up to the minute; a custom range runs from its first day's midnight to the midnight after its last day, in timezone. A custom range missing a day defaults to the last…

Function

(period: Period, timezone: string, now = Date.now()) => { start: number; end: number }

toCsv

Rows of objects as CSV text. Columns are columns when given (which also orders and filters them), else the keys of the first row; a header line names them.

Function

(rows: readonly Row[], columns: readonly (keyof Row & string)[] = Object.keys(rows[0] ?? {}) as (keyof Row & string)[]) => string

zonedDayOf

The YYYY-MM-DD day ts falls on in timezone.

Function

(ts: number, timezone: string) => string

zonedDayStart

Midnight at the start of a YYYY-MM-DD day in timezone, as a UTC instant.

Function

(day: string, timezone: string) => number

On this page