Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const acceptor = (predicate, mutation) => ({
predicate: predicate,
mutation: mutation
});
export const createModel = update => {
const acceptors = [
acceptor(
proposal => proposal.name === `precipitations`,
proposal => update(model => {
model.precipitations = proposal.value;
return model;
})
),
acceptor(
proposal => proposal.name === `precipitation`,
proposal => update(model => {
model.precipitation = proposal.value;
return model;
})
),
acceptor(
proposal => proposal.name === `date`,
proposal => update(model => {
model.date = proposal.value;
return model;
})
),
acceptor(
proposal => proposal.name === `increase`,
proposal => update(model => {
model.value = model.value + proposal.amount;
return model;
})
),
acceptor(
proposal => proposal.name === `changeUnits`,
proposal => update(model => {
if (model.units === "C") {
model.units = "F";
model.value = Math.round( model.value * 9 / 5 + 32 );
}
else {
model.units = "C";
model.value = Math.round( (model.value - 32) / 9 * 5 );
}
return model;
})
),
];
return {
present: proposal => {
acceptors.forEach(acceptor => {
if(acceptor.predicate(proposal)) {
acceptor.mutation(proposal);
}
});
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.