Skip to content

Commit

Permalink
renaming enum-values
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschga committed Sep 1, 2024
1 parent f7eb165 commit da32aa2
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 88 deletions.
4 changes: 2 additions & 2 deletions assets/js/components/BatterySettingsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ export default {
const multipleBatteries = this.battery.length > 1;
const energy = this.fmtWh(
(capacity / 100) * soc * 1e3,
POWER_UNIT.POWER_KW,
POWER_UNIT.KW,
!multipleBatteries,
1
);
const total = this.fmtWh(capacity * 1e3, POWER_UNIT.POWER_KW, true, 1);
const total = this.fmtWh(capacity * 1e3, POWER_UNIT.KW, true, 1);
const name = multipleBatteries ? "" : "";
const formattedSoc = multipleBatteries ? ` (${this.fmtSoc(soc)})` : "";
const formattedEnergy = this.$t("batterySettings.capacity", {
Expand Down
6 changes: 3 additions & 3 deletions assets/js/components/ChargingSessionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
fmtWh(
chargedEnergy,
chargedEnergy >= 1e3
? POWER_UNIT.POWER_KW
: POWER_UNIT.POWER_AUTO
? POWER_UNIT.KW
: POWER_UNIT.AUTO
)
}}
<div v-if="session.chargeDuration">
Expand All @@ -88,7 +88,7 @@
</th>
<td>
{{ fmtPercentage(session.solarPercentage, 1) }}
({{ fmtWh(solarEnergy, POWER_UNIT.POWER_AUTO) }})
({{ fmtWh(solarEnergy, POWER_UNIT.AUTO) }})
</td>
</tr>
<tr v-if="session.price != null">
Expand Down
6 changes: 3 additions & 3 deletions assets/js/components/Energyflow/Energyflow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ export default {
powerUnit: function () {
const watt = Math.max(this.gridImport, this.selfPv, this.selfBattery, this.pvExport);
if (watt >= 1_000_000) {
return POWER_UNIT.POWER_MW;
return POWER_UNIT.MW;
} else if (watt >= 1000) {
return POWER_UNIT.POWER_KW;
return POWER_UNIT.KW;
} else {
return POWER_UNIT.POWER_W;
return POWER_UNIT.W;
}
},
inPower: function () {
Expand Down
4 changes: 2 additions & 2 deletions assets/js/components/Energyflow/Visualization.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
/>
</div>
<div v-if="totalAdjusted <= 0" class="site-progress-bar w-100 grid-import">
<span>{{ fmtW(0, POWER_UNIT.POWER_AUTO, true) }}</span>
<span>{{ fmtW(0, POWER_UNIT.AUTO, true) }}</span>
</div>
</div>
<div class="label-scale d-flex">
Expand Down Expand Up @@ -112,7 +112,7 @@ export default {
pvProduction: { type: Number, default: 0 },
homePower: { type: Number, default: 0 },
batterySoc: { type: Number, default: 0 },
powerUnit: { type: String, default: POWER_UNIT.POWER_KW },
powerUnit: { type: String, default: POWER_UNIT.KW },
},
data: function () {
return { width: 0 };
Expand Down
4 changes: 2 additions & 2 deletions assets/js/components/Loadpoint.vue
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ export default {
},
fmtPower(value) {
const inKw = value == 0 || value >= 1000;
return this.fmtW(value, inKw ? POWER_UNIT.POWER_KW : POWER_UNIT.POWER_AUTO);
return this.fmtW(value, inKw ? POWER_UNIT.KW : POWER_UNIT.AUTO);
},
fmtEnergy(value) {
const inKw = value == 0 || value >= 1000;
return this.fmtWh(value, inKw ? POWER_UNIT.POWER_KW : POWER_UNIT.POWER_AUTO);
return this.fmtWh(value, inKw ? POWER_UNIT.KW : POWER_UNIT.AUTO);
},
openSettingsModal() {
const modal = Modal.getOrCreateInstance(
Expand Down
4 changes: 2 additions & 2 deletions assets/js/components/Savings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
$t('footer.savings.percentSelf', {
self: fmtW(
solarCharged * 1000,
POWER_UNIT.POWER_KW,
POWER_UNIT.KW,
false,
0
),
Expand All @@ -85,7 +85,7 @@
$t('footer.savings.percentGrid', {
grid: fmtW(
gridCharged * 1000,
POWER_UNIT.POWER_KW,
POWER_UNIT.KW,
false,
0
),
Expand Down
2 changes: 1 addition & 1 deletion assets/js/components/Vehicle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export default {
this.$emit("remove-vehicle");
},
fmtEnergy(value) {
return this.fmtWh(value, value == 0 ? POWER_UNIT.POWER_KW : POWER_UNIT.POWER_AUTO);
return this.fmtWh(value, value == 0 ? POWER_UNIT.KW : POWER_UNIT.AUTO);
},
openPlanModal() {
this.$refs.chargingPlan.openPlanModal();
Expand Down
28 changes: 14 additions & 14 deletions assets/js/mixins/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const ENERGY_PRICE_IN_SUBUNIT = {
};

export const POWER_UNIT = Object.freeze({
POWER_W: "W",
POWER_KW: "kW",
POWER_MW: "MW",
POWER_AUTO: "",
W: "W",
KW: "kW",
MW: "MW",
AUTO: "",
});

export default {
Expand All @@ -41,34 +41,34 @@ export default {
val = Math.abs(val);
return val >= this.fmtLimit ? this.round(val / 1e3, this.fmtDigits) : this.round(val, 0);
},
fmtW: function (watt = 0, format = POWER_UNIT.POWER_KW, withUnit = true, digits) {
let unit = POWER_UNIT.POWER_KW;
if (POWER_UNIT.POWER_AUTO === format) {
fmtW: function (watt = 0, format = POWER_UNIT.KW, withUnit = true, digits) {
let unit = POWER_UNIT.KW;
if (POWER_UNIT.AUTO === format) {
if (watt >= 1_000_000) {
unit = POWER_UNIT.POWER_MW;
unit = POWER_UNIT.MW;
} else if (watt >= 1000) {
unit = POWER_UNIT.POWER_KW;
unit = POWER_UNIT.KW;
} else {
unit = POWER_UNIT.POWER_W;
unit = POWER_UNIT.W;
}
}
let value = watt;
if (POWER_UNIT.POWER_KW === unit) {
if (POWER_UNIT.KW === unit) {
value = watt / 1000;
} else if (POWER_UNIT.POWER_MW === unit) {
} else if (POWER_UNIT.MW === unit) {
value = watt / 1_000_000;
}
let d = digits;
if (digits === undefined) {
d = POWER_UNIT.POWER_KW === unit || POWER_UNIT.POWER_MW === unit ? 1 : 0;
d = POWER_UNIT.KW === unit || POWER_UNIT.MW === unit ? 1 : 0;
}
return `${new Intl.NumberFormat(this.$i18n?.locale, {
style: "decimal",
minimumFractionDigits: d,
maximumFractionDigits: d,
}).format(value)}${withUnit ? ` ${unit}` : ""}`;
},
fmtWh: function (watt, format = POWER_UNIT.POWER_KW, withUnit = true, digits) {
fmtWh: function (watt, format = POWER_UNIT.KW, withUnit = true, digits) {
return this.fmtW(watt, format, withUnit, digits) + (withUnit ? "h" : "");
},
fmtNumber: function (number, decimals, unit) {
Expand Down
112 changes: 56 additions & 56 deletions assets/js/mixins/formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,75 +12,75 @@ const fmt = mount({

describe("fmtW", () => {
test("should format with units", () => {
expect(fmt.fmtW(0, POWER_UNIT.POWER_AUTO)).eq("0 W");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_AUTO)).eq("1,2 MW");
expect(fmt.fmtW(0, POWER_UNIT.POWER_MW)).eq("0,0 MW");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_MW)).eq("1,2 MW");
expect(fmt.fmtW(0, POWER_UNIT.POWER_KW)).eq("0,0 kW");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_KW)).eq("1.200,0 kW");
expect(fmt.fmtW(0, POWER_UNIT.POWER_W)).eq("0 W");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_W)).eq("1.200.000 W");
expect(fmt.fmtW(0, POWER_UNIT.AUTO)).eq("0 W");
expect(fmt.fmtW(1200000, POWER_UNIT.AUTO)).eq("1,2 MW");
expect(fmt.fmtW(0, POWER_UNIT.MW)).eq("0,0 MW");

Check failure on line 17 in assets/js/mixins/formatter.test.js

View workflow job for this annotation

GitHub Actions / UI

js/mixins/formatter.test.js > fmtW > should format with units

AssertionError: expected '0,0 kW' to equal '0,0 MW' Expected: "0,0 MW" Received: "0,0 kW" ❯ js/mixins/formatter.test.js:17:40
expect(fmt.fmtW(1200000, POWER_UNIT.MW)).eq("1,2 MW");
expect(fmt.fmtW(0, POWER_UNIT.KW)).eq("0,0 kW");
expect(fmt.fmtW(1200000, POWER_UNIT.KW)).eq("1.200,0 kW");
expect(fmt.fmtW(0, POWER_UNIT.W)).eq("0 W");
expect(fmt.fmtW(1200000, POWER_UNIT.W)).eq("1.200.000 W");
});
test("should format without units", () => {
expect(fmt.fmtW(0, POWER_UNIT.POWER_AUTO, false)).eq("0");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_AUTO, false)).eq("1,2");
expect(fmt.fmtW(0, POWER_UNIT.POWER_MW, false)).eq("0,0");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_MW, false)).eq("1,2");
expect(fmt.fmtW(0, POWER_UNIT.POWER_KW, false)).eq("0,0");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_KW, false)).eq("1.200,0");
expect(fmt.fmtW(0, POWER_UNIT.POWER_W, false)).eq("0");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_W, false)).eq("1.200.000");
expect(fmt.fmtW(0, POWER_UNIT.AUTO, false)).eq("0");
expect(fmt.fmtW(1200000, POWER_UNIT.AUTO, false)).eq("1,2");
expect(fmt.fmtW(0, POWER_UNIT.MW, false)).eq("0,0");
expect(fmt.fmtW(1200000, POWER_UNIT.MW, false)).eq("1,2");

Check failure on line 28 in assets/js/mixins/formatter.test.js

View workflow job for this annotation

GitHub Actions / UI

js/mixins/formatter.test.js > fmtW > should format without units

AssertionError: expected '1.200,0' to equal '1,2' Expected: "1,2" Received: "1.200,0" ❯ js/mixins/formatter.test.js:28:53
expect(fmt.fmtW(0, POWER_UNIT.KW, false)).eq("0,0");
expect(fmt.fmtW(1200000, POWER_UNIT.KW, false)).eq("1.200,0");
expect(fmt.fmtW(0, POWER_UNIT.W, false)).eq("0");
expect(fmt.fmtW(1200000, POWER_UNIT.W, false)).eq("1.200.000");
});
test("should format a given number of digits", () => {
expect(fmt.fmtW(12345, POWER_UNIT.POWER_AUTO, true, 0)).eq("12 kW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_AUTO, true, 1)).eq("12,3 kW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_AUTO, true, 2)).eq("12,35 kW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_MW, true, 0)).eq("0 MW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_MW, true, 1)).eq("0,0 MW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_MW, true, 2)).eq("0,01 MW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_KW, true, 0)).eq("12 kW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_KW, true, 1)).eq("12,3 kW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_KW, true, 2)).eq("12,35 kW");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_W, true, 0)).eq("12.345 W");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_W, true, 1)).eq("12.345,0 W");
expect(fmt.fmtW(12345, POWER_UNIT.POWER_W, true, 2)).eq("12.345,00 W");
expect(fmt.fmtW(12345, POWER_UNIT.AUTO, true, 0)).eq("12 kW");
expect(fmt.fmtW(12345, POWER_UNIT.AUTO, true, 1)).eq("12,3 kW");
expect(fmt.fmtW(12345, POWER_UNIT.AUTO, true, 2)).eq("12,35 kW");
expect(fmt.fmtW(12345, POWER_UNIT.MW, true, 0)).eq("0 MW");

Check failure on line 38 in assets/js/mixins/formatter.test.js

View workflow job for this annotation

GitHub Actions / UI

js/mixins/formatter.test.js > fmtW > should format a given number of digits

AssertionError: expected '12 kW' to equal '0 MW' Expected: "0 MW" Received: "12 kW" ❯ js/mixins/formatter.test.js:38:53
expect(fmt.fmtW(12345, POWER_UNIT.MW, true, 1)).eq("0,0 MW");
expect(fmt.fmtW(12345, POWER_UNIT.MW, true, 2)).eq("0,01 MW");
expect(fmt.fmtW(12345, POWER_UNIT.KW, true, 0)).eq("12 kW");
expect(fmt.fmtW(12345, POWER_UNIT.KW, true, 1)).eq("12,3 kW");
expect(fmt.fmtW(12345, POWER_UNIT.KW, true, 2)).eq("12,35 kW");
expect(fmt.fmtW(12345, POWER_UNIT.W, true, 0)).eq("12.345 W");
expect(fmt.fmtW(12345, POWER_UNIT.W, true, 1)).eq("12.345,0 W");
expect(fmt.fmtW(12345, POWER_UNIT.W, true, 2)).eq("12.345,00 W");
});
});

describe("fmtWh", () => {
test("should format with units", () => {
expect(fmt.fmtWh(0, POWER_UNIT.POWER_AUTO)).eq("0 Wh");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_AUTO)).eq("1,2 MWh");
expect(fmt.fmtWh(0, POWER_UNIT.POWER_MW)).eq("0,0 MWh");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_MW)).eq("1,2 MWh");
expect(fmt.fmtWh(0, POWER_UNIT.POWER_KW)).eq("0,0 kWh");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_KW)).eq("1.200,0 kWh");
expect(fmt.fmtWh(0, POWER_UNIT.POWER_W)).eq("0 Wh");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_W)).eq("1.200.000 Wh");
expect(fmt.fmtWh(0, POWER_UNIT.AUTO)).eq("0 Wh");
expect(fmt.fmtWh(1200000, POWER_UNIT.AUTO)).eq("1,2 MWh");
expect(fmt.fmtWh(0, POWER_UNIT.MW)).eq("0,0 MWh");

Check failure on line 54 in assets/js/mixins/formatter.test.js

View workflow job for this annotation

GitHub Actions / UI

js/mixins/formatter.test.js > fmtWh > should format with units

AssertionError: expected '0,0 kWh' to equal '0,0 MWh' Expected: "0,0 MWh" Received: "0,0 kWh" ❯ js/mixins/formatter.test.js:54:41
expect(fmt.fmtWh(1200000, POWER_UNIT.MW)).eq("1,2 MWh");
expect(fmt.fmtWh(0, POWER_UNIT.KW)).eq("0,0 kWh");
expect(fmt.fmtWh(1200000, POWER_UNIT.KW)).eq("1.200,0 kWh");
expect(fmt.fmtWh(0, POWER_UNIT.W)).eq("0 Wh");
expect(fmt.fmtWh(1200000, POWER_UNIT.W)).eq("1.200.000 Wh");
});
test("should format without units", () => {
expect(fmt.fmtW(0, POWER_UNIT.POWER_AUTO, false)).eq("0");
expect(fmt.fmtW(1200000, POWER_UNIT.POWER_AUTO, false)).eq("1,2");
expect(fmt.fmtWh(0, POWER_UNIT.POWER_MW, false)).eq("0,0");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_MW, false)).eq("1,2");
expect(fmt.fmtWh(0, POWER_UNIT.POWER_KW, false)).eq("0,0");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_KW, false)).eq("1.200,0");
expect(fmt.fmtWh(0, POWER_UNIT.POWER_W, false)).eq("0");
expect(fmt.fmtWh(1200000, POWER_UNIT.POWER_W, false)).eq("1.200.000");
expect(fmt.fmtW(0, POWER_UNIT.AUTO, false)).eq("0");
expect(fmt.fmtW(1200000, POWER_UNIT.AUTO, false)).eq("1,2");
expect(fmt.fmtWh(0, POWER_UNIT.MW, false)).eq("0,0");
expect(fmt.fmtWh(1200000, POWER_UNIT.MW, false)).eq("1,2");

Check failure on line 65 in assets/js/mixins/formatter.test.js

View workflow job for this annotation

GitHub Actions / UI

js/mixins/formatter.test.js > fmtWh > should format without units

AssertionError: expected '1.200,0' to equal '1,2' Expected: "1,2" Received: "1.200,0" ❯ js/mixins/formatter.test.js:65:54
expect(fmt.fmtWh(0, POWER_UNIT.KW, false)).eq("0,0");
expect(fmt.fmtWh(1200000, POWER_UNIT.KW, false)).eq("1.200,0");
expect(fmt.fmtWh(0, POWER_UNIT.W, false)).eq("0");
expect(fmt.fmtWh(1200000, POWER_UNIT.W, false)).eq("1.200.000");
});
test("should format a given number of digits", () => {
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_AUTO, true, 0)).eq("12 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_AUTO, true, 1)).eq("12,3 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_AUTO, true, 2)).eq("12,35 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_MW, true, 0)).eq("0 MWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_MW, true, 1)).eq("0,0 MWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_MW, true, 2)).eq("0,01 MWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_KW, true, 0)).eq("12 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_KW, true, 1)).eq("12,3 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_KW, true, 2)).eq("12,35 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_W, true, 0)).eq("12.345 Wh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_W, true, 1)).eq("12.345,0 Wh");
expect(fmt.fmtWh(12345, POWER_UNIT.POWER_W, true, 2)).eq("12.345,00 Wh");
expect(fmt.fmtWh(12345, POWER_UNIT.AUTO, true, 0)).eq("12 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.AUTO, true, 1)).eq("12,3 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.AUTO, true, 2)).eq("12,35 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.MW, true, 0)).eq("0 MWh");

Check failure on line 75 in assets/js/mixins/formatter.test.js

View workflow job for this annotation

GitHub Actions / UI

js/mixins/formatter.test.js > fmtWh > should format a given number of digits

AssertionError: expected '12 kWh' to equal '0 MWh' Expected: "0 MWh" Received: "12 kWh" ❯ js/mixins/formatter.test.js:75:54
expect(fmt.fmtWh(12345, POWER_UNIT.MW, true, 1)).eq("0,0 MWh");
expect(fmt.fmtWh(12345, POWER_UNIT.MW, true, 2)).eq("0,01 MWh");
expect(fmt.fmtWh(12345, POWER_UNIT.KW, true, 0)).eq("12 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.KW, true, 1)).eq("12,3 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.KW, true, 2)).eq("12,35 kWh");
expect(fmt.fmtWh(12345, POWER_UNIT.W, true, 0)).eq("12.345 Wh");
expect(fmt.fmtWh(12345, POWER_UNIT.W, true, 1)).eq("12.345,0 Wh");
expect(fmt.fmtWh(12345, POWER_UNIT.W, true, 2)).eq("12.345,00 Wh");
});
});

Expand Down
2 changes: 1 addition & 1 deletion assets/js/utils/energyOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function fmtEnergy(energy, step, fmtWh, zeroText) {
}
const inKWh = step >= 0.1;
const digits = inKWh && step < 1 ? 1 : 0;
return fmtWh(energy * 1e3, inKWh ? POWER_UNIT.POWER_KW : POWER_UNIT.POWER_W, true, digits);
return fmtWh(energy * 1e3, inKWh ? POWER_UNIT.KW : POWER_UNIT.W, true, digits);
}

export function estimatedSoc(energy, socPerKwh) {
Expand Down
4 changes: 2 additions & 2 deletions assets/js/views/ChargingSessions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export default {
unit: "kWh",
total: this.chargedEnergy,
value: (session) => session.chargedEnergy,
format: (value) => this.fmtWh(value * 1e3, POWER_UNIT.POWER_KW, false),
format: (value) => this.fmtWh(value * 1e3, POWER_UNIT.KW, false),
},
{
name: "solar",
Expand Down Expand Up @@ -347,7 +347,7 @@ export default {
return null;
},
format: (value) =>
value ? this.fmtW(value * 1e3, POWER_UNIT.POWER_KW, false, 1) : undefined,
value ? this.fmtW(value * 1e3, POWER_UNIT.KW, false, 1) : undefined,
},
];
// only columns with values are shown
Expand Down

0 comments on commit da32aa2

Please sign in to comment.