UNPKG

measure-convert

Version:

JS/TS package for managing units of measurement. Convert, add, subtract, multiply, divide, and compare units of measurement.

33 lines (32 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnitTemperature = void 0; // src/units/UnitTemperature.ts const Unit_1 = require("./Unit"); class UnitTemperature extends Unit_1.Unit { constructor(name, symbol, description, coefficient, constant, baseUnitConversionFactor) { super(name, symbol, description, baseUnitConversionFactor); this.coefficient = coefficient; this.constant = constant; this.baseUnitConversionFactor = baseUnitConversionFactor; } convert(value, toUnit) { // Convert input to Kelvin first using the correct formula const valueInKelvin = (value * this.coefficient) + this.constant; // Then convert from Kelvin to the target unit if (toUnit === UnitTemperature.kelvin) { return valueInKelvin; } else if (toUnit === UnitTemperature.celsius) { return valueInKelvin - 273.15; } else if (toUnit === UnitTemperature.fahrenheit) { return (valueInKelvin - 273.15) * 9 / 5 + 32; } throw new Error("Unsupported unit conversion."); } } exports.UnitTemperature = UnitTemperature; UnitTemperature.kelvin = new UnitTemperature("Kelvin", "K", "Absolute temperature scale", 1, 0, 1); UnitTemperature.celsius = new UnitTemperature("Degree Celsius", "°C", "Relative temperature scale based on the freezing and boiling points of water", 1, 273.15, 1); UnitTemperature.fahrenheit = new UnitTemperature("Degree Fahrenheit", "°F", "Relative temperature scale with the freezing point of water at 32°F and boiling point at 212°F", 0.55555555555556, 255.37222222222427, 1);