Skip to content

Commit

Permalink
Merge pull request #179 from NYCPlanning/godfrey-clean-top-bottom-bin…
Browse files Browse the repository at this point in the history
…ning

Improve clarity of top-bottom-code-estimate
  • Loading branch information
godfreyyeung authored Oct 25, 2021
2 parents b773061 + dba4d13 commit 0f13f49
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions utils/top-bottom-code-estimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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 };
Expand Down

0 comments on commit 0f13f49

Please sign in to comment.