UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

52 lines (51 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.$Temperature = void 0; const base_1 = require("./base"); const constants_1 = require("./constants"); class $Temperature extends base_1.$BaseConverter { constructor(value, unit) { super(value, unit); } static #toCelsius(value, from) { switch (from) { case 'fahrenheit': return (value - 32) * (5 / 9); case 'kelvin': return value - 273.15; default: return value; } } static #fromCelsius(value, to) { switch (to) { case 'fahrenheit': return value * (9 / 5) + 32; case 'kelvin': return value + 273.15; default: return value; } } to(target) { const celsiusValue = $Temperature.#toCelsius(this.value, this.unit); return $Temperature.#fromCelsius(celsiusValue, target); } toAll() { const result = {}; for (const unit of constants_1.UNITS.temp) { result[unit] = this.to(unit); } return result; } formatTo(target, options) { const value = this.to(target); const shortLabels = { celsius: '°C', fahrenheit: '°F', kelvin: 'K', }; return this.$formatTo(value, target, shortLabels, options); } } exports.$Temperature = $Temperature;