vehicle/running/costs/components/fuelandelectric/calculate.js

/**
 * Calculate costs of ICE and electric pence per mile
 * @module
 */

import { mpgToL100km, milesToKM } from '../../../../../maths/conversions';
import { requiredKeys } from '../../../../../keys/required';

/**
 *
 * @returns {struct}
*/
const checkRequiredKeys = (required_keys, skv_config) => {
    const rst_required_keys = requiredKeys(skv_config, required_keys);
    // check to see if any keys missing
    if (
        !rst_required_keys.success
        || rst_required_keys.data.length > 0
    ) {
        throw new Error(`Missing required keys: ${rst_required_keys.data} for running.vehicle.cost`);
    }
};

/**
 * where providing mpg and electric user provided values we want to have a ratio
 * vs the official figures to calculate the cost per mile
 * @param {number} user_value user provided mpg or electric range
 * @param {number} official_value official mpg or electric range
 * @returns {number} ratio
 */
const ratio = (user_value, official_value) => user_value / official_value;

/**
 * for user supplied mpg or electric range we want to apply a ratio to the official figures
 * @param {number} cost_ppm
 * @param {number} ratio_value
 * @returns {number}
 */
const applyRatio = (cost_ppm, ratio_value) => cost_ppm * ratio_value;

/** *******************************************
 *                   ICE                     *
 ********************************************* */

/**
 * litres of ICE fuel used per mile
 * @param {number} mpg
 * @param {number} miles
 * @returns {number}
 */
const litresUsedPerMile = (mpg, miles) => {
    const lpk = mpgToL100km(mpg);
    return (lpk / 100) * milesToKM(miles);
};

/**
 * amount ppm for ICE based on per litre used
 * @param {number} fuel_pp_litre
 * @param {number} litres_per_mile
 * @returns {number}
 */
const costForICEPPM = (fuel_pp_litre, litres_per_mile) => litres_per_mile * fuel_pp_litre;

/** *******************************************
 *                   ELECTRIC                  *
 ********************************************* */

/**
 * calculate how much for a full charge of electric based on battery capacity and cost per kwh.
 * The amount of electricity supplied by the chargepoint is calculated from WLTP
 * efficiency and is equivalent to usable capacity plus losses. On some cars this
 * figure happens to be close to Total capacity so we use that as a proxy.
 * @param {number} battery_total_capacity_kwh
 * @param {number} elec_pp_kwh
 * @returns {number}
 */
const costForFullCharge = (battery_total_capacity_kwh, elec_pp_kwh) => battery_total_capacity_kwh * elec_pp_kwh;

/**
 * amount ppm for a full battery charge
 * @param {number} cost_for_full_charge
 * @param {number} battey_range_miles
 * @returns {number}
 */
const costForFullChargePPM = (cost_for_full_charge, battery_range_miles) => cost_for_full_charge / battery_range_miles;

export {
    litresUsedPerMile,
    costForICEPPM,
    costForFullCharge,
    costForFullChargePPM,
    ratio,
    applyRatio,
    checkRequiredKeys,
};