@@ -90,18 +90,23 @@ const _clinicalStatus = moize((diab_presence:string|undefined,followup_assessmen
9090 * ------------------------------------------------------------------
9191 *
9292 * @precondition
93- * - date is never a missing value (is a default variable)
9493 * - the problem is 'active' (see clinicalStatus function)
9594 * - if there is a diabetes report on a follow-up 'diabetes_followup_adu_q_1', there should be a 'yes' value
9695 * in either t2d_followup_adu_q_1 or t2d_followup_adu_q_1 (diabetes type)
9796 *
9897 *
9998 * @pairingrule
100- * if diabetes_presence_adu_q_1 = yes in 1A => approximate year on which the participant given the age reported by diabetes_startage_adu_q_1
101- * (undefined if diabetes_startage_adu_q_1 has a missing value)
99+ * if diabetes_presence_adu_q_1 = yes in 1A =>
100+ * if start_age was reported, approximate year of the event given start_age reported (diabetes_startage_adu_q_1)
101+ * and the year of the year of the assessment.
102+ * else
103+ * undefined onset date
102104 * else
103- * if there is a 'yes' in any diabetes_followup_adu_q_1 => mean date between the date of the assessment
104- * where diabetes_followup_adu_q_1 = yes, and the date of the preceding one.
105+ * if there is a 'yes' in any diabetes_followup_adu_q_1 =>
106+ * If the date of the assessment where diabetes_followup_adu_q_1 = yes is available =>
107+ * mean date between that particular date (when diabetes_followup_adu_q_1 = yes), and the date of the preceding assessment.
108+ * Else
109+ * return undefined onset date
105110 * else
106111 * error/precondition violated ('diabetes' is not 'active' if the execution reached this point)
107112 *
@@ -135,19 +140,29 @@ export const onsetDateTime = ():string|undefined => {
135140 return lifelinesDateToISO ( lifelinesMeanDate ( date1 , date2 ) )
136141 }
137142 else {
138- throw Error ( "Unexpected input (precondition violated): no 'yes' values in neither t2d_followup_adu_q_1 nor t2d_followup_adu_q_1" )
143+ return undefined ;
139144 }
140145
141146
142147 }
143148
144149}
145150
146- /**
151+
152+ /*
153+ *
154+ * Helper function for 'onsetDateTime'
147155 *
148156 * @param diabFollowUp
149- * @returns
150- * @precondition there is always a date on the assessment prior to the one where diabetes_followup_adu_q_1 was 'yes'
157+ *
158+ * @return
159+ * If the date of the assessment where diabetes_followup_adu_q_1 = yes is available =>
160+ * mean date between that particular date (when diabetes_followup_adu_q_1 = yes), and the date of the preceding assessment.
161+ * Else
162+ * return undefined date
163+ *
164+ * @precondition : there is at least one 'yes'/1 on diabetes_followup_adu_q_1
165+ * @precondition there is always a date on one of the assessment prior to the one where diabetes_followup_adu_q_1 was 'yes'
151166 *
152167 */
153168function findDatesBetweenDiabetesPresenceReport ( ) : [ string , string ] | undefined {
@@ -160,18 +175,23 @@ function findDatesBetweenDiabetesPresenceReport(): [string,string]|undefined{
160175 assertIsDefined ( diabetesRepWave , `A 'yes' value on diabetes_followup_adu_q_1 was expected` )
161176
162177 const diabetesRepWaveDate = inputValue ( "date" , diabetesRepWave )
163- assertIsDefined ( diabetesRepWaveDate , `A non-null date is expected in the assessment where diabetes_followup_adu_q_1 is reported` )
178+ if ( diabetesRepWaveDate === undefined ) {
179+ return undefined
180+ }
181+ else {
182+ //find the previous non-undefined assessment date
183+ const assessmentDates :variableAssessments = inputValues ( 'date' )
184+ const waves = [ '1a' , '1b' , '1c' , '2a' , '3a' , '3b' ] ;
185+ const previousWaves = waves . slice ( 0 , waves . indexOf ( diabetesRepWave ) )
186+ const previousAssessmentWave = previousWaves . reverse ( ) . find ( ( pwave ) => assessmentDates [ pwave ] !== undefined )
187+
188+ assertIsDefined ( previousAssessmentWave , `Assessment (with a defined date) expected to exist previous to the one where diabetes_followup_adu_q_1 is reported` )
189+
190+ const previousAssessmentDate :string = assessmentDates [ previousAssessmentWave ] ! ;
191+ return [ previousAssessmentDate , diabetesRepWaveDate ]
192+ }
164193
165- //find the previous non-undefined assessment date
166- const assessmentDates :variableAssessments = inputValues ( 'date' )
167- const waves = [ '1a' , '1b' , '1c' , '2a' , '3a' , '3b' ] ;
168- const previousWaves = waves . slice ( 0 , waves . indexOf ( diabetesRepWave ) )
169- const previousAssessmentWave = previousWaves . reverse ( ) . find ( ( pwave ) => assessmentDates [ pwave ] !== undefined )
170-
171- assertIsDefined ( previousAssessmentWave , `Assessment (with a defined date) expected to exist previous to the one where diabetes_followup_adu_q_1 is reported` )
172194
173- const previousAssessmentDate :string = assessmentDates [ previousAssessmentWave ] ! ;
174- return [ previousAssessmentDate , diabetesRepWaveDate ]
175195
176196
177197 }
0 commit comments