Skip to content

Commit

Permalink
feat(calcs): gad
Browse files Browse the repository at this point in the history
  • Loading branch information
nckhell committed Jan 2, 2025
1 parent de9e432 commit 33bee7e
Show file tree
Hide file tree
Showing 23 changed files with 346 additions and 497 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { InputType } from '../../../src/types/calculations.types'
import { z } from 'zod'
import { ScoreInputSchemaType } from '../../../types'

export const GAD2_INPUTS: Array<InputType> = [
{
input_id: 'GAD2_Q01',
export const GAD2_INPUTS = {
GAD2_Q01: {
label: {
en: 'Over the last two weeks, how often have you been bothered by feeling nervous, anxious, or on edge?',
nl: 'Hoe vaak hebt u in de afgelopen 2 weken last gehad van uzelf zenuwachtig, angstig of gespannen te voelen?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -32,15 +32,14 @@ export const GAD2_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD2_Q02',
GAD2_Q02: {
label: {
en: 'Over the last two weeks, how often have you been bothered by not being able to stop or control worrying?',
nl: 'Hoe vaak hebt u in de afgelopen 2 weken last gehad van niet in staat te zijn om te stoppen met piekeren of om controle te krijgen over het piekeren?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -63,4 +62,4 @@ export const GAD2_INPUTS: Array<InputType> = [
],
},
},
]
} satisfies ScoreInputSchemaType
13 changes: 13 additions & 0 deletions src/scores/GAD_2/definition/gad_2_output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from 'zod'
import { ScoreOutputSchemaType } from '../../../types'

export const GAD2_OUTPUT = {
GAD2_SCORE: {
label: { en: 'GAD-2 Score' },
type: z.number(),
},
GAD2_INTERPRETATION: {
label: { en: 'GAD-2 Interpretation' },
type: z.string(),
},
} satisfies ScoreOutputSchemaType
File renamed without changes.
114 changes: 114 additions & 0 deletions src/scores/GAD_2/gad_2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ZodError } from 'zod'
import { Score } from '../../classes'
import { ScoreLibrary } from '../library'
import {
best_response,
random_response,
worst_response,
} from './__testdata__/gad_2_test_responses'
import { gad_2 } from './gad_2'

const BEST_SCORE = 0
const WORST_SCORE = 6

const gad_2_calculation = new Score(gad_2)

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

describe('basic assumptions', function () {
const outcome = gad_2_calculation.calculate({ payload: best_response })

it('should return 2 calculation results', function () {
expect(Object.keys(outcome).length).toEqual(2)
})

it('should have the expected calculation result ids', function () {
const EXPECTED_CALCULATION_ID = ['GAD2_SCORE', 'GAD2_INTERPRETATION']

const configured_calculation_id = Object.keys(outcome)

expect(configured_calculation_id).toEqual(EXPECTED_CALCULATION_ID)
})
})

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 = ['GAD2_Q01', 'GAD2_Q02']

const configured_input_ids = Object.keys(
gad_2_calculation.inputSchemaAsObject.shape,
)

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

describe('when an answer is not not one of the allowed answers', function () {
it('should throw an InvalidInputsError', function () {
expect(() =>
gad_2_calculation.calculate({
payload: {
GAD2_Q01: -1,
},
}),
).toThrow(ZodError)
})
})

describe('when called with an empty response', function () {
it('should throw a ZodError', function () {
expect(() => gad_2_calculation.calculate({ payload: {} })).toThrow(
ZodError,
)
})
})
})

describe('score calculation', function () {
describe('when called with the best response', function () {
const outcome = gad_2_calculation.calculate({ payload: best_response })

it('should return the best score', function () {
expect(outcome.GAD2_SCORE).toEqual(BEST_SCORE)
})

it('should return the "No anxiety" interpretation', function () {
expect(outcome.GAD2_INTERPRETATION).toEqual('No anxiety')
})
})

describe('when called with the worst response', function () {
const outcome = gad_2_calculation.calculate({
payload: worst_response,
})

it('should return the worst score', function () {
expect(outcome.GAD2_SCORE).toEqual(WORST_SCORE)
})

it('should return the "severe anxiety" interpretation', function () {
expect(outcome.GAD2_INTERPRETATION).toEqual('Severe anxiety')
})
})

describe('when called with a random response', function () {
const outcome = gad_2_calculation.calculate({
payload: random_response,
})

it('should return the expected score', function () {
const EXPECTED_SCORE = 3

expect(outcome.GAD2_SCORE).toEqual(EXPECTED_SCORE)
})

it('should return the "Mild anxiety" interpretation', function () {
expect(outcome.GAD2_INTERPRETATION).toEqual('Moderate anxiety')
})
})
})
})
22 changes: 22 additions & 0 deletions src/scores/GAD_2/gad_2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
GAD2_INPUTS,
GAD2_OUTPUT,
GAD2_INTERPRETATION_TABLE,
} from './definition'
import { ScoreType } from '../../types'
import _ from 'lodash'

export const gad_2: ScoreType<typeof GAD2_INPUTS, typeof GAD2_OUTPUT> = {
name: 'Generalised Anxiety Disorder Assessment (GAD-2)',
readmeLocation: __dirname,
inputSchema: GAD2_INPUTS,
outputSchema: GAD2_OUTPUT,
calculate: ({ data }) => {
const totalScore = data.GAD2_Q01 + data.GAD2_Q02

return {
GAD2_SCORE: totalScore,
GAD2_INTERPRETATION: GAD2_INTERPRETATION_TABLE[totalScore.toString()],
}
},
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { InputType } from '../../../src/types/calculations.types'
import { z } from 'zod'
import { ScoreInputSchemaType } from '../../../types'

export const GAD7_INPUTS: Array<InputType> = [
{
input_id: 'GAD7_Q01',
export const GAD7_INPUTS = {
GAD7_Q01: {
label: {
en: 'Over the last two weeks, how often have you been bothered by feeling nervous, anxious, or on edge?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -31,14 +31,13 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD7_Q02',
GAD7_Q02: {
label: {
en: 'Over the last two weeks, how often have you been bothered by not being able to stop or control worrying?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -61,14 +60,13 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD7_Q03',
GAD7_Q03: {
label: {
en: 'Over the last two weeks, how often have you been bothered by worrying too much about different things?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -91,14 +89,13 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD7_Q04',
GAD7_Q04: {
label: {
en: 'Over the last two weeks, how often have you been bothered by trouble relaxing?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -121,14 +118,13 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD7_Q05',
GAD7_Q05: {
label: {
en: 'Over the last two weeks, how often have you been bothered by being so restless that it is hard to sit still?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -151,14 +147,13 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD7_Q06',
GAD7_Q06: {
label: {
en: 'Over the last two weeks, how often have you been bothered by becoming easily annoyed or irritable?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -181,14 +176,13 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
{
input_id: 'GAD7_Q07',
GAD7_Q07: {
label: {
en: 'Over the last two weeks, how often have you been bothered by feeling afraid, as if something awful might happen?',
},
type: {
type: 'number',
allowed_answers: [
type: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]),
uiOptions: {
options: [
{
label: { en: 'Not at all', nl: 'Helemaal niet' },
value: 0,
Expand All @@ -211,4 +205,4 @@ export const GAD7_INPUTS: Array<InputType> = [
],
},
},
]
} satisfies ScoreInputSchemaType
13 changes: 13 additions & 0 deletions src/scores/GAD_7/definition/gad_7_output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from 'zod'
import { ScoreOutputSchemaType } from '../../../types'

export const GAD7_OUTPUT = {
GAD7_SCORE: {
label: { en: 'GAD-7 Score' },
type: z.number(),
},
GAD7_INTERPRETATION: {
label: { en: 'GAD-7 Interpretation' },
type: z.string(),
},
} satisfies ScoreOutputSchemaType
File renamed without changes.
Loading

0 comments on commit 33bee7e

Please sign in to comment.