Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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