Skip to content

Instantly share code, notes, and snippets.

@knod
Last active October 16, 2017 22:03
Show Gist options
  • Save knod/37452cbfef13d4b017300e837772fd6a to your computer and use it in GitHub Desktop.
Save knod/37452cbfef13d4b017300e837772fd6a to your computer and use it in GitHub Desktop.
Alternative code for Result if 1. it's kept as a class, 2. it's made into an object constructor, or 3 or 4, which are 1 and 2, but slightly rearranged.
/**
* All benefit programs' return value should be an instance of this class.
*
* @todo Implement `.expirationDate` to keep track of data that needs updating
* @todo Figure out how to access this jsdoc definition externally.
*
* @external
*
* @class
* @param {object} trial - Data to try out/validate
* @param {string} trial.result - valid values: 'good', 'information', 'warning'
* Proposed valid values: 'good', 'warning', 'bad'. Also, clearer name needed.
* Maybe 'status'.
* @param {string} trial.details - Explanation of results that will be seen
* by the user. Example: 'Your income is x% FPL. Your benefit could go down'.
* Should this be allowed to have a value of `undefined` or `null`?
* @param {string} trial.benefitValue - Monthly subsidy this benefit will provide.
* @param {object} [trial.data] - Any other necessary information.
* @param {object} [trial.expirationDate] - A `Date` object. In future this
* class/object may provide a flag to coders.
*/
class Result {
constructor ( trial ) {
let errorMessage = checkTypes( trial );
/** Check the 2nd line of your error stack to find issue */
if ( errorMessage.length > 0 ) { throw new Error( errorMessage ) }
let result = this;
result.result = trial.result;
result.details = trial.details;
result.benefitValue = trial.benefitValue;
result.data = trial.data;
} // End constructor()
}; // End Result class
// ========================================
// ========================================
// (IGNORE) VALIDATION OF RESULT PROPERTIES
// ========================================
// ========================================
/** @todo Add function description */
const checkTypes = function ( data ) {
/** @todo Is there a way to loop through to do this? I attempted
* it with a 'types' object to check types, but type tests aren't
* enough. For example, `expirationDate` needs to be tested for
* `instanceof Date`. */
var invalid = [],
result = data.result,
benefitValue = data.benefitValue,
details = data.details; // Should this be allowed to be undefined?
// , date = data.expirationDate;
if ( typeof pushUndefined( 'result', result, invalid ) !== 'string' ) {
if ( typeof result !== 'string' ) {
invalid.push( 'The value of `result` was type of ' + (typeof result) + ' instead of "string".' );
}
}
if ( typeof pushUndefined( 'details', details, invalid ) !== 'string' ) {
if ( typeof details !== 'string' ) {
invalid.push( 'The value of `details` was type of ' + (typeof details) + ' instead of "string".' );
}
}
if ( typeof pushUndefined( 'benefitValue', benefitValue, invalid ) !== 'string' ) {
if ( typeof benefitValue !== 'number' ) {
invalid.push( 'The value of `benefitValue` was type of ' + (typeof benefitValue) + ' instead of "number".' );
}
}
// if ( typeof pushUndefined( 'expirationDate', date, invalid ) !== 'string' ) {
// if ( !( date instanceof Date ) ) {
// invalid.push( 'The value of `expirationDate` was not an instance of `Date`.' );
// }
// }
var error = invalid.join( ' ' ); // empty array returns a string
return error;
}
/** @todo Add function description */
const pushUndefined = function ( key, val, invalidArray ) {
var error = null
if ( val === undefined ) {
error = 'The value of `' + key + '` is not allowed to be `undefined`.'
invalidArray.push( error );
}
return error;
}; // End pushUndefined()
// ========================================
// ========================================
// EXPORTS
// ========================================
// ========================================
export { Result }
/**
* Everything else the same, but declaration of `result` var moved.
*
* It can be more clear and consistent /in general in a codebase/ if all
* declarations of a `this` holder are always at the very top of the class
* constructor.
*/
class Result {
constructor ( trial ) {
let result = this;
let errorMessage = checkTypes( trial );
/** Check the 2nd line of your error stack to find issue */
if ( errorMessage.length > 0 ) { throw new Error( errorMessage ) }
result.result = trial.result;
result.details = trial.details;
result.benefitValue = trial.benefitValue;
result.data = trial.data;
} // End constructor()
}; // End Result class
/**
* All benefit programs' return value should be an instance of this object.
* Create with `const myResult = new Result( myValues );`
*
* @todo Implement `.expirationDate` to keep track of data that needs updating
* @todo Figure out how to access this jsdoc definition externally.
*
* @external
*
* @param {object} trial - Data to try out/validate
* @param {string} trial.result - valid values: 'good', 'information', 'warning'
* Proposed valid values: 'good', 'warning', 'bad'. Also, clearer name needed.
* Maybe 'status'.
* @param {string} trial.details - Explanation of results that will be seen
* by the user. Example: 'Your income is x% FPL. Your benefit could go down'.
* Should this be allowed to have a value of `undefined` or `null`?
* @param {string} trial.benefitValue - Monthly subsidy this benefit will provide.
* @param {object} [trial.data] - Any other necessary information.
* @param {object} [trial.expirationDate] - A `Date` object. In future this
* class/object may provide a flag to coders.
*/
const Result = function ( trial ) {
let errorMessage = checkTypes( trial );
/** Check the 2nd line of your error stack to find issue */
if ( errorMessage.length > 0 ) { throw new Error( errorMessage ) }
let result = {};
result.result = trial.result;
result.details = trial.details;
result.benefitValue = trial.benefitValue;
result.data = trial.data;
return result;
}; // End Result {}
// ========================================
// ========================================
// (IGNORE) VALIDATION OF RESULT PROPERTIES
// ========================================
// ========================================
/** @todo Add function description */
const checkTypes = function ( data ) {
/** @todo Is there a way to loop through to do this? I attempted
* it with a 'types' object to check types, but type tests aren't
* enough. For example, `expirationDate` needs to be tested for
* `instanceof Date`. */
var invalid = [],
result = data.result,
benefitValue = data.benefitValue,
details = data.details; // Should this be allowed to be undefined?
// , date = data.expirationDate;
if ( typeof pushUndefined( 'result', result, invalid ) !== 'string' ) {
if ( typeof result !== 'string' ) {
invalid.push( 'The value of `result` was type of ' + (typeof result) + ' instead of "string".' );
}
}
if ( typeof pushUndefined( 'details', details, invalid ) !== 'string' ) {
if ( typeof details !== 'string' ) {
invalid.push( 'The value of `details` was type of ' + (typeof details) + ' instead of "string".' );
}
}
if ( typeof pushUndefined( 'benefitValue', benefitValue, invalid ) !== 'string' ) {
if ( typeof benefitValue !== 'number' ) {
invalid.push( 'The value of `benefitValue` was type of ' + (typeof benefitValue) + ' instead of "number".' );
}
}
// if ( typeof pushUndefined( 'expirationDate', date, invalid ) !== 'string' ) {
// if ( !( date instanceof Date ) ) {
// invalid.push( 'The value of `expirationDate` was not an instance of `Date`.' );
// }
// }
var error = invalid.join( ' ' ); // empty array returns a string
return error;
}
/** @todo Add function description */
var pushUndefined = function ( key, val, invalidArray ) {
var error = null
if ( val === undefined ) {
error = 'The value of `' + key + '` is not allowed to be `undefined`.'
invalidArray.push( error );
}
return error;
}; // End pushUndefined()
// ========================================
// ========================================
// EXPORTS
// ========================================
// ========================================
export { Result }
/**
* Everything else the same, but declaration of `result` var moved.
*
* It can be more clear and consistent /in general in a codebase/ if all
* declarations of the returned object are always at the very top of the
* object constructor.
*/
const Result = function ( trial ) {
let result = {};
let errorMessage = checkTypes( trial );
/** Check the 2nd line of your error stack to find issue */
if ( errorMessage.length > 0 ) { throw new Error( errorMessage ) }
result.result = trial.result;
result.details = trial.details;
result.benefitValue = trial.benefitValue;
result.data = trial.data;
return result;
}; // End Result {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment