Skip to content

Main content

Body Mass Index Source Code

Browse the implementation files for Body Mass Index.
@tool.io/tools/body-mass-index source ↗
tool.ts typescript
import {
	calculateBodyMassIndex,
	example as portableExample
} from '@tool.io/tools/body-mass-index';

export const bodyMassIndex = (
	weight: unknown,
	height: unknown
): string => {
	const weightKg =
		typeof weight === 'number'
			? weight
			: typeof weight === 'string' && weight.trim() !== ''
				? Number(weight)
				: Number.NaN;
	const heightM =
		typeof height === 'number'
			? height
			: typeof height === 'string' && height.trim() !== ''
				? Number(height)
				: Number.NaN;
	const result = calculateBodyMassIndex({ heightM, weightKg });

	return result.ok ? result.output.bmi.toPrecision(4) : '';
};

// The tool definition.
export const Tool: TypeTool = {
	name: 'Body Mass Index',
	slug: 'body-mass-index',
	intro: 'Calculate your body mass index (BMI).',
	tool: bodyMassIndex,
	i: [
		{
			example: String(portableExample.input.weightKg),
			id: 'weightKg',
			label: 'Weight',
			placeholder: 'Weight',
			quantity: {
				profileUnits: {
					metric: 'kg',
					uk: 'lb',
					us: 'lb'
				},
				units: [
					{
						label: 'kg',
						placeholder: '70',
						value: 'kg',
						valueType: {
							format: 'mass',
							type: 'number',
							unit: 'kg'
						}
					},
					{
						label: 'lb',
						placeholder: '154',
						value: 'lb',
						valueType: {
							format: 'mass',
							type: 'number',
							unit: 'lb'
						}
					}
				]
			},
			type: 'number',
			valueType: {
				format: 'mass',
				type: 'number',
				unit: 'kg'
			}
		},
		{
			example: String(portableExample.input.heightM),
			id: 'heightM',
			label: 'Height',
			placeholder: 'Height',
			quantity: {
				profileUnits: {
					metric: 'cm',
					uk: 'ft-in',
					us: 'ft-in'
				},
				units: [
					{
						label: 'cm',
						placeholder: '180',
						value: 'cm',
						valueType: {
							format: 'length',
							type: 'number',
							unit: 'cm'
						}
					},
					{
						label: 'ft',
						placeholder: `5'10"`,
						value: 'ft-in',
						valueType: {
							format: 'feet-and-inches',
							type: 'string'
						}
					},
					{
						label: 'm',
						placeholder: '1.8',
						value: 'm',
						valueType: {
							format: 'length',
							type: 'number',
							unit: 'm'
						}
					}
				]
			},
			type: 'number',
			valueType: {
				format: 'length',
				type: 'number',
				unit: 'm'
			}
		}
	],
	o: [
		{
			example: String(portableExample.output?.bmi ?? ''),
			id: 'bmi',
			label: 'BMI',
			placeholder: 'Body mass index',
			render: 'textarea',
			type: 'number',
			valueType: {
				format: 'body-mass-index',
				type: 'number'
			}
		}
	],
	equation:
		'$$ bodyMassIndex(\\text{weight}, \\text{height}) \\rightarrow  \\frac{weight}{height * height}$$',
	alternative: [
		{
			title: 'Waist to height calculator',
			url: '/waist-to-height'
		},
		{
			title: 'Waist to hip calculator',
			url: '/waist-to-hip'
		}
	],
	cite: [
		{
			title: 'Body mass index - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Body_mass_index'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Body Mass Index.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools