/**
* Converting between different units mostly imperial -> metric and metric -> imperial
* @module
*/
import { unit_conversions } from './constants';
import { roundToDP } from './util';
/**
* Convert from miles to kilometers
* @param {number} quantity
* @param {boolean} equivalence - if true, return value so that 1mile = 1km
* @returns {number} kilometers to 5 decimal places
*/
const milesToKM = (quantity, equivalence = false) => {
let num_value;
if (equivalence) {
// when equivalence is set to true, convert down so that 1mile = 1km
// e.g 100 co2gpm will become co2gpkm to accommodate the smaller
// distance of km vs miles
// eslint-disable-next-line
num_value = kmToMiles(quantity);
} else {
num_value = quantity * unit_conversions.milesToKm;
}
return roundToDP(num_value, 5);
};
/**
* Convert from kilometers to miles
* @param {number} quantity
* @param {boolean} equivalence - if true, return value so that 1km = 1mile
* @returns {number} miles to 5 decimal places
*/
const kmToMiles = (quantity, equivalence = false) => {
let num_value;
if (equivalence) {
// when equivalence is set to true, convert up so that 1km = 1mile
// e.g 150 co2gpkm will become 241.401 co2gpm to accommodate the larger
// distance of km vs miles
num_value = milesToKM(quantity);
} else {
num_value = quantity * (1 / unit_conversions.milesToKm);
}
return roundToDP(num_value, 5);
};
/**
* Convert from miles per gallon to litres per 100km
* @param {number} quantity
* @returns {number} litres per 100km
*/
const mpgToL100km = (quantity) => (1 / quantity) * unit_conversions.mpgToL100km;
/**
* Convert from litres per 100km to miles per gallon
* @param {number} quantity
* @returns {number} miles per gallon
*/
const l100kmToMpg = (quantity) => (1 / quantity) * unit_conversions.mpgToL100km;
/**
* Convert from gallons to litres
* @param {number} quantity
* @returns {number} litres
*/
const gallonsToLitres = (quantity) => quantity * unit_conversions.gallonsToLitres;
/**
* Convert from litres to gallons
* @param {number} quantity
* @returns {number} gallons
*/
const litresToGallons = (quantity, equivalence = false) => {
let num_value;
if (equivalence) {
num_value = gallonsToLitres(quantity);
} else {
num_value = quantity * (1 / unit_conversions.gallonsToLitres);
}
return roundToDP(num_value, 6);
};
/**
* Grams per km to grams per miles
* @param {number} quantity
* @returns {number} gallons
*/
const gramsPerKMtoGramsPerMiles = (quantity) => {
const num_value = quantity / (1 / unit_conversions.milesToKm);
return roundToDP(num_value, 6);
};
const litresToHydrogenKg = (value) => value * unit_conversions.hydrogenKgToLitres;
/**
* Hydrogen kg to litres
* @param {number} value hydrogen in kg
* @returns {number} converted to litres
*/
const hydrogenKgToLitres = (value, equivalence = false) => {
let num_value;
if (equivalence) {
num_value = litresToHydrogenKg(value);
} else {
num_value = value * (1 / unit_conversions.hydrogenKgToLitres);
}
return roundToDP(num_value, 6);
};
export {
milesToKM,
kmToMiles,
mpgToL100km,
l100kmToMpg,
gallonsToLitres,
litresToGallons,
gramsPerKMtoGramsPerMiles,
hydrogenKgToLitres,
};