Point to an element to see the design tokens behind it.
Back to work

Diabetic Utils

TypeScript library for glucose, A1C, and Time in Range (TIR) calculations, with 100% test coverage. Featured in Google AI Overview for diabetes developer tools.

My role
Creator & Maintainer
Timeline
2023 to present
Built with
  • TypeScript
  • Vitest
  • npm
  • Open Source
  • Clinical Algorithms
  • HealthKit

Extract the calculations I kept rewriting

While working on the GlucoseIQ Apple Watch app, I kept rewriting the same utility functions. I wanted those calculations in one tested TypeScript library that other diabetes developers could use too.

Calculation and data-handling problems
  • Glucose unit conversion (mg/dL to mmol/L) rewritten from scratch in each project

  • A1C formulas copied from medical papers without validation

  • Time in Range (TIR) metrics implemented inconsistently across apps

  • No type-safe APIs for glucose data structures

  • Calculation errors that could affect patient care

Test calculations and edge cases

Diabetic Utils is a TypeScript library of calculations for glucose monitoring, A1C estimation, and Time in Range.

100% test coverage

The Vitest suite covers:

  • Every formula, checked against known medical values

  • Edge cases like boundary values, negative numbers, and zero

  • Input validation and error handling

  • Precision and rounding behavior

One API for explicit units

Example from the README:

typescript
import {
  mgDlToMmolL,
  mmolLToMgDl,
  estimateGMI,
  estimateA1CFromAverage
} from 'diabetic-utils'

// Glucose unit conversions
mgDlToMmolL(180)  // -> 10.0
mmolLToMgDl(5.5)  // -> 99

// GMI calculation (multiple input formats)
estimateGMI(100, 'mg/dL')              // -> 5.4
estimateGMI('5.5 mmol/L')              // -> 5.4
estimateGMI({ value: 100, unit: 'mg/dL' })  // -> 5.4

// A1C estimation
estimateA1CFromAverage(154, 'mg/dL')  // -> 7.0
Conversion, A1C, and Time in Range API reference

Glucose conversions

Type-safe functions for converting between mg/dL and mmol/L:

  • mgDlToMmolL() converts mg/dL to mmol/L and rounds the result

  • mmolLToMgDl() converts mmol/L to mg/dL

  • Handles edge cases and validates input ranges

  • Uses the standard conversion factor (18.0182) from clinical guidelines

A1C calculations

Estimated A1C (eA1C) from average glucose, using the ADAG formula:

  • calculateA1C() converts average glucose to an estimated A1C percentage

  • Based on the ADAG Study formula, A1C = (avg_glucose + 46.7) / 28.7

  • Accepts mg/dL or mmol/L input

  • Returns A1C as a percentage (for example, 7.2%)

Time in Range (TIR)

The TIR metrics follow the ADA/ATTD consensus guidelines:

  • calculateTIR() returns the percentage of readings in the target range (70-180 mg/dL)

  • calculateTBR() returns time below range (hypoglycemia detection)

  • calculateTAR() returns time above range (hyperglycemia detection)

  • Configurable target ranges for personalized therapy goals

  • Handles missing data and validates reading counts

Enhanced TIR (5-range)

Enhanced TIR splits readings into five ranges that mirror the clinical targets.

typescript
import { calculateEnhancedTIR } from 'diabetic-utils'
import type { GlucoseReading } from 'diabetic-utils'

const readings: GlucoseReading[] = [
  { value: 120, unit: 'mg/dL', timestamp: '2024-01-01T08:00:00Z' },
  { value: 95, unit: 'mg/dL', timestamp: '2024-01-01T08:05:00Z' },
  { value: 180, unit: 'mg/dL', timestamp: '2024-01-01T08:10:00Z' },
  // ... more readings
]

const result = calculateEnhancedTIR(readings)

console.log(`TIR: ${result.inRange.percentage}%`)
console.log(`Very Low: ${result.veryLow.percentage}%`)
console.log(`Assessment: ${result.meetsTargets.overallAssessment}`)
console.log(result.meetsTargets.recommendations)

Use published formulas and typed inputs

Formula sources

Each formula is checked against a published clinical source:
  • A1C uses the ADAG Study consensus formula

  • TIR ranges follow the ADA/ATTD 2019 guidelines

  • Conversion factors match international standards

  • Test cases come from published medical examples

TypeScript contracts, package format, and integrations

TypeScript API

The API is typed throughout:
  • Typed glucose reading objects

  • Enum types for units (mg/dL, mmol/L)

  • Generic functions that accept either unit system

  • IntelliSense in the editor

Package format

It's pure TypeScript with zero dependencies, and it ships with:
  • Tree-shakeable exports to keep bundles small

  • JSDoc documentation throughout

  • Semantic versioning on npm

Use cases

It's written for:
  • CGM (continuous glucose monitor) companion apps

  • Diabetes management dashboards

  • Apple Watch and wearable health apps, like the GlucoseIQ watch app I'm building

  • HealthKit integration layers

  • Clinical research data processing

Where it stands

  • Featured in Google AI Overview for diabetes developer tools

  • 100% test coverage on the calculation formulas and their edge cases

  • A typed API with explicit mg/dL and mmol/L units

  • Open source, so other diabetes developers don't have to write these calculations again

  • Zero dependencies, pure TypeScript

The 1.5.x line stays on npm. New work happens in GlucoseIQ, which grew out of this library.

Project notes

100%

Vitest coverage for formulas, input validation, precision, and rounding

Google AI

Featured in Google AI Overview for diabetes developer tools