diff --git a/docker-compose.yml b/docker-compose.yml index 54359e6..c553725 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ services: web: build: . - image: umrechner:v1.0.3 + image: umrechner:v1.0.4 restart: unless-stopped environment: - POCKETBASE_URL={$POCKETBASE_URL} diff --git a/hugo/layouts/conversion/single.html b/hugo/layouts/conversion/single.html index ba832e3..63d4586 100644 --- a/hugo/layouts/conversion/single.html +++ b/hugo/layouts/conversion/single.html @@ -1,17 +1,21 @@ {{ define "main" }} + {{ $catData := index hugo.Data .Params.category }} {{ $config := "" }} {{ if eq .Params.engine "linear" }} {{ $config = dict "fromFactor" .Params.from_factor "toFactor" .Params.to_factor | jsonify }} {{ else if eq .Params.engine "intermediate" }} - {{ $config = dict "fromUnit" .Params.from_unit - "toUnit" .Params.to_unit | jsonify }} + {{ $toExpr := index $catData.to_intermediate + .Params.from | default "v" }} + {{ $fromExpr := index $catData.from_intermediate + .Params.to | default "v" }} + {{ $config = printf + "{ toIntermediate: v => %s, fromIntermediate: v => %s }" + $toExpr $fromExpr }} {{ else if eq .Params.engine "runtime" }} {{ $config = dict "fromCurrency" .Params.from "toCurrency" .Params.to | jsonify }} {{ end }} - - {{ $catData := index hugo.Data .Params.category }} {{ $availableUnits := partial "available-units.html" (dict "category" .Params.category "units" $catData.units) }} diff --git a/hugo/static/js/converter.js b/hugo/static/js/converter.js index 1c476b9..cee4e4f 100644 --- a/hugo/static/js/converter.js +++ b/hugo/static/js/converter.js @@ -152,11 +152,10 @@ function createConverter(engine, config) { rawResult = value.times(fromFactor) .dividedBy(toFactor); } else if (engine === 'intermediate') { - rawResult = convertTemperature( - value, - config.fromUnit, - config.toUnit - ); + let v = value.toNumber(); + v = config.toIntermediate(v); + v = config.fromIntermediate(v); + rawResult = new Decimal(v); } else { this.result = ''; return; @@ -176,36 +175,6 @@ function createConverter(engine, config) { }; } -/** - * Temperature conversion via Celsius intermediate. - * @param {Decimal} value - * @param {string} fromUnit - * @param {string} toUnit - * @returns {number} - */ -function convertTemperature(value, fromUnit, toUnit) { - let v = value.toNumber(); - if (fromUnit === 'fahrenheit') { - v = (v - 32) * 5 / 9; - } else if (fromUnit === 'kelvin') { - v = v - 273.15; - } else if (fromUnit === 'rankine') { - v = (v - 491.67) * 5 / 9; - } else if (fromUnit === 'reaumur') { - v = v * 5 / 4; - } - if (toUnit === 'fahrenheit') { - v = v * 9 / 5 + 32; - } else if (toUnit === 'kelvin') { - v = v + 273.15; - } else if (toUnit === 'rankine') { - v = (v + 273.15) * 9 / 5; - } else if (toUnit === 'reaumur') { - v = v * 4 / 5; - } - return v; -} - /** * Formats a number as plain digits with dot as decimal separator. * @param {Decimal|string|number} num