Skip to content

Commit 52909fc

Browse files
committed
Merge branch 'up/fix/kelvin-limit' into chore/all-my-stuffs
2 parents 57281b6 + 3e95060 commit 52909fc

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/tools/temperature-converter/temperature-converter.vue

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type TemperatureScale = 'kelvin' | 'celsius' | 'fahrenheit' | 'rankine' | 'delis
2222
const units = reactive<
2323
Record<
2424
string | TemperatureScale,
25-
{ title: string; unit: string; ref: number; toKelvin: (v: number) => number; fromKelvin: (v: number) => number }
25+
{ title: string; unit: string; ref: number; toKelvin: (v: number) => number; fromKelvin: (v: number) => number; min?: number; max?: number; maxLimit?: boolean }
2626
>
2727
>({
2828
kelvin: {
@@ -59,6 +59,7 @@ const units = reactive<
5959
ref: 0,
6060
toKelvin: convertDelisleToKelvin,
6161
fromKelvin: convertKelvinToDelisle,
62+
maxLimit: true,
6263
},
6364
newton: {
6465
title: 'Newton',
@@ -86,7 +87,9 @@ const units = reactive<
8687
function update(key: TemperatureScale) {
8788
const { ref: value, toKelvin } = units[key];
8889
89-
const kelvins = toKelvin(value) ?? 0;
90+
let kelvins = toKelvin(value) ?? 0;
91+
92+
kelvins = kelvins < 0 ? 0 : kelvins;
9093
9194
_.chain(units)
9295
.omit(key)
@@ -96,6 +99,18 @@ function update(key: TemperatureScale) {
9699
.value();
97100
}
98101
102+
function setupLimit() {
103+
_.forEach(units, (unit) => {
104+
if (unit.maxLimit) {
105+
unit.max = Math.ceil(unit.fromKelvin(0) * 100) / 100;
106+
}
107+
else {
108+
unit.min = Math.floor(unit.fromKelvin(0) * 100) / 100;
109+
}
110+
});
111+
}
112+
113+
setupLimit();
99114
update('kelvin');
100115
</script>
101116

@@ -109,6 +124,8 @@ update('kelvin');
109124
<n-input-number
110125
v-model:value="units[key].ref"
111126
style="flex: 1"
127+
:min="units[key].min"
128+
:max="units[key].max"
112129
@update:value="() => update(key as TemperatureScale)"
113130
/>
114131

0 commit comments

Comments
 (0)