Skip to content

Commit

Permalink
Cleans '/src/utils' through 'interpolation.js'. codeforboston#1018
Browse files Browse the repository at this point in the history
  • Loading branch information
knod committed Dec 2, 2018
1 parent 4d47463 commit 5bc13f7
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 137 deletions.
18 changes: 9 additions & 9 deletions src/utils/CLIENT_DEFAULTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import { cloneDeep } from 'lodash';
*/
const CLIENT_DEFAULTS = {

// Property Name | Default Value | Valid Value Description
// Property Name | Default Value | Valid Value Description

// @todo get this value from the app somewhere
USState: 'MA', // Two-letter code denoting the state the client is in
// @todo get this value from the app somewhere
USState: `MA`, // Two-letter code denoting the state the client is in

current: {
// Current programs
benefits: [], // Benefit names (e.g. 'snap', 'section8')
benefits: [], // Benefit names (e.g. 'snap', 'section8')
// Household
// 'm_' for 'member'
household: [
{ // 'head' member // one or more member objects
m_age: 30, // whole number
m_role: 'head', // 'head', 'spouse', 'member'
{ // 'head' member // one or more member objects
m_age: 30, // whole number
m_role: `head`, // 'head', 'spouse', 'member'
m_disabled: false, // boolean
},
],
Expand Down Expand Up @@ -53,7 +53,7 @@ const CLIENT_DEFAULTS = {
disabledMedical: 0, // positive number
otherMedical: 0, // positive number
/** @todo Make housing expense values more robust. */
housing: 'homeless', // 'homeless', 'homeowner', 'renter', 'voucher'
housing: `homeless`, // 'homeless', 'homeowner', 'renter', 'voucher'
contractRent: 0, // positive number
rentShare: 0, // positive number
rent: 0, // positive number
Expand All @@ -78,7 +78,7 @@ const CLIENT_DEFAULTS = {
},

// Note: A `future` value will default to the client's `current` value until a user changes it explicitly
future: {}, // same structure // same
future: {}, // same structure // same

};

Expand Down
24 changes: 12 additions & 12 deletions src/utils/cashflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
const getDependentCostsMonthly = function (client) {
let props = UNDER13_CARE_EXPENSES.concat(OVER12_CARE_EXPENSES);
return sumProps(client, props);
}; // End getDependentCostsMonthly()
};


/**
Expand All @@ -47,27 +47,27 @@ const getDependentCostsMonthly = function (client) {
*/
const getUnder13Expenses = function (client) {
return sumProps(client, UNDER13_CARE_EXPENSES);
}; // End getUnder13Expenses()
};


const getOver12Expenses = function (client) {
return sumProps(client, OVER12_CARE_EXPENSES);
}; // End getOver13Expenses()
};


const getMedicalExpenses = function (client) {
return sumProps(client, ALL_MEDICAL_EXPENSES);
}; // End getMedicalExpenses()
};


const getNonTransportCareCosts = function (client) {
return sumProps(client, NON_TRANSPORT_DEPENDENT_COSTS);
}; // End getNonTransportationCareCosts()
};


const getTransportDependentCosts = function (client) {
return sumProps(client, TRANSPORT_DEPENDENT_COSTS);
}; // End getTransportDependentCosts()
};


// ==================================
Expand All @@ -84,7 +84,7 @@ const getHousingCosts = function (client) {
} else if (housing === `homeowner`) {
return sumProps(client, HOMEOWNER_COSTS);
}
}; // End getHousingCosts()
};


// ==================================
Expand All @@ -101,7 +101,7 @@ const getHousingCosts = function (client) {
*/
const getGrossUnearnedIncomeMonthly = function (client) {
return sumProps(client, UNEARNED_INCOME_SOURCES);
}; // End getGrossUnearnedIncomeMonthly()
};


/**
Expand All @@ -115,10 +115,10 @@ const getGrossUnearnedIncomeMonthly = function (client) {
* income with no deductions or exclusions.
*/
const getSimpleGrossIncomeMonthly = function (client) {
let earned = client.earned,
unearned = getGrossUnearnedIncomeMonthly(client);
let earned = client.earned,
unearned = getGrossUnearnedIncomeMonthly(client);
return earned + unearned;
}; // End getSimpleGrossIncomeMonthly()
};


// ==================================
Expand All @@ -136,7 +136,7 @@ const getSimpleGrossIncomeMonthly = function (client) {
*/
const sumProps = function (obj, props) {
return sum(values(pick(obj, props)));
}; // End sumProps()
};


export {
Expand Down
42 changes: 21 additions & 21 deletions src/utils/convert-by-timescale.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ let toYearlyFrom = function (amount, startTimescale) {

let converted = amount;

if (startTimescale === 'weekly') {
if (startTimescale === `weekly`) {
converted = amount * 52;
} else if (startTimescale === 'monthly') {
} else if (startTimescale === `monthly`) {
converted = amount * 12;
} else if (startTimescale === 'yearly') {
} else if (startTimescale === `yearly`) {
// do nothing
}

return converted;
}; // End toYearlyFrom()
};


/** Returns `amount` converted from whatever 'timescale' it was before
Expand All @@ -36,16 +36,16 @@ let toMonthlyFrom = function (amount, startTimescale) {

let converted = amount;

if (startTimescale === 'weekly') {
if (startTimescale === `weekly`) {
converted = amount * (4 + (1 / 3));
} else if (startTimescale === 'monthly') {
} else if (startTimescale === `monthly`) {
// do nothing
} else if (startTimescale === 'yearly') {
} else if (startTimescale === `yearly`) {
converted = amount / 12;
}

return converted;
}; // End toMonthlyFrom()
};


/** Returns `amount` converted from whatever 'timescale' it was before
Expand All @@ -59,35 +59,35 @@ let toWeeklyFrom = function (amount, startTimescale) {

let converted = amount;

if (startTimescale === 'weekly') {
if (startTimescale === `weekly`) {
// do nothing
} else if (startTimescale === 'monthly') {
} else if (startTimescale === `monthly`) {
converted = amount / (4 + (1 / 3));
} else if (startTimescale === 'yearly') {
} else if (startTimescale === `yearly`) {
converted = amount * 52;
}

return converted;
}; // End toWeeklyFrom()
};

let timescaleMultipliers = {};

timescaleMultipliers.fromYearly = {
'Weekly': 1 / 12 / (4 + 1 / 3),
'Monthly': 1 / 12,
'Yearly': 1,
Weekly: 1 / 12 / (4 + 1 / 3),
Monthly: 1 / 12,
Yearly: 1,
};

timescaleMultipliers.fromMonthly = {
'Weekly': 1 / (4 + 1 / 3),
'Monthly': 1,
'Yearly': 12,
Weekly: 1 / (4 + 1 / 3),
Monthly: 1,
Yearly: 12,
};

timescaleMultipliers.fromWeekly = {
'Weekly': 1,
'Monthly': (4 + 1 / 3),
'Yearly': 52,
Weekly: 1,
Monthly: (4 + 1 / 3),
Yearly: 52,
};


Expand Down
5 changes: 1 addition & 4 deletions src/utils/convertForUpdate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
const convertForUpdate = function ({ name, route, ...otherProps }) {

let forUpdate = {
Expand All @@ -10,7 +7,7 @@ const convertForUpdate = function ({ name, route, ...otherProps }) {

return forUpdate;

}; // End convertForUpdate()
};


export { convertForUpdate };
64 changes: 34 additions & 30 deletions src/utils/getGovData.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* Getting or calculating data values by leveraging common data patterns
* we've seen so far.
* @module
*/

import { moneyToWholeNum } from './math';

/**
* Getting or calculating data values by leveraging common data patterns
* we've seen so far.
*/

/** Calculate appropriate bracket/limit value (such as income
* limit) by number of relevant items (such as number of
Expand All @@ -25,22 +27,23 @@ import { moneyToWholeNum } from './math';
* @param {object} data Data to use to get a bracket/limit value.
* @param {number} data.0 Never known to equal more than 0 so far.
* @param {number} data.1 (Or any int key) Value of bracket/limit that
* matches the number described by the key. For example, data.3 would be
* the income limit value for a household with three members.
* matches the number described by the key. For example, data.3 would be
* the income limit value for a household with three members.
* @param {number|function} data.eachAdditional Usually an amount to
* add for each person or item over the maximum hardcoded limits. Can be a
* function to calculate said amount based on number of extra items.
* add for each person or item over the maximum hardcoded limits. Can be a
* function to calculate said amount based on number of extra items.
* @param {number} numItems Number of items (for example, household size).
* @param {number} [percent] Multiplies the result before sending it back.
* You'd pass in 100% as `100`.
* You'd pass in 100% as `100`.
*
* @returns Data value determined for the number of items, numItems, wanted.
* @returns {number}
*/
const getLimitBySize = function (data, numItems, percent) {
// @todo Deal with non-number values?

let safePerc = percent || 100,
limit = null,
maxGiven = getMaxIntKey(data);
let safePerc = percent || 100,
limit = null,
maxGiven = getMaxIntKey(data);

if (numItems <= maxGiven) {

Expand All @@ -56,22 +59,23 @@ const getLimitBySize = function (data, numItems, percent) {

// The right kind of math as observed in MA data tables
return moneyToWholeNum(limit * (safePerc / 100));
}; // End getLimitBySize()
};


/** Deals with different value types for data.eachAdditional
*
* @function
* @param {number} numExtra Number of extra items
* @param {number|function} eachAdditional Either a number value to add
* for each extra item or a function that will return that number.
*
* @returns {number} The amount created by those extra items.
*/
*
* @function
* @param {number} numExtra Number of extra items
* @param {number|function} eachAdditional Either a number value to add
* for each extra item or a function that will return that number.
*
* @returns {number} The amount created by those extra items.
*/
const getExtraAmount = function (data, numExtra) {
// @todo Deal with non-number values?

let extraAmount = 0,
eachAdditional = data.eachAdditional;
let extraAmount = 0,
eachAdditional = data.eachAdditional;

// Either allow additional amount to be calculated
// or add a hard-coded amount.
Expand All @@ -81,20 +85,20 @@ const getExtraAmount = function (data, numExtra) {

} else { // Assumed either number or falsy

/** @todo Future discussioin - flexibility vs. consistency */
/* @todo Future discussion - flexibility vs. consistency */
let overageRate = eachAdditional || 0;
extraAmount = numExtra * overageRate;

}

return extraAmount;
}; // End getExtraAmount()
};


/**
* Of the keys in an object that can be converted to integers,
* return the highest converted value.
*/
* Of the keys in an object that can be converted to integers,
* return the highest converted value.
*/
const getMaxIntKey = function (data) {
let max = 0;
for (let key in data) {
Expand All @@ -106,7 +110,7 @@ const getMaxIntKey = function (data) {

}
return max;
}; // End getMaxIntKey()
};


export {
Expand Down
Loading

0 comments on commit 5bc13f7

Please sign in to comment.