import _ from 'lodash'; import acanthro from './growthstandards/acanthro.json'; import bmianthro from './growthstandards/bmianthro.json'; import hcanthro from './growthstandards/hcanthro.json'; import lenanthro from './growthstandards/lenanthro.json'; import ssanthro from './growthstandards/ssanthro.json'; import tsanthro from './growthstandards/tsanthro.json'; import weianthro from './growthstandards/weianthro.json'; import wfhanthro from './growthstandards/wfhanthro.json'; import wflanthro from './growthstandards/wflanthro.json'; const growthStandards: any = { acanthro, bmianthro, hcanthro, lenanthro, ssanthro, tsanthro, weianthro, wfhanthro, wflanthro, }; // rounding function function round(value: number, digitsPastDecimal = 3): number { const multiplier = parseFloat('1'.padEnd(digitsPastDecimal+1,'0')) return Math.round((value + Number.EPSILON) * multiplier) / multiplier } // Lookup function for growth standards function lookupGrowthStandards(key: number | Array, colKeyMatch: string | Array, filename: string): Record | undefined { if (_.isEmpty(key)) return undefined; const attributes = growthStandards[filename]; if (Array.isArray(colKeyMatch) && Array.isArray(key) && key.length && colKeyMatch.length && key.length > 1 && key.length === colKeyMatch.length) { let multioutput = attributes; colKeyMatch.forEach((colKey: string, index) => { if (Array.isArray(multioutput)) { multioutput = multioutput.filter((n) => n[colKey] === key[index]) }; // eslint-disable-line max-len }); return multioutput[0] || undefined; } const output = !Array.isArray(colKeyMatch) ? attributes.find( (n: Record) => n[colKeyMatch] === key, ) : undefined; return output || undefined; } // days of the month constant const ANTHRO_DAYS_OF_MONTH: number = 30.4375; // cleans the sex variable function standardize_sex_var(sex: string | number): 1 | 2 | undefined { const out = (typeof sex === 'number') ? _.toLower(_.trim(sex.toString())) : _.toLower(_.trim(sex)); if (out === 'm' || out === '1') { return 1; } if (out === 'f' || out === '2') { return 2; } console.error("Sex variable not 'm', 'f', '1', or '2'"); return undefined; } function standardize_oedema_var(oedema: string): string { const outOedema = _.toLower(_.trim(oedema)); if (outOedema === '1') return 'y'; if (outOedema === '2') return 'n'; if (outOedema === 'y') return 'y'; return 'n'; } function age_to_days(age: number, isAgeInMonth: boolean): number { // age in days is always treated as rounded integers if (isAgeInMonth) { return round(age * ANTHRO_DAYS_OF_MONTH); } return age; } function age_to_months(age: number, isAgeInMonth: boolean): number { if (isAgeInMonth) { return age; } return age / ANTHRO_DAYS_OF_MONTH; } // ' Groups age in months into groups // ' @param age_in_months numeric vector of positive ages // ' @noRd function anthro_age_groups(age_in_months: number): string | undefined { if (typeof age_in_months !== 'number') return undefined; if (age_in_months > 0 && age_in_months <= 5) return '00-05 mo'; if (age_in_months > 5 && age_in_months <= 11) return '06-11 mo'; if (age_in_months > 11 && age_in_months <= 23) return '12-23 mo'; if (age_in_months > 23 && age_in_months <= 35) return '24-35 mo'; if (age_in_months > 35 && age_in_months <= 47) return '36-47 mo'; if (age_in_months > 47 && age_in_months <= 59) return '48-59 mo'; return undefined; } export { lookupGrowthStandards, standardize_sex_var, standardize_oedema_var, age_to_days, age_to_months, anthro_age_groups, round };