Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-generate score: [Score Agent] Add BMI #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/scores/bmi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Body Mass Index (BMI)

## Description

The Body Mass Index (BMI) is a calculation derived from weight and height. The BMI is an indicator of body fatness for most people.

## Formula

BMI = Weight (kg) / Height (m)\^2

## Instructions

- Input the weight in kilograms.
- Input the height in centimeters.

## Output

- The BMI score (kg/m²)
61 changes: 61 additions & 0 deletions src/scores/bmi/bmi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ScoreLibrary } from '../library'
import { bmi } from './bmi'
import { Score } from '../../classes'
import { ZodError } from 'zod'

const bmi_calculation = new Score(bmi)

describe('bmi', function () {
it('bmi calculation function should be available as a calculation', function () {
expect(ScoreLibrary).toHaveProperty('bmi')
})

describe('validation', function () {
describe('the score includes the correct input fields', function () {
it('should have all the expected input ids configured', function () {
const EXPECTED_INPUT_IDS = ['weight', 'height']
const configured_input_ids = Object.keys(
bmi_calculation.inputSchemaAsObject.shape,
)

expect(EXPECTED_INPUT_IDS).toEqual(configured_input_ids)
})
})

describe('when called with invalid data', function () {
it('should throw a ZodError', function () {
expect(() =>
bmi_calculation.calculate({
payload: {
weight: 'hello',
},
}),
).toThrow(ZodError)
})

it('should throw a ZodError when height is missing', function () {
expect(() =>
bmi_calculation.calculate({
payload: {
weight: 70,
},
}),
).toThrow(ZodError)
})
})
})

describe('score calculation', function () {
describe('when called with valid data', function () {
it('should return the correct BMI', function () {
const outcome = bmi_calculation.calculate({
payload: {
weight: 70,
height: 170,
},
})
expect(outcome.bmi).toEqual(24.22)
})
})
})
})
14 changes: 14 additions & 0 deletions src/scores/bmi/bmi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type ScoreType } from '../../types'
import { BMI_INPUTS, BMI_OUTPUT } from './definition'

Check failure on line 2 in src/scores/bmi/bmi.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find module './definition' or its corresponding type declarations.

export const bmi: ScoreType<typeof BMI_INPUTS, typeof BMI_OUTPUT> = {
name: 'Body Mass Index (BMI)',
readmeLocation: __dirname,
inputSchema: BMI_INPUTS,
outputSchema: BMI_OUTPUT,
calculate: ({ data }) => {
return {
bmi: data.weight / ((data.height / 100) ** 2),
}
}
}
15 changes: 15 additions & 0 deletions src/scores/bmi/definition/bmi_inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { z } from 'zod'
import type { ScoreInputSchemaType } from '../../../types'

export const BMI_INPUTS = {
weight: {
label: { en: 'Weight (kg)' },
type: z.number().nonnegative(),
unit: 'kg',

Check failure on line 8 in src/scores/bmi/definition/bmi_inputs.ts

View workflow job for this annotation

GitHub Actions / test

Type 'string' has no properties in common with type 'LabelType'.
},
height: {
label: { en: 'Height (cm)' },
type: z.number().positive(),
unit: 'cm',

Check failure on line 13 in src/scores/bmi/definition/bmi_inputs.ts

View workflow job for this annotation

GitHub Actions / test

Type 'string' has no properties in common with type 'LabelType'.
}
} satisfies ScoreInputSchemaType
10 changes: 10 additions & 0 deletions src/scores/bmi/definition/bmi_output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod'
import type { ScoreOutputSchemaType } from '../../../types'

export const BMI_OUTPUT = {
bmi: {
label: { en: 'Body Mass Index (BMI)' },
type: z.number(),
unit: 'kg/m²'

Check failure on line 8 in src/scores/bmi/definition/bmi_output.ts

View workflow job for this annotation

GitHub Actions / test

Type 'string' has no properties in common with type 'LabelType'.
}
} satisfies ScoreOutputSchemaType
1 change: 1 addition & 0 deletions src/scores/bmi/definition/bmi_subscales.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// No subscales for BMI
3 changes: 1 addition & 2 deletions src/scores/bmi/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { bmi as bmi_metric } from './metric/bmi'
export { bmi_us as bmi_imperial } from './us/bmi'
export * from './bmi'
Loading