Skip to content

Commit

Permalink
(chore) Better typings for common-expression-helpers (#111)
Browse files Browse the repository at this point in the history
* (fix) Fix some tests with implicit environment assumptions

Mostly this relates to local time being only slightly > UTC

* (chore) Add typings for commmon expression helpers
  • Loading branch information
ibacher authored Aug 22, 2023
1 parent f8f14e6 commit 8488801
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 64 deletions.
11 changes: 6 additions & 5 deletions src/hooks/useInitialValues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,11 @@ jest.mock('../utils/expression-runner', () => {
};
});

describe('useInialValues', () => {
describe('useInitialValues', () => {
const encounterDate = new Date();

afterEach(() => {
allFormFields = allFormFields.slice(0, 6);
allFormFields.forEach(field => {
allFormFields.slice(0, 6).forEach(field => {
delete field.value;
});
});
Expand Down Expand Up @@ -182,10 +181,10 @@ describe('useInialValues', () => {
notes: 'Mother is in perfect condition',
screening_methods: [],
// child one
date_of_birth: '7/24/2023',
date_of_birth: new Date('2023-07-24T00:00:00.000+0000').toLocaleDateString('en-US'),
infant_name: 'TBD',
// child two
date_of_birth_1: '7/24/2023',
date_of_birth_1: new Date('2023-07-24T00:00:00.000+0000').toLocaleDateString('en-US'),
infant_name_1: ' TDB II',
});
expect(allFormFields.find(field => field.id === 'date_of_birth_1')).not.toBeNull();
Expand Down Expand Up @@ -231,7 +230,9 @@ describe('useInialValues', () => {
notes: '',
screening_methods: [],
date_of_birth: '',
date_of_birth_1: '',
infant_name: '',
infant_name_1: '',
latest_mother_hiv_status: '664AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/ohri-form.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ describe('OHRI Forms:', () => {
fireEvent.change(lmpField, { target: { value: '2022-07-06' } });

// verify
await act(async () => expect(lmpField.value).toBe('7/6/2022'));
await act(async () => expect(eddField.value).toBe('4/12/2023'));
await act(async () => expect(lmpField.value).toBe(new Date('2022-07-06').toLocaleDateString('en-US')));
await act(async () => expect(eddField.value).toBe(new Date('2023-04-12').toLocaleDateString('en-US')));
});

it('Should evaluate months on ART', async () => {
Expand All @@ -244,7 +244,7 @@ describe('OHRI Forms:', () => {
fireEvent.blur(artStartDateField, { target: { value: '05/02/2022' } });

// verify
await act(async () => expect(artStartDateField.value).toBe('5/2/2022'));
await act(async () => expect(artStartDateField.value).toBe(new Date('2022-05-02T00:00:00.000+0000').toLocaleDateString('en-US')));
await act(async () => expect(assumeTodayToBe).toBe('7/11/2022'));
await act(async () => expect(monthsOnARTField.value).toBe('5'));
});
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('OHRI Forms:', () => {
await act(async () => expect(mrn.value).toBe(''));

// verify
await act(async () => expect(enrollmentDate.value).toBe('7/6/1975'));
await act(async () => expect(enrollmentDate.value).toBe(new Date('1975-07-06T00:00:00.000Z').toLocaleDateString('en-US')));
await act(async () => expect(mrn.value).toBe(''));
await act(async () => expect(mrn).toBeVisible());
});
Expand Down
121 changes: 66 additions & 55 deletions src/utils/common-expression-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use ';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration);
Expand Down Expand Up @@ -35,7 +34,7 @@ export class CommonExpressionHelpers {
return new Date();
}

includes = (collection: any[], value: any) => {
includes = <T = any>(collection: T[], value: T) => {
return collection?.includes(value);
};

Expand Down Expand Up @@ -72,7 +71,7 @@ export class CommonExpressionHelpers {
return selectedDate.getTime() > calculatedDate.getTime();
};

addWeeksToDate = (date, weeks) => {
addWeeksToDate = (date: Date, weeks: number) => {
date.setDate(date.getDate() + 7 * weeks);

return date;
Expand All @@ -91,58 +90,58 @@ export class CommonExpressionHelpers {
return null;
};

calcBMI = (height, weight) => {
let r;
calcBMI = (height: number, weight: number) => {
let r: string;
if (height && weight) {
r = (weight / (((height / 100) * height) / 100)).toFixed(1);
}
return height && weight ? parseFloat(r) : null;
return r ? parseFloat(r) : null;
};

/**
* Expected date of delivery
* @param lmpQuestionId
* @returns
*/
calcEDD = lmp => {
calcEDD = (lmp: Date) => {
let resultEdd = {};
if (lmp) {
resultEdd = new Date(lmp.getTime() + 280 * 24 * 60 * 60 * 1000);
}
return lmp ? resultEdd : null;
};

calcMonthsOnART = artStartDate => {
calcMonthsOnART = (artStartDate: Date) => {
let today = new Date();
let resultMonthsOnART;
let resultMonthsOnART: number;
let artInDays = Math.round((today.getTime() - artStartDate.getTime?.()) / 86400000);
if (artStartDate && artInDays >= 30) {
resultMonthsOnART = Math.floor(artInDays / 30);
}
return artStartDate ? resultMonthsOnART : null;
};

calcViralLoadStatus = viralLoadCount => {
let resultViralLoadStatus;
calcViralLoadStatus = (viralLoadCount: number) => {
let resultViralLoadStatus: string;
if (viralLoadCount) {
if (viralLoadCount > 50) {
resultViralLoadStatus = 'a6768be6-c08e-464d-8f53-5f4229508e54';
} else {
resultViralLoadStatus = '5d5e42cc-acc4-4069-b3a8-7163e0db5d96';
}
}
return viralLoadCount ? resultViralLoadStatus : null;
return resultViralLoadStatus ?? null;
};

calcNextVisitDate = (followupDate, arvDispensedInDays) => {
let resultNextVisitDate = {};
let resultNextVisitDate: Date;
if (followupDate && arvDispensedInDays) {
resultNextVisitDate = new Date(followupDate.getTime() + arvDispensedInDays * 24 * 60 * 60 * 1000);
}
return followupDate && arvDispensedInDays ? resultNextVisitDate : null;
return resultNextVisitDate ?? null;
};

calcTreatmentEndDate = (followupDate, arvDispensedInDays, patientStatus) => {
calcTreatmentEndDate = (followupDate: Date, arvDispensedInDays: number, patientStatus: string) => {
let resultTreatmentEndDate = {};
let extraDaysAdded = 30 + arvDispensedInDays;
if (followupDate && arvDispensedInDays && patientStatus == '160429AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') {
Expand All @@ -153,7 +152,7 @@ export class CommonExpressionHelpers {
: null;
};

calcAgeBasedOnDate = dateValue => {
calcAgeBasedOnDate = (dateValue?: ConstructorParameters<typeof Date>[0] | null) => {
let targetYear = null;
if (dateValue) {
targetYear = new Date(dateValue).getFullYear();
Expand All @@ -166,57 +165,69 @@ export class CommonExpressionHelpers {
};

//Ampath Helper Functions
calcBSA = (height, weight) => {
let result;
calcBSA = (height: number, weight: number) => {
let result: string;
if (height && weight) {
result = Math.sqrt((height * weight) / 3600).toFixed(2);
}
return height && weight ? parseFloat(result) : null;
return result ? parseFloat(result) : null;
};

arrayContains = (array, members) => {
if (Array.isArray(members)) {
if (members.length === 0) {
return true;
}
arrayContains = <T = any>(array: T[], members: T[] | T) => {
if (!array || !Array.isArray(array)) {
return false;
}

let contains = true;
if (array.length === 0) {
return members === undefined || members === null || (Array.isArray(members) && members.length === 0);
}

for (let i = 0; i < members.length; i++) {
const val = members[i];
if (array.indexOf(val) === -1) {
contains = false;
}
}
if (!Array.isArray(members)) {
members = [members];
}

return contains;
} else {
return array.indexOf(members) !== -1;
if (members.length === 0) {
return true;
}
};

arrayContainsAny = (array, members) => {
if (Array.isArray(members)) {
if (members.length === 0) {
return true;
for (let val of members) {
if (array.indexOf(val) === -1) {
return false;
}
let contains = false;
}

return true;
};

for (let i = 0; i < members.length; i++) {
const val = members[i];
if (array.indexOf(val) !== -1) {
contains = true;
}
arrayContainsAny = <T = any>(array: T[], members: T[]) => {
if (!array || !Array.isArray(array)) {
return false;
}

if (array.length === 0) {
return members === undefined || members === null || (Array.isArray(members) && members.length === 0);
}

if (!Array.isArray(members)) {
members = [members];
}

if (members.length === 0) {
return true;
}

for (let val of members) {
if (array.indexOf(val) !== -1) {
return true;
}
return contains;
} else {
return array.indexOf(members) !== -1;
}

return false;
};

formatDate = (value, format, offset) => {
format = format || 'yyyy-MM-dd';
offset = offset || '+0300';
formatDate = (value: ConstructorParameters<typeof Date>[0], format?: string | null, offset?: string | null) => {
format = format ?? 'yyyy-MM-dd';
offset = offset ?? '+0300';

if (!(value instanceof Date)) {
value = new Date(value);
Expand All @@ -228,7 +239,7 @@ export class CommonExpressionHelpers {
return value;
};

extractRepeatingGroupValues = (key, array) => {
extractRepeatingGroupValues = (key: string | number | symbol, array: Record<string | number | symbol, unknown>[]) => {
const values = array.map(function(item) {
return item[key];
});
Expand All @@ -249,8 +260,8 @@ export class CommonExpressionHelpers {
return gravida;
}

calcTimeDifference = (obsDate, timeFrame) => {
let daySinceLastObs;
calcTimeDifference = (obsDate: Date | dayjs.Dayjs, timeFrame: 'd' | 'w' | 'm' | 'y') => {
let daySinceLastObs: number | string = '';
const endDate = dayjs();
if (obsDate) {
if (timeFrame == 'd') {
Expand All @@ -266,7 +277,7 @@ export class CommonExpressionHelpers {
daySinceLastObs = Math.abs(Math.round(endDate.diff(obsDate, 'year', true)));
}
}
return daySinceLastObs == '' ? '0' : daySinceLastObs;
return daySinceLastObs === '' ? '0' : daySinceLastObs;
};
}

Expand Down

0 comments on commit 8488801

Please sign in to comment.