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