Health calculator: the JavaScript library

Health calculators: BMI, ideal weight, body fat, calories, heart rate zones, WHO child growth, pregnancy and others, plus a food log and a weight diary. Pure functions, no network.

  • @openlibsdev/health-calculator
  • v1.0.0
  • ES module
  • MIT

Installation

With a bundler or Node-style imports:

npm install @openlibsdev/health-calculator
import HealthCalculator from "@openlibsdev/health-calculator";

Or straight in a page, without a build step (jsDelivr resolves the dependencies):

<script type="module">
import HealthCalculator from "https://cdn.jsdelivr.net/npm/@openlibsdev/health-calculator@1.0.0/+esm";
</script>

Usage

import HealthCalculator from "@openlibsdev/health-calculator";

const profile = { sex: "female", age: 34, height: 168, weight: 61 };   // cm, kg
const r = HealthCalculator.compute("bmi", profile, {});
console.log(r.hero.value.n);            // 21.6
console.log(HealthCalculator.bmrMifflin(profile));

API

HealthCalculator.compute(id, profile, values)

Runs a calculator by its id for a profile and inputs; returns the result to show: main value, badge, scale, notes, tiles, table, chart.

HealthCalculator.CALCS

All calculators with their inputs.

HealthCalculator.GROUPS

Calculator groups.

HealthCalculator.inputDefaults(id)

Default inputs of a calculator.

HealthCalculator.normalizeProfile(p)

Checks a profile and fills in the missing fields.

HealthCalculator.bmiOf(p)

Body mass index.

HealthCalculator.bmrMifflin(p)

Basal metabolic rate (Mifflin–St Jeor).

HealthCalculator.lbmBoer(p)

Lean body mass (Boer).

HealthCalculator.maxHrTanaka(age)

Maximum heart rate (Tanaka).

HealthCalculator.whoLMS(sex, months)

WHO growth reference parameters for a sex and age in months.

HealthCalculator.whoZ(x, L, M, S)

Z-score by the LMS method.

HealthCalculator.FOODS

Food table with nutrients per 100 g.

HealthCalculator.mealTotals(items)

Nutrient totals of a meal.

HealthCalculator.addDiaryEntry(entries, entry)

Adds a weight diary entry.

HealthCalculator.diaryStats(entries, heightCm)

Statistics of the weight diary.

HealthCalculator.diaryCsv(entries)

Weight diary as CSV.

HealthCalculator.parseDiaryCsv(text)

Reads a weight diary from CSV.

HealthCalculator.APP

Name, version and release date of the library: {name, version, created}.

HealthCalculator.COLOR

Colors of the results: good, warning, bad and others.

HealthCalculator.MEALS

Meals of the food log: breakfast, lunch, dinner, snack.

HealthCalculator.MET_LIST

Activities with their MET value, for calorie burn.

HealthCalculator.UNITS

Unit conversion: inches ↔ cm, pounds ↔ kg.

HealthCalculator.WHO_BMI

WHO 2007 BMI-for-age reference (5–19 years): L, M, S for each month, by sex.

HealthCalculator.WORK_DEFAULTS

Default state of the whole work: calculator, units, profile, inputs, food log, custom foods, weight diary.

HealthCalculator.addDays(d, n)

A date plus a number of days.

HealthCalculator.daysBetween(a, b)

Number of days between two dates.

HealthCalculator.findFood(id, custom)

A food by id: a built-in one or one of the custom foods.

HealthCalculator.findNamedColor(text, lang)

Finds a named color by its name in the given language ("dark blue" → #00008b).

HealthCalculator.foodLogCsv(log, custom)

Food log as CSV: date, meal, food, grams, kcal, protein, fat, carbs.

HealthCalculator.healthCard(profile)

Main figures of a profile for the printable health card, or null when the profile is incomplete.

HealthCalculator.isoDate(d)

A local date as "YYYY-MM-DD" (today when none is given).

HealthCalculator.itemsTotals(items, custom)

Nutrient totals of a list of {food, grams}; unknown foods are skipped.

HealthCalculator.listNamedColors(lang)

Lists the named colors with their names in the given language.

HealthCalculator.lmsValue(L, M, S, z)

Value for a z-score from WHO L, M, S parameters.

HealthCalculator.makeCustomFood(o)

Makes a custom food from {name, k, p, f, c} per 100 g, or null when the data is wrong.

HealthCalculator.namedColorTexts(lang)

Labels of the color field in the given language (the texts of the color picker), English for a missing language.

HealthCalculator.normCDF(z)

Standard normal distribution: probability that a value is below z.

HealthCalculator.normalizeWork(w)

Checks a saved work (project or autosave) and fills in what is missing.

HealthCalculator.parseDate(s)

"YYYY-MM-DD" → local date, or null.

HealthCalculator.round(n, d)

Rounds a number to the given number of decimals (1 by default).

HealthCalculator.sortDiary(entries)

Weight diary entries that are valid, sorted by date.

HealthCalculator.textFontData

Data for a text and font block: the font list, loading a font style, adding a font file, the block's labels in a language and the default values.

Dependencies

What the library loads, with the licenses (all are listed on the Licenses page).

NameVersionLicenseFiles
@openlibsdev/font-forge1.0.0MITGitHub
@openlibsdev/image-forge1.0.0MITGitHub
@imagemagick/magick-wasm0.0.35Apache-2.0 License · Source code
@imagemagick/magick-wasm0.0.35Apache-2.0 License · Source code
@simonwep/pickr1.10.2MIT License · Source code
@simonwep/pickr1.10.2MIT License · Source code
chart.js4.4.1MIT License · Source code
chart.js4.4.1MIT License · Source code
dejavu-fonts-ttf2.37.3Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain License · Source code
imagetracerjs1.2.6Unlicense License · Source code