From 28a559727fcd368258dbe38b602d2c3d13d7abd4 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 16 Jan 2023 22:50:25 +0100 Subject: [PATCH] Fixes table sort for file size (#86) * Fixed algorithm to de-humanize size string to bytes * Resolved linting issues --- src/components/torrent/TorrentSearchResults.vue | 4 +++- src/components/torrent/TorrentTable.vue | 8 ++++---- src/utils.ts | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/components/torrent/TorrentSearchResults.vue b/src/components/torrent/TorrentSearchResults.vue index 2fad3c1..8b40ebb 100644 --- a/src/components/torrent/TorrentSearchResults.vue +++ b/src/components/torrent/TorrentSearchResults.vue @@ -20,7 +20,7 @@ diff --git a/src/components/torrent/TorrentTable.vue b/src/components/torrent/TorrentTable.vue index 1c5b33c..479cce4 100644 --- a/src/components/torrent/TorrentTable.vue +++ b/src/components/torrent/TorrentTable.vue @@ -131,12 +131,12 @@ function sortSize() { const torrentsCopy = [...torrents.value]; if (direction.value) { - torrents.value = torrentsCopy.sort( - (a, b) => sortableSize(a.size) - sortableSize(b.size) + torrents.value = torrentsCopy.sort((a, b) => + sortableSize(a.size) > sortableSize(b.size) ? 1 : -1 ); } else { - torrents.value = torrentsCopy.sort( - (a, b) => sortableSize(b.size) - sortableSize(a.size) + torrents.value = torrentsCopy.sort((a, b) => + sortableSize(a.size) < sortableSize(b.size) ? 1 : -1 ); } } diff --git a/src/utils.ts b/src/utils.ts index 212a2fd..19ec87c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,11 @@ export const sortableSize = (string: string): number => { - const UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; const [numStr, unit] = string.split(" "); if (UNITS.indexOf(unit) === -1) return null; - const exponent = UNITS.indexOf(unit) * 3; - return Number(numStr) * exponent ** 10; + const exponent = UNITS.indexOf(unit) * 3 + 4; + return Math.floor(Number(numStr) * 10 ** exponent); }; export const parseJwt = (token: string) => {