Glucose Values & Formatting
Validation, guards, alignment, labels, and formatting.
calculateHOMAIR
Import: @glucoseiq/core
calculateHOMAIR(glucose: number, insulin: number): { interpretation: string; value: number }Calculates HOMA-IR (Homeostatic Model Assessment for Insulin Resistance) from fasting glucose and insulin.
Formula: HOMA-IR = (fasting glucose [mg/dL] × fasting insulin [µIU/mL]) / 405
Returns the calculated value with the function's cutoff label. The result does not diagnose insulin resistance.
Returns: Object with numeric HOMA-IR value and interpretation label.
Throws: If glucose or insulin are invalid (non-finite, zero, or negative).
See:
| Parameter | Description |
|---|---|
glucose | Fasting glucose value in mg/dL. Must be a positive finite number. |
insulin | Fasting insulin value in µIU/mL. Must be a positive finite number. |
checkGlycemicAlignment
Import: @glucoseiq/core
checkGlycemicAlignment(a1c: number, glucose: number, insulin: number): { disclaimer: string; estimatedAverageGlucose: number; flags: string[]; homaIR: { interpretation: string; value: number }; recommendation: string }Compares A1C-derived estimated average glucose with fasting glucose and calculates HOMA-IR from fasting glucose and insulin. Returns both calculated values, flags from fixed comparison rules, and an informational disclaimer. The result does not diagnose a condition.
Returns: Object with estimated average glucose (mg/dL), HOMA-IR result object, flags array, recommendation string, and disclaimer.
Throws: If any input value is invalid (non-finite, zero, or negative).
See: CDC: eAG formula
| Parameter | Description |
|---|---|
a1c | A1C value (percentage). Must be a positive finite number. |
glucose | Fasting glucose value in mg/dL. Must be a positive finite number. |
insulin | Fasting insulin value in µIU/mL. Must be a positive finite number. |
formatDate
Import: @glucoseiq/core
formatDate(iso: string, timeZone?: string): stringFormats a UTC ISO 8601 timestamp in the requested IANA time zone.
Returns: Localized date/time string (e.g., 'Mar 20, 2024, 06:00 AM')
Throws: If the ISO string is invalid or cannot be parsed
Throws: If the time zone is invalid
| Parameter | Description |
|---|---|
iso | ISO 8601 timestamp string (e.g., '2024-03-20T10:00:00Z') |
timeZone | Optional IANA time zone (e.g., 'America/New_York') |
formatGlucose
Import: @glucoseiq/core
formatGlucose(val: number, unit: GlucoseUnit, options: { digits?: number; suffix?: boolean } = {}): stringFormats a numeric glucose value with optional rounding and a unit suffix.
Returns: Formatted glucose string (e.g., '5.5 mmol/L', '120 mg/dL')
See: https://wwwn.cdc.gov/Nchs/Data/Nhanes/Public/2021/DataFiles/BIOPRO_L.htm
| Parameter | Description |
|---|---|
val | Glucose value (number) |
unit | Glucose unit ('mg/dL' or 'mmol/L') |
options | Formatting options: { digits?: number; suffix?: boolean } (default: { digits: 0, suffix: true }) |
formatPercentage
Import: @glucoseiq/core
formatPercentage(val: number, digits: number = 1): stringFormats a number with fixed decimal places and a percent sign.
Returns: Formatted percentage string (e.g., '85.0%')
| Parameter | Description |
|---|---|
val | Value to format (fraction or percent) |
digits | Number of decimal places (default: 1) |
getGlucoseLabel
Import: @glucoseiq/core
getGlucoseLabel(val: number, unit: GlucoseUnit = MG_DL, thresholds?: { hyper?: { mgdl?: number; mmoll?: number }; hypo?: { mgdl?: number; mmoll?: number } }): 'normal' | 'low' | 'high'Returns 'low', 'normal', or 'high' from the configured thresholds for the value's unit.
Returns: 'low', 'normal', or 'high' based on configured thresholds
See: https://www.diabetes.co.uk/diabetes_care/blood-sugar-level-ranges.html
| Parameter | Description |
|---|---|
val | Glucose value (number) |
unit | Glucose unit ('mg/dL' or 'mmol/L'), default: 'mg/dL' |
thresholds | Optional custom thresholds for hypo/hyper ({ hypo?: { mgdl?: number; mmoll?: number }, hyper?: { mgdl?: number; mmoll?: number } }) |
isEstimateGMIOptions
Import: @glucoseiq/core
isEstimateGMIOptions(input: unknown): input is EstimateGMIOptionsType guard for EstimateGMIOptions.
Requires a positive finite value and one of the two supported glucose units.
Returns: True if input is a valid EstimateGMIOptions object.
| Parameter | Description |
|---|---|
input | Candidate value to validate. |
isHyper
Import: @glucoseiq/core
isHyper(val: number, unit: GlucoseUnit = MG_DL, thresholds?: { mgdl?: number; mmoll?: number }): booleanReturns whether a glucose value is above the configured hyperglycemia threshold for its unit.
Returns: True if value is above the hyperglycemia threshold
See: https://www.diabetes.co.uk/diabetes_care/blood-sugar-level-ranges.html
| Parameter | Description |
|---|---|
val | Glucose value (number) |
unit | Glucose unit ('mg/dL' or 'mmol/L'), default: 'mg/dL' |
thresholds | Optional custom thresholds ({ mgdl?: number; mmoll?: number }) |
isHypo
Import: @glucoseiq/core
isHypo(val: number, unit: GlucoseUnit = MG_DL, thresholds?: { mgdl?: number; mmoll?: number }): booleanReturns whether a glucose value is below the configured hypoglycemia threshold for its unit.
Returns: True if value is below the hypoglycemia threshold
See: https://www.diabetes.co.uk/diabetes_care/blood-sugar-level-ranges.html
| Parameter | Description |
|---|---|
val | Glucose value (number) |
unit | Glucose unit ('mg/dL' or 'mmol/L'), default: 'mg/dL' |
thresholds | Optional custom thresholds ({ mgdl?: number; mmoll?: number }) |
isValidGlucoseString
Import: @glucoseiq/core
isValidGlucoseString(input: unknown): input is stringReturns whether a string contains a positive finite number followed by one of the two supported glucose units. It checks only the declared string format and the positive, finite numeric bound.
Returns: True when input matches the supported glucose string format
See: https://wwwn.cdc.gov/Nchs/Data/Nhanes/Public/2021/DataFiles/BIOPRO_L.htm
| Parameter | Description |
|---|---|
input | Value to check |
isValidGlucoseValue
Import: @glucoseiq/core
isValidGlucoseValue(value: unknown, unit: unknown): booleanValidates a glucose value and unit. Ensures value is a positive finite number and unit is supported.
Returns: True if value and unit are valid
| Parameter | Description |
|---|---|
value | Glucose value to validate |
unit | Glucose unit to validate |
isValidInsulin
Import: @glucoseiq/core
isValidInsulin(value: unknown): value is numberReturns whether a value is finite and greater than 0 but less than 1000 µIU/mL. This is an input bound, not a reference interval.
Returns: True when the value satisfies the input bound
| Parameter | Description |
|---|---|
value | Candidate insulin value |
parseGlucoseString
Import: @glucoseiq/core
parseGlucoseString(input: string): { unit: GlucoseUnit; value: number }Parses a glucose string (e.g., "100 mg/dL", "5.5 mmol/L") into value and unit. Accepts only a positive finite value and a supported glucose unit.
Returns: Object with numeric value and validated unit
Throws: If input string is invalid or not in expected format
import { parseGlucoseString } from '@glucoseiq/core'
const input: string = '100 mg/dL'
const reading = parseGlucoseString(input) // { value: 100, unit: 'mg/dL' }See: https://wwwn.cdc.gov/Nchs/Data/Nhanes/Public/2021/DataFiles/BIOPRO_L.htm
| Parameter | Description |
|---|---|
input | String in the format "value unit" (e.g., "100 mg/dL") |