formatNumber (_components/format.ts)

appwrite · apps/console/src/components/pages/projects/$projectId/analytics/_components/format.ts · typescript · BSD-3-Clause
analytics

* Shared value formatting for the analytics screens. Nullish input is accepted because the API only populates the session-shaped measures on the flat aggregate; on breakdown and time-series rows they are null. /

// Source: https://github.com/appwrite/appwrite | License: BSD-3-Clause | Copyright 2019 Appwrite (https://appwrite.io) and individual contributors
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Shared value formatting for the analytics screens.
 *
 * Nullish input is accepted because the API only populates the session-shaped
 * measures on the flat aggregate; on breakdown and time-series rows they are
 * null.
 */

const COMPACT_NUMBER_THRESHOLD = 1000

export function formatNumber(num: number | null | undefined): string {
  if (num == null || !Number.isFinite(num)) return '0'
  if (Math.abs(num) >= 1000000) {
    return (num / 1000000).toFixed(1) + 'M'
  }
  if (Math.abs(num) >= COMPACT_NUMBER_THRESHOLD) {
    return (num / 1000).toFixed(1) + 'K'
  }
  return Str

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key