Bookmark this: tool.io/cost/road-trip/source-code
Road Trip Cost Calculator Source Code
Browse the implementation files for Road Trip Cost Calculator.tool.ts typescript
import {
CURRENCY_OPTIONS,
formatCostSummary,
formatMoney,
parseCurrency,
parseNonnegative,
parsePositive
} from '../calculation';
import { calculateRoadTripCost } from '@tool.io/tools/cost/road-trip';
export const roadTripCost = (
currencyInput: unknown,
systemInput: unknown,
distanceInput: unknown,
efficiencyInput: unknown,
priceInput: unknown,
tollsInput: unknown,
parkingInput: unknown,
travelersInput: unknown
): string => {
const currency = parseCurrency(currencyInput);
if (systemInput !== 'us' && systemInput !== 'metric') return '';
const distance = parseNonnegative(distanceInput);
const efficiency = parsePositive(efficiencyInput);
const price = parseNonnegative(priceInput);
const tolls = parseNonnegative(tollsInput);
const parking = parseNonnegative(parkingInput);
const travelers = parsePositive(travelersInput);
if (
!currency ||
distance === null ||
efficiency === null ||
price === null ||
tolls === null ||
parking === null ||
travelers === null ||
!Number.isInteger(travelers)
)
return '';
const result = calculateRoadTripCost(
systemInput,
distance,
efficiency,
price,
tolls,
parking,
travelers,
currency
);
if (!result) return '';
return formatCostSummary([
['Fuel cost', formatMoney(result.fuelCost, currency)],
[
'Tolls and parking',
formatMoney(result.tollsAndParking, currency)
],
['Trip total', formatMoney(result.totalCost, currency)],
[
'Cost per traveler',
formatMoney(result.costPerTraveler, currency)
]
]);
};
export const Tool: TypeTool = {
name: 'Road Trip Cost Calculator',
slug: 'cost/road-trip',
intro:
'Estimate fuel, toll, parking, total, and per-person costs for a road trip.',
tool: roadTripCost,
i: [
{
example: 'USD',
label: 'Currency',
options: CURRENCY_OPTIONS,
placeholder: 'Select currency',
type: 'select'
},
{
example: 'us',
label: 'Units',
options: [
{
selected: true,
text: 'US — miles and MPG',
value: 'us'
},
{ text: 'Metric — km and L/100 km', value: 'metric' }
],
placeholder: 'Select units',
type: 'select'
},
{
example: '300',
label: 'Distance',
placeholder: 'Trip distance',
type: 'number'
},
{
example: '30',
label: 'Fuel Economy',
placeholder: 'MPG or L/100 km',
type: 'number'
},
{
example: '3.50',
label: 'Fuel Price',
placeholder: 'Price per gallon or liter',
type: 'number'
},
{
example: '20',
label: 'Tolls',
placeholder: 'Total tolls',
type: 'number'
},
{
example: '10',
label: 'Parking',
placeholder: 'Parking cost',
type: 'number'
},
{
example: '2',
label: 'Travelers',
placeholder: 'Number of travelers',
type: 'number'
}
],
o: [
{
example:
'Fuel cost: $35.00\nTolls and parking: $30.00\nTrip total: $65.00\nCost per traveler: $32.50',
label: 'Road Trip Cost',
placeholder: 'Trip costs will appear here...',
render: 'textarea',
type: 'text'
}
],
alternative: [
{ title: 'Fuel Cost Calculator', url: '/cost/fuel' }
],
cite: [
{
title: 'Road trip - Wikipedia',
url: 'https://en.wikipedia.org/wiki/Road_trip'
}
]
};
export default Tool;
Description
Browse the implementation files for Road Trip Cost Calculator.