Skip to content

Commit

Permalink
refactor: format numbers in other file
Browse files Browse the repository at this point in the history
  • Loading branch information
xnought committed Nov 18, 2023
1 parent 6463c2e commit b866ba2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
12 changes: 12 additions & 0 deletions frontend/src/lib/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function numberWithCommas(x: number, round = 0) {
const formatter = new Intl.NumberFormat("en-US");
return formatter.format(+x.toFixed(round));
}

export function formatProteinName(name: string) {
return name.replaceAll(" ", "_");
}

export function humanReadableProteinName(name: string) {
return name.replaceAll("_", " ");
}
13 changes: 8 additions & 5 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { goto } from "$app/navigation";
import { Backend } from "$lib/backend";
import type { ProteinEntry } from "$lib/backend";
import { humanReadableProteinName, numberWithCommas } from "$lib/format";
import {
Table,
TableBody,
Expand Down Expand Up @@ -35,7 +36,7 @@
<Table>
<TableHead>
<TableHeadCell>Protein name</TableHeadCell>
<TableHeadCell>Amino Acids</TableHeadCell>
<TableHeadCell>Length</TableHeadCell>
<TableHeadCell>Mass (Da)</TableHeadCell>
</TableHead>
<TableBody tableBodyClass="divide-y">
Expand All @@ -49,11 +50,11 @@
>
<TableBodyCell
><span class="text-primary-700"
>{entry.name.replaceAll("_", " ")}</span
>{humanReadableProteinName(entry.name)}</span
></TableBodyCell
>
<TableBodyCell>{entry.length}</TableBodyCell>
<TableBodyCell>{entry.mass}</TableBodyCell>
<TableBodyCell>{numberWithCommas(entry.mass)}</TableBodyCell>
</TableBodyRow>
{/each}
{/if}
Expand All @@ -70,10 +71,12 @@
on:click={() => goto(`/protein/${entry.name}`)}
>
<div class="name text-primary-700">
{entry.name.replaceAll("_", " ")}
{humanReadableProteinName(entry.name)}
</div>
<div class="description">
Seq Len: {entry.length}, Mass: {entry.mass}
Length: {entry.length}, Mass (Da): {numberWithCommas(
entry.mass
)}
</div>
</Card>
{/each}
Expand Down
33 changes: 21 additions & 12 deletions frontend/src/routes/protein/[proteinName]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { Button, Card, Spinner } from "flowbite-svelte";
import Markdown from "$lib/Markdown.svelte";
import { Heading, P, Span } from "flowbite-svelte";
import { humanReadableProteinName, numberWithCommas } from "$lib/format";
export let data; // linked to +page.ts return (aka the id)
let entry: ProteinEntry | null = null;
Expand Down Expand Up @@ -54,7 +55,7 @@ Todo: add ways to cite papers here that automatically show up in references.`;
<Span
underline
decorationClass="decoration-8 decoration-primary-400 dark:decoration-primary-600"
>{entry.name.replaceAll("_", " ")}</Span
>{humanReadableProteinName(entry.name)}</Span
>
</Heading>
<P class="mt-4 text-lg">description of the protein here (optional)</P>
Expand All @@ -64,19 +65,27 @@ Todo: add ways to cite papers here that automatically show up in references.`;

<div class="grid grid-cols-2">
<div>Source Organism</div>
<div>Unknown</div>
<div>
<a href="/organism/unknown">unknown organism</a>
</div>

<div>Biological Function</div>
<div>Unknown</div>

<div>Structure From</div>
<div>AlphaFold</div>

<div>Amino Acids Length</div>
<div>{entry.length}</div>

<div>Total Mass</div>
<div>{entry.mass}</div>
<div>
<a href="/function/unknown">unknown function</a>
</div>

<div>Structure</div>
<div>
<a href="https://deepmind.google/technologies/alphafold/"
>AlphaFold</a
>
</div>

<div>Length</div>
<div><code>{entry.length}</code></div>

<div>Mass (Da)</div>
<div><code>{numberWithCommas(entry.mass)}</code></div>
</div>
</Card>

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes/upload/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Backend, UploadError } from "$lib/backend";
import { Fileupload, Button, Input, Label, Helper } from "flowbite-svelte";
import { goto } from "$app/navigation";
import { formatProteinName } from "$lib/format";
let name: string = "";
let files: FileList | undefined; // bind:files on the Fileupload
Expand Down Expand Up @@ -60,7 +61,7 @@
console.log(uploadError);
} else {
// success, so we can go back!
goto(`/protein/${name.replaceAll(" ", "_")}`);
goto(`/protein/${formatProteinName(name)}`);
}
} catch (e) {
console.log(e);
Expand Down

0 comments on commit b866ba2

Please sign in to comment.