Skip to content

Commit 9831a29

Browse files
committed
Extract formatPercentChange
1 parent 40e860a commit 9831a29

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

site/frontend/src/pages/compare/artifact-size/artifact-size-table.vue

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<script setup lang="ts">
22
import {ArtifactDescription} from "../types";
3-
import {diffClass, formatSize} from "../shared";
3+
import {
4+
diffClass,
5+
formatPercentChange,
6+
formatSize,
7+
isValidValue,
8+
} from "../shared";
9+
import Tooltip from "../tooltip.vue";
410
511
const props = defineProps<{
612
a: ArtifactDescription;
@@ -31,14 +37,14 @@ function formatName(component: string): string {
3137
}
3238
3339
function formatValue(value: number | undefined): string {
34-
if (value === undefined) {
40+
if (!isValidValue(value)) {
3541
return "-";
3642
}
3743
return formatSize(value);
3844
}
3945
4046
function formatChange(a: number | undefined, b: number | undefined): string {
41-
if (a === undefined || b === undefined) {
47+
if (!isValidValue(a) || !isValidValue(b)) {
4248
return "-";
4349
}
4450
const diff = b - a;
@@ -49,18 +55,8 @@ function formatChange(a: number | undefined, b: number | undefined): string {
4955
return formatted;
5056
}
5157
52-
function formatPercentChange(
53-
a: number | undefined,
54-
b: number | undefined
55-
): string {
56-
if (a === undefined || b === undefined) {
57-
return "-";
58-
}
59-
return `${(((b - a) / a) * 100).toFixed(3)}%`;
60-
}
61-
6258
function getClass(a: number | undefined, b: number | undefined): string {
63-
if (a === undefined || b === undefined || a == b) {
59+
if (!isValidValue(a) || !isValidValue(b) || a == b) {
6460
return "";
6561
}
6662
return diffClass(b - a);

site/frontend/src/pages/compare/shared.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,20 @@ export function formatSize(size: number): string {
5050
}
5151
return `${size} B`;
5252
}
53+
54+
// Formats a percentual change between two values
55+
export function formatPercentChange(
56+
before: number | undefined,
57+
after: number | undefined,
58+
invalidPlaceholder: string = "-"
59+
): string {
60+
if (!isValidValue(before) || !isValidValue(after)) {
61+
return invalidPlaceholder;
62+
}
63+
return `${(((after - before) / before) * 100).toFixed(3)}%`;
64+
}
65+
66+
// Checks if the value is not undefined and not zero
67+
export function isValidValue(size: number | undefined): boolean {
68+
return size !== undefined && size !== 0;
69+
}

site/frontend/src/pages/compare/tabs.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<script setup lang="tsx">
22
import {computed, h, ref, Ref} from "vue";
33
import {CompareResponse, Tab} from "./types";
4-
import {diffClass, formatSize, percentClass} from "./shared";
4+
import {
5+
diffClass,
6+
formatPercentChange,
7+
formatSize,
8+
percentClass,
9+
} from "./shared";
510
import {SummaryGroup} from "./data";
611
import SummaryPercentValue from "./summary/percent-value.vue";
712
import SummaryRange from "./summary/range.vue";
@@ -156,7 +161,7 @@ const activeTab: Ref<Tab> = ref(props.initialTab);
156161
>
157162
{{ totalSizeB < totalSizeA ? "-" : ""
158163
}}{{ formatSize(Math.abs(totalSizeB - totalSizeA)) }} ({{
159-
(((totalSizeB - totalSizeA) / totalSizeA) * 100).toFixed(3)
164+
formatPercentChange(totalSizeA, totalSizeB)
160165
}}%)
161166
</div>
162167
</div>

0 commit comments

Comments
 (0)