MDK Logo

Utilities

Helper functions for formatting, validation, and data manipulation

Utilities

The @mdk/core package provides 15 utility modules with helper functions for common operations.

Import

import {
  formatNumber,
  formatHashrate,
  formatDate,
  formatRelativeTime,
  cn,
  isEmpty,
  isValidEmail,
} from '@mdk/core'

Formatting utilities

formatNumber

Format numbers with locale formatting and configurable options.

import { formatNumber, FALLBACK } from '@mdk/core'

formatNumber(1234.567)                          // "1,234.57"
formatNumber(null)                               // "-"
formatNumber(1234, { minimumFractionDigits: 2 }) // "1,234.00"
formatNumber(undefined, {}, 'N/A')               // "N/A"

formatHashrate

Format hashrate values with rounding.

import { formatHashrate } from '@mdk/core'

formatHashrate(150.456)  // "150.46"
formatHashrate(null)     // "-"

formatCurrency

Format currency values.

import { formatCurrency } from '@mdk/core'

formatCurrency(1234.56, 'USD')  // "$1,234.56"
formatCurrency(0.00012345, 'BTC')  // "₿0.00012345"

getPercentFormattedNumber

Format numbers as percentages.

import { getPercentFormattedNumber } from '@mdk/core'

getPercentFormattedNumber(0.75)     // "75%"
getPercentFormattedNumber(0.1234, 1) // "12.3%"

formatValueUnit

Format value-unit objects.

import { formatValueUnit } from '@mdk/core'

formatValueUnit(150, 'TH/s')  // "150 TH/s"

Date utilities

formatDate

Format dates with customizable patterns.

import { formatDate } from '@mdk/core'

formatDate(new Date())                    // "Jan 15, 2025"
formatDate(1705334400000, { format: 'yyyy-MM-dd' })  // "2025-01-15"

formatRelativeTime

Format dates as relative time strings.

import { formatRelativeTime } from '@mdk/core'

formatRelativeTime(new Date(Date.now() - 3600000))  // "1h ago"
formatRelativeTime(new Date(Date.now() - 86400000)) // "1d ago"

formatChartDate

Format timestamps for chart display.

import { formatChartDate } from '@mdk/core'

formatChartDate(1705334400)           // "Jan 15"
formatChartDate(1705334400, true)     // "Jan 15, 2025"

isValidTimestamp

Check if a timestamp is valid.

import { isValidTimestamp } from '@mdk/core'

isValidTimestamp(1705334400000)  // true
isValidTimestamp('invalid')       // false

parseMonthLabelToDate

Parse month labels to Date objects.

import { parseMonthLabelToDate } from '@mdk/core'

parseMonthLabelToDate('01-26')    // Date(2026, 0, 1)
parseMonthLabelToDate('03-2025')  // Date(2025, 2, 1)

getPastDateFromDate

Get a date in the past.

import { getPastDateFromDate } from '@mdk/core'

getPastDateFromDate({ dateTs: Date.now(), days: 7 })  // 7 days ago

Validation utilities

isEmpty

Check if a value is empty.

import { isEmpty } from '@mdk/core'

isEmpty(null)       // true
isEmpty('')         // true
isEmpty([])         // true
isEmpty({})         // true
isEmpty('hello')    // false
isEmpty([1, 2, 3])  // false

isValidEmail

Validate email addresses.

import { isValidEmail } from '@mdk/core'

isValidEmail('user@example.com')  // true
isValidEmail('invalid')            // false

isValidUrl

Validate URLs.

import { isValidUrl } from '@mdk/core'

isValidUrl('https://example.com')  // true
isValidUrl('not-a-url')            // false

isNil

Check if value is null or undefined.

import { isNil } from '@mdk/core'

isNil(null)       // true
isNil(undefined)  // true
isNil(0)          // false
isNil('')         // false

isPlainObject

Check if value is a plain object.

import { isPlainObject } from '@mdk/core'

isPlainObject({})           // true
isPlainObject({ a: 1 })     // true
isPlainObject([])           // false
isPlainObject(new Date())   // false

Class name utilities

cn

Merge class names using clsx and tailwind-merge.

import { cn } from '@mdk/core'

cn('px-4', 'py-2')                    // "px-4 py-2"
cn('text-red', isError && 'bg-red')   // conditional classes
cn('p-4', { 'hidden': !visible })     // object syntax

Conversion utilities

toMW / toMWh

Convert watts to megawatts.

import { toMW, toMWh } from '@mdk/core'

toMW(1000000)   // 1
toMWh(1000000)  // 1

toPHS

Convert raw hashrate to PH/s.

import { toPHS } from '@mdk/core'

toPHS(1000000000000000)  // 1

convertMpaToBar

Convert pressure units.

import { convertMpaToBar } from '@mdk/core'

convertMpaToBar(0.1)  // 1

unitToKilo

Convert to kilo units.

import { unitToKilo } from '@mdk/core'

unitToKilo(1000)  // 1

Number utilities

percentage

Calculate percentage.

import { percentage } from '@mdk/core'

percentage(25, 100)  // 25
percentage(1, 4)     // 25

getPercentChange

Calculate percentage change.

import { getPercentChange } from '@mdk/core'

getPercentChange(110, 100)  // 10
getPercentChange(90, 100)   // -10

convertUnits

Convert between SI-prefix units.

import { convertUnits } from '@mdk/core'

convertUnits(1, 'k', 'M')  // 0.001
convertUnits(1000, 'decimal', 'k')  // 1

safeNumber

Safely convert to number.

import { safeNumber } from '@mdk/core'

safeNumber('123')      // 123
safeNumber('invalid')  // 0
safeNumber(null)       // 0

String utilities

toTitleCase

Convert string to Title Case.

import { toTitleCase } from '@mdk/core'

toTitleCase('hello world')  // "Hello World"

formatMacAddress

Format MAC addresses.

import { formatMacAddress } from '@mdk/core'

formatMacAddress('aa:bb:cc:dd:ee:ff')  // "AA:BB:CC:DD:EE:FF"

safeString

Safely convert to string.

import { safeString } from '@mdk/core'

safeString(123)    // "123"
safeString(null)   // ""

Time utilities

secondsToMs

Convert seconds to milliseconds.

import { secondsToMs } from '@mdk/core'

secondsToMs(60)  // 60000

breakTimeIntoIntervals

Split time range into intervals.

import { breakTimeIntoIntervals } from '@mdk/core'

breakTimeIntoIntervals(start, end, 3600000)  // Array of 1-hour intervals

timeRangeWalker

Generator for iterating through time ranges.

import { timeRangeWalker } from '@mdk/core'

for (const interval of timeRangeWalker(start, end, duration)) {
  // Process each interval
}

Color utilities

hexToRgba

Convert hex color to rgba.

import { hexToRgba } from '@mdk/core'

hexToRgba('#72F59E', 0.5)  // "rgba(114, 245, 158, 0.5)"

Array utilities

getNestedValue

Get nested value by dot-path.

import { getNestedValue } from '@mdk/core'

getNestedValue({ a: { b: 1 } }, 'a.b')  // 1

getWeightedAverage

Calculate weighted average.

import { getWeightedAverage } from '@mdk/core'

getWeightedAverage(items, 'value', 'weight')

circularArrayAccess

Create infinite cycling generator.

import { circularArrayAccess } from '@mdk/core'

const colors = circularArrayAccess(['red', 'green', 'blue'])
colors.next().value  // 'red'
colors.next().value  // 'green'
colors.next().value  // 'blue'
colors.next().value  // 'red' (cycles)

On this page