Skip to content

Instantly share code, notes, and snippets.

@alexcastrodev
Last active November 3, 2024 19:37
Show Gist options
  • Save alexcastrodev/18b27d1ce267aebb7792a5cf2b60a112 to your computer and use it in GitHub Desktop.
Save alexcastrodev/18b27d1ce267aebb7792a5cf2b60a112 to your computer and use it in GitHub Desktop.
nearest
export function get_nearby_delivery_fees(distance: number) {
if (!Number.isSafeInteger(distance)) {
throw new Error('Invalid distance');
}
const table_price = [
{
until: 6.5,
fee: 12.5
},
{
until: 7.5,
fee: 14.5
},
{
until: 8.5,
fee: 15.5
},
{
until: 9.5,
fee: 16.5
},
{
until: 10.5,
fee: 17.5
},
{
until: 15.5,
fee: 18.5
},
{
until: 25.5,
fee: 21.5
},
{
until: Infinity,
fee: 25
}
];
return table_price.find(price => distance <= price.until);;
}
get_nearby_delivery_fees(7) // { until: 7.5, fee: 14.5 }
get_nearby_delivery_fees(7.5) // { until: 7.5, fee: 14.5 }
get_nearby_delivery_fees(8) // { until: 8.5, fee: 15.5 }
get_nearby_delivery_fees(100) // { until: Infinity, fee: 25 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment