Skip to content

Commit

Permalink
Merge pull request #1 from IvyZ23/master
Browse files Browse the repository at this point in the history
merge: complete TypeScript implementation (Ivy Zhu)
  • Loading branch information
MichaelPascale authored Aug 27, 2024
2 parents 889fb1c + dd0a851 commit bf0e8a7
Show file tree
Hide file tree
Showing 15 changed files with 2,301 additions and 227 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Multisubstance Online Timeline Follow-Back

The [timeline follow-back](https://en.wikipedia.org/wiki/Timeline_Followback_Method_Assessment_(Alcohol)) is a method for retrospectively ascertaining recent alcohol and other drug use patterns developed by [Linda C. and Mark B. Sobell](#Credits). This web-based timeline form has beed developed for the [MGH Center for Addiction Medicine](http://www.mghaddictionmedicine.com/) and is designed for use with [REDCap](https://www.project-redcap.org/) databases in clinical research.
The [timeline follow-back](https://en.wikipedia.org/wiki/Timeline_Followback_Method_Assessment_(Alcohol)) is a method for retrospectively ascertaining recent alcohol and other drug use patterns developed by [Linda C. and Mark B. Sobell](#Credits). This web-based timeline form was developed for the [MGH Center for Addiction Medicine](http://www.mghaddictionmedicine.com/) and is designed for use with [REDCap](https://www.project-redcap.org/) databases in clinical research.

A major revision in Typescript was completed in 2024 by Ivy Zhu. The original application was developed by Michael Pascale.

![TLFB Application Screenshot](https://github.com/user-attachments/assets/67d3ff2f-f43f-485c-ac8a-c0b3d0f13d41)


## Prerequisites

Expand Down Expand Up @@ -73,4 +78,4 @@ Sobell, L. C., & Sobell, M. B. (1992). _Timeline Follow-Back_. In R. Z. Litten &


---
Source code is copyright (c) 2023, Michael Pascale and distributed under the MIT License.
Source code is copyright (c) 2024, Ivy Zhu and Michael Pascale and distributed under the MIT License.
455 changes: 405 additions & 50 deletions index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tlfb",
"version": "1.0.0",
"version": "3.0.0",
"description": "Electronic Timeline Follow-Back Form",
"private": true,
"directories": {
Expand Down
90 changes: 90 additions & 0 deletions src/calculate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* calculate.js
* Calculate various metrics on the timeline follow-back data.
*/

import { UseEvent, CalendarEvent } from "./state";
import { CVT_MS_DAY } from "./constants";


function calc_use_days(eventlist: Array<CalendarEvent>, category: string, include_unknown: boolean): Array<string> {

let use_days: Set<string> = new Set();
let use_days_amount: Set<string> = new Set();

for (let event of eventlist) {
if((event as UseEvent).properties.category === category){
use_days.add(event.date.substring(0,10));

if ((event as UseEvent).properties.amount != "unknown") {
use_days_amount.add(event.date.substring(0,10));
}
}
}

if (include_unknown) {
return Array.from(use_days);
} else {
return Array.from(use_days_amount);
}

}

export function calc_days_used(eventlist: Array<CalendarEvent>, category: string) {

let use_days = calc_use_days(eventlist, category, true);

return use_days.length;
}

// Used to calculate number of days where amount used is known.
export function calc_days_used_amount(eventlist: Array<CalendarEvent>, category: string) {

let use_days = calc_use_days(eventlist, category, false);

return use_days.length;
}

export function calc_total_occasions(eventlist: Array<CalendarEvent>, category: string) {

let total = 0;

for (let event of eventlist) {
if((event as UseEvent).properties.category === category){
total+=Number((event as UseEvent).properties.times)
}
}

return total;
}

/* For a particular substance and unit of that substance, return the total
* amount used.
*/
export function calc_total_units(eventlist: Array<CalendarEvent>, substance: string, units: string) {

let total = 0;

for (let event of eventlist) {
if ((event as UseEvent).properties.substance == substance &&
(event as UseEvent).properties.units == units &&
(event as UseEvent).properties.amount != "unknown")
total = total + Number((event as UseEvent).properties.amount);
}

return total;
}

export function calc_days_since_last_use(eventlist: Array<CalendarEvent>, category: string, date : string) {
let use_days_str: Array<string> = calc_use_days(eventlist, category, true);

if (use_days_str.length < 1)
return NaN;

let last_day = use_days_str.reduce(function(acc, cur) {
let d = new Date(cur);
return (d > acc) ? d : acc;
}, new Date(use_days_str[0]));

return (((new Date(date)).valueOf() - last_day.valueOf()) / CVT_MS_DAY)
}
20 changes: 11 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
*
* Defines various constants used elsewhere.
*
* Copyright (c) 2023, Michael Pascale <[email protected]>
* Last modified: 2023-07-26
* Copyright (c) 2024, Michael Pascale <[email protected]>
* Last modified: 2024-08-26
*/

export const VERSION = '3.0.0';

// Time conversion factors.
export const CVT_MS_S = 1000;
export const CVT_S_MIN = 60;
Expand All @@ -17,14 +19,14 @@ export const CVT_MS_DAY = CVT_MS_S * CVT_S_MIN * CVT_MIN_HR * CVT_HR_DAY;
export const CAL_HEIGHT_DIFF = 205;


export const COLOR_WHITE = '#FFFFFF'
export const COLOR_TEXT = '#15141A'
export const COLOR_WHITE = '#FFFFFF';
export const COLOR_TEXT = '#15141A';

export const COLOR_PURPLE = '#485FC7'
export const COLOR_ORANGE = '#E67975'
export const COLOR_YELLOW = '#F7DBA7'
export const COLOR_GREEN = '#041F1E'
export const COLOR_BROWN = '#5A352A'
export const COLOR_PURPLE = '#485FC7';
export const COLOR_ORANGE = '#E67975';
export const COLOR_YELLOW = '#F7DBA7';
export const COLOR_GREEN = '#041F1E';
export const COLOR_BROWN = '#5A352A';

// Regular Expressions for input validation.
export const RGX_INTEGER = /^\d+$/;
Expand Down
Loading

0 comments on commit bf0e8a7

Please sign in to comment.