I can’t figure out how to properly make an alert for myself that a RegEx query is false because the text is not in the content. See the first alert condition. I assume I have to repeat this for every RegEx search and then at the end, if at least one is missing, tell Drafts, not to insert text. Am I on the right track?
//Date of Assessment
let matchDateAssessment = draft.content.match(/Date\sof\sAssessment:\s(\w+\s\d{1,2}\,\s\d{4})/);
if (matchDateAssessment) {
var dateAssessment = draft.content.match(/Date\sof\sAssessment:\s(\w+\s\d{1,2}\,\s\d{4})/)[1]
} else {
alert('Missing Date of Assessment'); context.fail();
}
//Diagnosis
let matchDiagnosis = draft.content.match(/(F\d{1,2}\.\d{1,2}\s.+)\./);
if (matchDiagnosis) {
var diagnosis = draft.content.match(/(F\d{1,2}\.\d{1,2}\s.+)\./)[1];
} else {
alert('Missing Diagnosis'); context.fail();
}
//First Session
let matchFirstSession = draft.content.match(/\d\/\d{1,2}\s\-\s(\w+\s\d+\,\s\d+)/);
if (matchFirstSession) {
var firstSession = draft.content.match(/\d\/\d{1,2}\s\-\s(\w+\s\d+\,\s\d+)/)[1];
} else {
alert('Missing Date of First Session'); context.fail();
}
//Number of Sessions
let matchSessions = draft.content.match(/TP\d{1,2}:\s(\d+)/);
if (matchSessions) {
var sessions = draft.content.match(/TP\d{1,2}:\s(\d+)/)[1];
} else {
alert('Missing Number of Session'); context.fail();
}
//Treatment Plan Number
let matchTreatmentPlan = draft.content.match(/TP(\d)/);
if (matchTreatmentPlan) {
var treatmentPlan = draft.content.match(/TP(\d)/)[1];
//Funciton - Convert numebrs to words
function numeral(number){
var numbers = ['zero','first','second','third','fourth','fifth','sixth','seventh','eighth'];
return numbers[number]
}
treatmentPlan = numeral(treatmentPlan);
} else {
alert('Missing Treatment Plan Number'); context.fail();
}
//Insert Text if all RegEx queries were found
if ((dateAssessment) && (diagnosis) && (firstSession) && (sessions) && (treatmentPlan)) {
editor.setSelectedText(`Mr. XX's initial psychological assessment was completed on ${dateAssessment}. His initial presentation was consistent with the DSM-5 and ICD-10 diagnoses of ${diagnosis}. Mr. XX initiated his *${treatmentPlan} psychotherapy treatment plan* on ${firstSession}, and has completed all ${sessions} of his cognitive-behavioural therapy sessions.`);
} else {
context.fail();
}