bmi-logic
v0.0.2
Published
Body mass index (BMI) calculation
Maintainers
Readme
bmi-logic
A tiny, dependency-free TypeScript library that computes the Body Mass Index from a weight and a height.
It exposes a single function — nothing more — so it stays small, predictable, and easy to embed in any environment (Node, browsers, edge runtimes).
Install
yarn add bmi-logic
# or
npm install bmi-logicUsage
import { calculateBmi } from 'bmi-logic'
calculateBmi(65, 1.7) // "22.49"
calculateBmi(75, 1.8) // "23.15"
calculateBmi(0, 1.7) // undefined
calculateBmi(70, -1.7) // undefinedAPI
calculateBmi(weight, height)
| Parameter | Type | Description |
| --------- | -------- | ------------------- |
| weight | number | Weight in kilograms |
| height | number | Height in meters |
Returns: string | undefined
- A two-decimal string (e.g.
"22.49") when both inputs are positive numbers. undefinedwhenweightorheightis<= 0.
The string format is intentional: it preserves the trailing zero (e.g.
"22.00") so the value can be rendered as-is in a UI. CallNumber(...)if you need a numeric value.
And about the categorization?
It only produces the raw BMI value — it does not classify it.
To turn that value into a human-readable category (e.g. "Normal weight", "Overweight") or to compute an ideal weight range, pair it with bmi-utils package:
import { calculateBmi } from 'bmi-logic'
import { loadCategory, MALE } from 'bmi-utils'
const bmi = calculateBmi(75, 1.8)
const category = bmi !== undefined
? loadCategory(MALE, Number(bmi), 'en-US')
: ''
console.log({ bmi, category })
// { bmi: '23.15', category: 'Normal weight' }Author
Emanuel Gonçalves
- GitHub @emanuelgsouza
- LinkedIn @emanuelgsouza
