From dba4d13ebcc9ee4b8a46683242a8f63f1373451e Mon Sep 17 00:00:00 2001 From: godfreyyeung Date: Mon, 25 Oct 2021 15:38:20 -0400 Subject: [PATCH] Improve clarity of top-bottom-code-estimate --- utils/top-bottom-code-estimate.js | 56 ++++++++++++------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/utils/top-bottom-code-estimate.js b/utils/top-bottom-code-estimate.js index dfc18ff..3f305a4 100644 --- a/utils/top-bottom-code-estimate.js +++ b/utils/top-bottom-code-estimate.js @@ -21,15 +21,23 @@ function topBottomCodeEstimate(estimate, variable, year, isPrevious, config) { const codingRule = get(topBottomCodings, `${year}.${variable}`); - if (isPrevious) { - const { - preInflation: preInflationUpper, - postInflation: postInflationUpper - } = get(codingRule, 'upper'); + const { + preInflation: preInflationUpper, + postInflation: postInflationUpper + } = get(codingRule, 'upper'); + + const { + preInflation: preInflationLower, + postInflation: postInflationLower + } = get(codingRule, 'lower'); + if (isPrevious) { if (estimate === preInflationUpper) { mutatedEstimate = postInflationUpper; codingThreshold = 'upper'; + } else if (estimate === preInflationLower) { + mutatedEstimate = postInflationLower; + codingThreshold = 'lower'; } else { if ( config.options && @@ -42,41 +50,19 @@ function topBottomCodeEstimate(estimate, variable, year, isPrevious, config) { if (mutatedEstimate >= postInflationUpper) { mutatedEstimate = postInflationUpper; codingThreshold = 'upper'; + } else if (mutatedEstimate <= postInflationLower) { + mutatedEstimate = postInflationLower; + codingThreshold = 'lower'; } } - } else if (estimate >= get(codingRule, 'upper')) { + } else { // current year + if (estimate <= get(codingRule, 'lower')) { + mutatedEstimate = get(codingRule, 'lower'); + codingThreshold = 'lower'; + } else if (estimate >= get(codingRule, 'upper')) { mutatedEstimate = get(codingRule, 'upper'); codingThreshold = 'upper'; - } - - if (!codingThreshold && isPrevious) { - mutatedEstimate = estimate; - - const { - preInflation: preInflationLower, - postInflation: postInflationLower - } = get(codingRule, 'lower'); - - if (estimate === preInflationLower) { - mutatedEstimate = postInflationLower; - codingThreshold = 'lower'; - } else { - if ( - config.options && - config.options.transform && - config.options.transform.inflate - ) { - mutatedEstimate = estimate * INFLATION_FACTOR - } - - if (mutatedEstimate <= postInflationLower) { - mutatedEstimate = postInflationLower; - codingThreshold = 'lower'; - } } - } else if (estimate <= get(codingRule, 'lower')) { - mutatedEstimate = get(codingRule, 'lower'); - codingThreshold = 'lower'; } return { mutatedEstimate, codingThreshold };