Created
September 27, 2017 11:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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