Skip to content

Instantly share code, notes, and snippets.

@jasongorman
Created April 6, 2020 11:21
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 jasongorman/9383f98e142d488cb1b19b6b757e60dd to your computer and use it in GitHub Desktop.
Save jasongorman/9383f98e142d488cb1b19b6b757e60dd to your computer and use it in GitHub Desktop.
function match(conditions) {
for (let i = 0; i < conditions.length; i++) {
if (conditions[i][0]()) {
return conditions[i][1]();
}
}
return undefined;
}
function postage(order) {
return match([
[() => order.location === 'UK',
() => match([
[() => totalPrice(order) >= 100, () => 0],
[() => totalWeight(order) < 1, () => 2.99],
[() => totalWeight(order) >= 1 && totalWeight(order) < 3, () => 4.99],
[() => totalWeight(order) >= 3, () => 6.99]
])
],
[() => order.location === 'EU',
() => match([
[() => totalWeight(order) < 1, () => 4.99],
[() => totalWeight(order) >= 1 && totalWeight(order) < 3, () => 6.99],
[() => totalWeight(order) >= 3, () => 8.99]
])
]
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment