Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Merge Dynamic count shortening in statistics (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
CST1229 authored Jun 7, 2023
1 parent 90bd602 commit d89cc3b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/lib/LargeCount.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- A component for showing shortened large numbers (like 1k etc). -->
<script>
export let num = 0;
// we don't need negative numbers but why not
$: magnitude = Math.abs(num);
function round(n, decimals) {
return +n.toFixed(decimals);
}
</script>

{#if (isNaN(num) || num === Infinity || num === -Infinity) || magnitude < 1000}
{num}
{:else if magnitude < 1000000}
{round(num / 1000, 2)}k
{:else if magnitude < 1000000000}
{round(num / 1000, 2)}M
{:else}
<!-- how -->
{round(num / 1000, 2)}B
{/if}
7 changes: 5 additions & 2 deletions src/screens/Search.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {apiUrl} from "../lib/urls.js";
import Loading from "../lib/Loading.svelte";
import Container from "../lib/Container.svelte";
import LargeCount from "../lib/LargeCount.svelte";
let errors = "";
</script>
Expand Down Expand Up @@ -153,8 +154,10 @@
<Loading />
</div>
{:then stats}
There are {(stats.posts / 1000).toFixed(2)}k posts, {stats.chats}
chats and {(stats.users / 1000).toFixed(2)}k users on Meower.
There {stats.posts == 1 ? "is" : "are"}
<LargeCount num={stats.posts} /> post{stats.posts == 1 ? "" : "s"},
<LargeCount num={stats.chats} /> chats and
<LargeCount num={stats.users} /> users on Meower.
{/await}
{/await}
</Container>
Expand Down

0 comments on commit d89cc3b

Please sign in to comment.