Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created September 27, 2017 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/f40dc26ec2ce4c0fad2338a99d952116 to your computer and use it in GitHub Desktop.
Save barneycarroll/f40dc26ec2ce4c0fad2338a99d952116 to your computer and use it in GitHub Desktop.
An example of static, serialized logic for how an application model should be processed
const endOfTime = '9999-12-31'
const conditions = [
{
demographic: {
gender: 'female',
dob: '1954-10-06',
},
retirement: {
type: 'fixed',
value: '2020-09-06',
},
},
{
demographic: {
gender: 'female',
dob: '1960-05-06',
},
retirement: {
type: 'addition',
value: {
years: 66,
months: 1,
},
},
},
{
demographic: {
gender: 'female',
dob: endOfTime,
},
retirement: {
type: 'addition',
value: {
years: 68,
},
},
},
]
const $ = selector =>
document.querySelector(selector)
const demographicMatch = ({subject, target}) =>
subject.gender === target.gender
&& moment(subject.dob).isBefore(target.dob)
const calculate = () => {
const dob = $('#dob').value
const gender = $('#gender').value
const match = conditions.find(condition =>
demographicMatch({
subject : {dob, gender},
target : condition.demographic
})
)
// This nested ternary queries the match
// and produces a string indicating our outcome
// In a structured application you'd probably break out this function
// so that it can be tested independently
const outcome = (
! match ? 'No idea'
: moment(
match.retirement.type === 'addition'
? moment(dob).add(match.retirement.value)
: moment(match.retirement.value)
).format('MMMM YYYY')
)
$('#pensionDate').innerHTML = outcome
}
document.addEventListener('input', calculate)
calculate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment