UNPKG

sussy-util

Version:
147 lines (146 loc) 5.25 kB
"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 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; } /** * 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 convert(value, from, to) { // @ts-ignore this can not happen because the first check already checked for that case if (!this.map[from] || !this.map[from][to]) { throw new TypeError("Unknown conversion type."); } // @ts-ignore can not happen because the if statement before checks for it return this.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 * 2.20462; } /** * 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 / 2.20462; } } _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.map = { Celsius: { Kelvin: _a.celsiusToKelvin, Fahrenheit: _a.celsiusToFahrenheit, }, Fahrenheit: { Celsius: _a.fahrenheitToCelsius, Kelvin: _a.fahrenheitToKelvin }, Kelvin: { Celsius: _a.kelvinToCelsius, Fahrenheit: _a.kelvinToFahrenheit }, Miles: { Kilometers: _a.milesToKilometers, }, Kilometers: { Miles: _a.kilometersToMiles, } }; exports.default = UnitConverter;