diff --git a/src/index.js b/src/index.js index 295c8e36..92800312 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ import ReactDOM from 'react-dom'; import './index.css'; import './styles/dev.css'; import App from './App'; -import registerServiceWorker from './utils/registerServiceWorker'; +import { registerServiceWorker } from './utils/registerServiceWorker'; import 'semantic-ui-css/semantic.min.css'; ReactDOM.render(, document.getElementById('root')); diff --git a/src/utils/math.js b/src/utils/math.js index 500c27c8..3d177fbf 100644 --- a/src/utils/math.js +++ b/src/utils/math.js @@ -1,4 +1,6 @@ -/** For different kinds of math operations we need, some unconventional */ +/** For different kinds of math operations we need, some unconventional + * @module + */ const sum = function (vals) { let total = 0; @@ -14,13 +16,14 @@ const roundMoney = function (val) { // storage objects, keep things exact. Also, this doesn't restrict // to two decimal places. Do that in the input's attributes. return (Math.round(val * 100) / 100); // val = '' returns 0 -}; // End roundMoney() +}; /** This is how we've seen it done in MA tables * * @see Math was observed at {link http://www.mass.gov/eohhs/docs/masshealth/deskguides/fpl-deskguide.pdf} * @see More notes on data at {link https://docs.google.com/document/d/1DRNm1TLP31s_yDdsH8IDoRV7_KjjJ46NyAaZOgLoQmY/edit#} + * @todo Implement US State specific rounding? Benefit program-specific? */ const moneyToWholeNum = function (val) { return Math.ceil(val); @@ -28,25 +31,23 @@ const moneyToWholeNum = function (val) { /** -* Turns a value into a float, limits it in between min and max, and -* makes sure to return a number (not NaN). -* -* @todo Testing required -*/ + * Turns a value into a float, limits it in between min and max, and + * makes sure to return a number (not NaN). + */ const limit = function (initialVal, minMax) { - /** @todo Add trailing 0's somewhere */ + // @todo Add trailing 0's somewhere let min = minMax.min, max = minMax.max; let raw = parseFloat(initialVal), value = raw; - if (typeof min === 'number' && !isNaN(min)) { + if (typeof min === `number` && !isNaN(min)) { value = Math.max(min, raw); } - if (typeof max === 'number' && !isNaN(max)) { + if (typeof max === `number` && !isNaN(max)) { value = Math.min(max, raw); } @@ -55,19 +56,19 @@ const limit = function (initialVal, minMax) { } return value; -}; // End limit() +}; let toMonthlyAmount = {}; +/** @see {@link https://docs.google.com/document/d/13kb1hsxMi6pN9oAUGsTatDz4OSX5IeDLF9B-ddPjMCk/edit#heading=h.hxz256tmbsz9} */ toMonthlyAmount.weekly = function (evnt, weeklyVal) { - /** @see {@link https://docs.google.com/document/d/13kb1hsxMi6pN9oAUGsTatDz4OSX5IeDLF9B-ddPjMCk/edit#heading=h.hxz256tmbsz9} */ - let monthlyRaw = weeklyVal * (4 + 1 / 3), - monthly = toMonthlyAmount[ 'monthly' ](evnt, monthlyRaw); + let monthlyRaw = weeklyVal * (4 + 1 / 3), + monthly = toMonthlyAmount[ `monthly` ](evnt, monthlyRaw); return monthly; -}; // End toMonthlyAmount.weekly() +}; toMonthlyAmount.monthly = function (evnt, monthlyVal) { @@ -75,17 +76,17 @@ toMonthlyAmount.monthly = function (evnt, monthlyVal) { let monthlyInBounds = limit(monthlyVal, { min: 0 }); return monthlyInBounds; -}; // End toMonthlyAmount.monthly() +}; +/** @see {@link https://docs.google.com/document/d/13kb1hsxMi6pN9oAUGsTatDz4OSX5IeDLF9B-ddPjMCk/edit#heading=h.hxz256tmbsz9} */ toMonthlyAmount.yearly = function (evnt, yearlyVal) { - /** @see {@link https://docs.google.com/document/d/13kb1hsxMi6pN9oAUGsTatDz4OSX5IeDLF9B-ddPjMCk/edit#heading=h.hxz256tmbsz9} */ - let monthlyRaw = (yearlyVal / 12), - monthly = toMonthlyAmount[ 'monthly' ](evnt, monthlyRaw); + let monthlyRaw = (yearlyVal / 12), + monthly = toMonthlyAmount[ `monthly` ](evnt, monthlyRaw); return monthly; -}; // End toMonthlyAmount.yearly() +}; export { diff --git a/src/utils/objectKeyPaths.js b/src/utils/objectKeyPaths.js index 07cc039c..5eaa0865 100644 --- a/src/utils/objectKeyPaths.js +++ b/src/utils/objectKeyPaths.js @@ -70,7 +70,7 @@ * // [ 'e', 'f', 'g', 'h' ] * // ] */ -const getKeyPathsArray = (obj, stripVersions, base = []) => { +const getKeyPathsArray = function (obj, stripVersions, base = []) { // Array to contain our keys paths (if any) let pathsArr = [], versionRegex = /_v\d+$/; @@ -80,7 +80,7 @@ const getKeyPathsArray = (obj, stripVersions, base = []) => { return pathsArr; // Return an array of paths to each prop using the base path and the key in each loop iteration - } else if (typeof obj === 'object') { + } else if (typeof obj === `object`) { const keys = Object.keys(obj).sort(); // Handle having an empty object @@ -109,13 +109,13 @@ const getKeyPathsArray = (obj, stripVersions, base = []) => { pathsArr = pathsArr.concat(childPaths); } return pathsArr; - } + } // ends if there are or aren't any keys // Otherwise, we've got some sort of primative such as a str, num, etc, so don't do anything } else { return pathsArr; - } -}; + } // ends if is or isn't an array +}; // Ends getKeyPathsArray() /** @@ -146,7 +146,7 @@ const getKeyPathsArray = (obj, stripVersions, base = []) => { * // 'e.f.g.h' * // ] */ -const getKeyPathStrings = (keyPathsArr) => { +const getKeyPathStrings = function (keyPathsArr) { return keyPathsArr.map((keyPath) => { return keyPath.join('.'); }); diff --git a/src/utils/prettifiers.js b/src/utils/prettifiers.js index 116d2614..c6edf4f2 100644 --- a/src/utils/prettifiers.js +++ b/src/utils/prettifiers.js @@ -1,9 +1,10 @@ /** * Functions for making client values into values * that users will see. + * @module */ -/** @todo Put code from #226 in here */ +// @todo Reduce duplication in money calculation and formatting const toMoneyStr = function (decimal) { return (decimal).toFixed(2); }; diff --git a/src/utils/registerServiceWorker.js b/src/utils/registerServiceWorker.js index 157c6fed..d67fc539 100644 --- a/src/utils/registerServiceWorker.js +++ b/src/utils/registerServiceWorker.js @@ -11,17 +11,17 @@ // This link also includes instructions on opting out of this behavior. const isLocalhost = Boolean( - window.location.hostname === 'localhost' || + window.location.hostname === `localhost` || // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || - // 127.0.0.1/8 is considered localhost for IPv4. - window.location.hostname.match( - /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ - ) + window.location.hostname === `[::1]` || + // 127.0.0.1/8 is considered localhost for IPv4. + window.location.hostname.match( + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ) ); -export default function register() { - if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { +const registerServiceWorker = function () { + if (process.env.NODE_ENV === `production` && `serviceWorker` in navigator) { // The URL constructor is available in all browsers that support SW. const publicUrl = new URL(process.env.PUBLIC_URL, window.location); if (publicUrl.origin !== window.location.origin) { @@ -31,7 +31,7 @@ export default function register() { return; } - window.addEventListener('load', () => { + window.addEventListener(`load`, () => { const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; if (!isLocalhost) { @@ -42,10 +42,11 @@ export default function register() { checkValidServiceWorker(swUrl); } }); - } -} + } // ends if the URL constructor is available +}; + -function registerValidSW(swUrl) { +const registerValidSW = function (swUrl) { navigator.serviceWorker .register(swUrl) .then(registration => { @@ -58,30 +59,31 @@ function registerValidSW(swUrl) { // the fresh content will have been added to the cache. // It's the perfect time to display a "New content is // available; please refresh." message in your web app. - console.log('New content is available; please refresh.'); + console.log(`New content is available; please refresh.`); } else { // At this point, everything has been precached. // It's the perfect time to display a // "Content is cached for offline use." message. - console.log('Content is cached for offline use.'); + console.log(`Content is cached for offline use.`); } } }; }; - }) + }) // ends then register .catch(error => { - console.error('Error during service worker registration:', error); + console.error(`Error during service worker registration:`, error); }); -} +}; + -function checkValidServiceWorker(swUrl) { +const checkValidServiceWorker = function (swUrl) { // Check if the service worker can be found. If it can't reload the page. fetch(swUrl) .then(response => { // Ensure service worker exists, and that we really are getting a JS file. if ( response.status === 404 || - response.headers.get('content-type').indexOf('javascript') === -1 + response.headers.get(`content-type`).indexOf(`javascript`) === -1 ) { // No service worker found. Probably a different app. Reload the page. navigator.serviceWorker.ready.then(registration => { @@ -92,19 +94,28 @@ function checkValidServiceWorker(swUrl) { } else { // Service worker found. Proceed as normal. registerValidSW(swUrl); - } - }) + } // ends if service worker exists + }) // ends fetch response .catch(() => { console.log( - 'No internet connection found. App is running in offline mode.' + `No internet connection found. App is running in offline mode.` ); }); } -export function unregister() { - if ('serviceWorker' in navigator) { + +const unregister = function () { + if (`serviceWorker` in navigator) { navigator.serviceWorker.ready.then(registration => { registration.unregister(); }); } +}; + + +export { + registerServiceWorker, + registerValidSW, + checkValidServiceWorker, + unregister, } diff --git a/src/utils/setNestedProperty.js b/src/utils/setNestedProperty.js index 4fb9290c..2c51691d 100644 --- a/src/utils/setNestedProperty.js +++ b/src/utils/setNestedProperty.js @@ -1,6 +1,7 @@ import { valueFixers } from './valueFixers'; import { getSideEffects } from './getSideEffects'; + const setNestedProperty = function ({ route, value, time }, { current, future }, previouslySetByUser) { let itemID = route.shift(); @@ -20,38 +21,39 @@ const setNestedProperty = function ({ route, value, time }, { current, future }, }; setNestedProperty({ route, value, time }, next, previouslySetByUser); applySideEffects({ current, future, itemID }); - } + } // ends if last recursion because route is empty -}; // End setNestedProperty() +}; const setValidCurrent = function ({ name, value, type }, newCurrent) { - if (type === 'current') { + if (type === `current`) { newCurrent[ name ] = valueFixers[ name ](value, newCurrent); } return newCurrent; -}; // End setValidCurrent() +}; const setValidFuture = function (evnt, newFuture, setByUser) { let newValue = valueFixers[ evnt.name ](evnt.value, newFuture); - if (evnt.type === 'future') { + if (evnt.type === `future`) { newFuture[ evnt.name ] = newValue; - } else if (evnt.type === 'current') { + } else if (evnt.type === `current`) { // If this 'future' value hasn't been changed by the user // then it continue to be synched up with the 'current' // value. if (!setByUser) { newFuture[ evnt.name ] = newValue; } - } + } // ends if which part of the client object return newFuture; -}; // End setValidFuture() +}; + /** Trigger functions that could affect other client * values. Run after other client properties have diff --git a/src/utils/validators.js b/src/utils/validators.js index 2e25e741..bda28faa 100644 --- a/src/utils/validators.js +++ b/src/utils/validators.js @@ -16,15 +16,15 @@ const hasOnlyNonNegWholeNumberChars = function (str) { }; /** Returns true if a string represents a positve number (integer or float) */ -// Should this only be valid if it has <= 2 decimal places? const isNonNegNumber = function (str) { + // Should this only be valid if it has <= 2 decimal places? return str !== '' && !/[^0-9.]|\..*\./.test(str); }; /** Returns true if a string represents a positive integer */ -/** @todo Change name to 'isWholeNumber'. */ const isNonNegWholeNumber = function (str) { + // @todo Change name to 'isWholeNumber'. return str !== '' && /^[0-9]*$/.test(str); }; @@ -68,7 +68,7 @@ const isNumberlike = function (numberOrString) { return new TypeError(`Expected 'numberOrString' to be a number or string that can be converted to a number and it was ${numberOrString}.`); } -}; +}; // Ends isNumberlike() export { diff --git a/src/utils/valueFixers.js b/src/utils/valueFixers.js index ca6f3d3a..bd7f4932 100644 --- a/src/utils/valueFixers.js +++ b/src/utils/valueFixers.js @@ -1,23 +1,23 @@ -import { isNumberlike } from './validators'; - - /** * Transformers for transforming client * values into valid values. * @module */ +import { isNumberlike } from './validators'; + + const returnSame = function (newVal, state) { return newVal; }; const toBoolean = function (value) { - if (value === 'Yes') { + if (value === `Yes`) { return true; - } else if (value === 'No') { + } else if (value === `No`) { return false; - } else if (typeof(value) === 'boolean') { + } else if (typeof(value) === `boolean`) { return value; } else { return null; @@ -42,13 +42,10 @@ const toNumber = function (numberOrString) { } return Number(numberOrString); - }; -/** - * For every client property and - * nested property. - */ + +/** For every client property and nested property. */ const valueFixers = { // Current programs benefits: returnSame, @@ -59,7 +56,6 @@ const valueFixers = { m_disabled: returnSame, // MONEY AMOUNTS // Income - /** @todo All incomes need transformation */ earned: toNumber, TAFDC: toNumber, SSI: toNumber, @@ -86,7 +82,7 @@ const valueFixers = { earnedBecauseOfAdultCare: toNumber, disabledMedical: toNumber, otherMedical: toNumber, - /** @todo When client has section 8, switch this to 'housingVoucher' */ + // @todo When client has section 8, switch this to 'housingVoucher' housing: returnSame, contractRent: toNumber, rentShare: toNumber,