sussy-util
Version:
Util package made by me
410 lines (409 loc) • 13.9 kB
JavaScript
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
class UnitConverter {
/**
* This function takes a number and adds 273.15 to it.
* @param {number} val - number - The value to convert
* @returns The value of the variable val plus the value of the constant KELVIN_TO_CELSIUS_OFFSET.
*/
static kelvinToCelsius(val) {
return Number(val) + this.KELVIN_TO_CELSIUS_OFFSET;
}
/**
* Convert a temperature in Celsius to Kelvin.
* @param {number} val - number - The value to convert.
* @returns The value of the argument minus 273.15
*/
static celsiusToKelvin(val) {
return Number(val) - this.KELVIN_TO_CELSIUS_OFFSET;
}
/**
* Convert a temperature in Fahrenheit to Kelvin by first converting it to Celsius and then to
* Kelvin.
* @param {number} val - number - The value to convert
* @returns The value of the function call.
*/
static fahrenheitToKelvin(val) {
return this.celsiusToKelvin(this.fahrenheitToCelsius(val));
}
/**
* Convert a temperature in Kelvin to Fahrenheit by first converting it to Celsius and then to
* Fahrenheit.
* @param {number} val - number - The value to convert
* @returns The value of the function call.
*/
static kelvinToFahrenheit(val) {
return this.celsiusToFahrenheit(this.kelvinToCelsius(val));
}
/**
* Convert a temperature in Celsius to Rankine.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 9/5.
*/
static celsiusToRankine(val) {
return val * (9 / 5);
}
/**
* Convert a temperature in Celsius to Delisle.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by -3/2 and added to 100.
*/
static celsiusToDelisle(val) {
return (val * -3) / 2 + 100;
}
/**
* Convert a temperature in Fahrenheit to Rankine.
* @param {number} val - The value to convert.
* @returns The value of the argument plus 459.67.
*/
static fahrenheitToRankine(val) {
return val + 459.67;
}
/**
* Convert a temperature in Fahrenheit to Delisle.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by -3/2 and added to 212.
*/
static fahrenheitToDelisle(val) {
return (val * -3) / 2 + 212;
}
/**
* Convert a temperature in Kelvin to Rankine.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 9/5.
*/
static kelvinToRankine(val) {
return val * (9 / 5);
}
/**
* Convert a temperature in Kelvin to Delisle.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 273.15, multiplied by -3/2, and added to 373.15.
*/
static kelvinToDelisle(val) {
return ((val - 273.15) * -3) / 2 + 373.15;
}
/**
* Convert a temperature in Rankine to Celsius.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 459.67.
*/
static rankineToCelsius(val) {
return val - 459.67;
}
/**
* Convert a temperature in Rankine to Fahrenheit.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 459.67.
*/
static rankineToFahrenheit(val) {
return val - 459.67;
}
/**
* Convert a temperature in Rankine to Kelvin.
* @param {number} val - The value to convert.
* @returns The value of the argument divided by 1.8.
*/
static rankineToKelvin(val) {
return val / 1.8;
}
/**
* Convert a temperature in Rankine to Delisle.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 491.67, multiplied by -3/2, and added to 100.
*/
static rankineToDelisle(val) {
return ((val - 491.67) * -3) / 2 + 100;
}
/**
* Convert a temperature in Delisle to Celsius.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 100, multiplied by -2/3.
*/
static delisleToCelsius(val) {
return ((val - 100) * -2) / 3;
}
/**
* Convert a temperature in Delisle to Fahrenheit.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 100, multiplied by -2/3, and added to 212.
*/
static delisleToFahrenheit(val) {
return ((val - 100) * -2) / 3 + 212;
}
/**
* Convert a temperature in Delisle to Kelvin.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 100, multiplied by -2/3, and added to 373.15.
*/
static delisleToKelvin(val) {
return ((val - 100) * -2) / 3 + 373.15;
}
/**
* Convert a temperature in Delisle to Rankine.
* @param {number} val - The value to convert.
* @returns The value of the argument minus 100, multiplied by -2/3, and added to 491.67.
*/
static delisleToRankine(val) {
return ((val - 100) * -2) / 3 + 491.67;
}
/**
* Convert kilometers to miles.
* @param {number} kilometers - number
* @returns The number of miles in the given number of kilometers.
*/
static kilometersToMiles(kilometers) {
return kilometers / this.KILOMETERS_PER_MILE;
}
/**
* Converts miles to kilometers.
* @param {number} miles - number - The number of miles to convert to kilometers.
* @returns The number of kilometers per mile.
*/
static milesToKilometers(miles) {
return miles * this.KILOMETERS_PER_MILE;
}
/**
* Convert a temperature in Celsius to Fahrenheit.
* @param {number} celsius - number - The temperature in celsius
* @returns The temperature in Fahrenheit.
*/
static celsiusToFahrenheit(celsius) {
return (celsius * 9) / 5 + this.FAHRENHEIT_TO_CELSIUS_OFFSET;
}
/**
* Convert a temperature in Fahrenheit to Celsius.
* @param {number} fahrenheit - number - The temperature in Fahrenheit
* @returns The return value is the result of the calculation.
*/
static fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - this.FAHRENHEIT_TO_CELSIUS_OFFSET) * this.FAHRENHEIT_TO_CELSIUS_FACTOR;
}
/**
* Convert a length in Yards to Miles.
* @param {number} val - The value to convert.
* @returns The value of the argument divided by 1760.
*/
static yardsToMiles(val) {
return val / 1760;
}
/**
* Convert a length in Yards to Kilometers.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 0.0009144.
*/
static yardsToKilometers(val) {
return val * 0.0009144;
}
/**
* Convert a length in Yards to Feet.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 3.
*/
static yardsToFeet(val) {
return val * 3;
}
/**
* Convert a length in Feet to Miles.
* @param {number} val - The value to convert.
* @returns The value of the argument divided by 5280.
*/
static feetToMiles(val) {
return val / 5280;
}
/**
* Convert a length in Feet to Kilometers.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 0.0003048.
*/
static feetToKilometers(val) {
return val * 0.0003048;
}
/**
* Convert a length in Feet to Yards.
* @param {number} val - The value to convert.
* @returns The value of the argument divided by 3.
*/
static feetToYards(val) {
return val / 3;
}
/**
* If the conversion is not possible, throws an error, otherwise, returns the result of the
* conversion.
*
* @param {number} value - number - The value to convert
* @param {UnitType} from - UnitType
* @param {UnitType} to - UnitType - The unit type to convert to
* @returns The return value is the result of the conversion
*/
static convertLenght(value, from, to) {
if (!this.LENGTH_MAP[from] || !this.LENGTH_MAP[from][to]) {
throw new TypeError('Unknown conversion type.');
}
return this.LENGTH_MAP[from][to](value);
}
/**
* Convert a length in Kilometers to Yards.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 1094.
*/
static kilometersToYards(val) {
return val * 1094;
}
/**
* Convert a length in Kilometers to Feet.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 3281.
*/
static kilometersToFeet(val) {
return val * 3281;
}
/**
* Convert a length in Miles to Yards.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 1760.
*/
static milesToYards(val) {
return val * 1760;
}
/**
* Convert a length in Miles to Feet.
* @param {number} val - The value to convert.
* @returns The value of the argument multiplied by 5280.
*/
static milesToFeet(val) {
return val * 5280;
}
/**
* If the conversion is not possible, throws an error, otherwise, returns the result of the
* conversion.
*
* @param {number} value - number - The value to convert
* @param {UnitType} from - UnitType
* @param {UnitType} to - UnitType - The unit type to convert to
* @returns The return value is the result of the conversion
*/
static convertTemperature(value, from, to) {
if (!this.TEMPERATURE_MAP[from] || !this.TEMPERATURE_MAP[from][to]) {
throw new TypeError('Unknown conversion type.');
}
return this.TEMPERATURE_MAP[from][to](value);
}
/**
* If the conversion is not possible, throws an error, otherwise, returns the result of the
* conversion.
*
* @param {number} value - number - The value to convert
* @param {UnitType} from - UnitType
* @param {UnitType} to - UnitType - The unit type to convert to
* @returns The return value is the result of the conversion
*/
static convertSpeed(value, from, to) {
if (!this.SPEED_MAP[from] || !this.SPEED_MAP[from][to]) {
throw new Error(`Conversion from ${from} to ${to} is not supported.`);
}
return this.SPEED_MAP[from][to](value);
}
/**
* Converts a speed from kilometers per hour to miles per hour.
* @param {number} kmPerHour - The speed in kilometers per hour.
* @returns {number} The speed in miles per hour.
*/
static kilometersPerHourToMilesPerHour(kmPerHour) {
return kmPerHour / this.KILOMETERS_PER_MILE;
}
/**
* Converts a speed from miles per hour to kilometers per hour.
* @param {number} mph - The speed in miles per hour.
* @returns {number} The speed in kilometers per hour.
*/
static milesPerHourToKilometersPerHour(mph) {
return mph * this.KILOMETERS_PER_MILE;
}
/**
* Converts a weight from kilograms to pounds.
* @param {number} kilograms - The weight in kilograms.
* @returns {number} The weight in pounds.
*/
static kilogramsToPounds(kilograms) {
return kilograms * this.POUNDS_TO_KILOGRAMS;
}
/**
* Converts a weight from pounds to kilograms.
* @param {number} pounds - The weight in pounds.
* @returns {number} The weight in kilograms.
*/
static poundsToKilograms(pounds) {
return pounds / this.POUNDS_TO_KILOGRAMS;
}
}
_a = UnitConverter;
UnitConverter.KILOMETERS_PER_MILE = 1.60934;
UnitConverter.FAHRENHEIT_TO_CELSIUS_OFFSET = 32;
UnitConverter.KELVIN_TO_CELSIUS_OFFSET = 273.15;
UnitConverter.FAHRENHEIT_TO_CELSIUS_FACTOR = 5 / 9;
UnitConverter.POUNDS_TO_KILOGRAMS = 2.20462;
UnitConverter.TEMPERATURE_MAP = {
Celsius: {
Kelvin: _a.celsiusToKelvin,
Fahrenheit: _a.celsiusToFahrenheit,
Rankine: _a.celsiusToRankine,
Delisle: _a.celsiusToDelisle,
},
Fahrenheit: {
Celsius: _a.fahrenheitToCelsius,
Kelvin: _a.fahrenheitToKelvin,
Rankine: _a.fahrenheitToRankine,
Delisle: _a.fahrenheitToDelisle,
},
Kelvin: {
Celsius: _a.kelvinToCelsius,
Fahrenheit: _a.kelvinToFahrenheit,
Rankine: _a.kelvinToRankine,
Delisle: _a.kelvinToDelisle,
},
Rankine: {
Celsius: _a.rankineToCelsius,
Fahrenheit: _a.rankineToFahrenheit,
Kelvin: _a.rankineToKelvin,
Delisle: _a.rankineToDelisle,
},
Delisle: {
Celsius: _a.delisleToCelsius,
Fahrenheit: _a.delisleToFahrenheit,
Kelvin: _a.delisleToKelvin,
Rankine: _a.delisleToRankine,
},
};
UnitConverter.LENGTH_MAP = {
Miles: {
Kilometers: _a.milesToKilometers,
Yards: _a.milesToYards,
Feet: _a.milesToFeet,
},
Kilometers: {
Miles: _a.kilometersToMiles,
Yards: _a.kilometersToYards,
Feet: _a.kilometersToFeet,
},
Yards: {
Miles: _a.yardsToMiles,
Kilometers: _a.yardsToKilometers,
Feet: _a.yardsToFeet,
},
Feet: {
Miles: _a.feetToMiles,
Kilometers: _a.feetToKilometers,
Yards: _a.feetToYards,
},
};
UnitConverter.SPEED_MAP = {
KilometersPerHour: {
MilesPerHour: _a.kilometersPerHourToMilesPerHour,
},
MilesPerHour: {
KilometersPerHour: _a.milesPerHourToKilometersPerHour,
},
};
exports.default = UnitConverter;