Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: complete utils/display.ts tests and docs #532

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions routes/pricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
stripe,
StripProductWithPrice,
} from "@/utils/stripe.ts";
import { formatAmountForDisplay } from "@/utils/display.ts";
import { formatCurrency } from "@/utils/display.ts";
import Stripe from "stripe";
import IconCheckCircle from "tabler_icons_tsx/circle-check.tsx";
import Head from "@/components/Head.tsx";
Expand Down Expand Up @@ -83,7 +83,7 @@ function PremiumPlanCard(
</div>
<p>
<span class="text-4xl font-bold">
{formatAmountForDisplay(
{formatCurrency(
defaultPrice.unit_amount! / 100,
defaultPrice?.currency,
)}
Expand Down
25 changes: 19 additions & 6 deletions utils/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { difference } from "std/datetime/difference.ts";
*
* pluralize(0, "meow"); // Returns "0 meows"
* pluralize(1, "meow"); // Returns "1 meow"
* pluralize(2, "meow"); // Returns "2 meows"
* ```
*/
export function pluralize(amount: number, unit: string) {
Expand All @@ -26,7 +25,6 @@ export function pluralize(amount: number, unit: string) {
* import { SECOND, MINUTE, HOUR } from "std/datetime/constants.ts";
*
* timeAgo(new Date()); // Returns "just now"
* timeAgo(new Date(Date.now() - MINUTE)); // Returns "2 minutes ago"
* timeAgo(new Date(Date.now() - 3 * HOUR)); // Returns "3 hours ago"
* ```
*/
Expand Down Expand Up @@ -55,18 +53,33 @@ export function timeAgo(date: Date) {
return pluralize(amount, unit.slice(0, -1)) + " ago";
}

export function formatAmountForDisplay(
/**
* Returns a formatted string based on the given amount of currency and the
* machine's preferred language.
*
* @see {@linkcode Intl.NumberFormat}
*
* @example
* ```ts
* import { formatCurrency } from "@/utils/display.ts";
*
* formatCurrency(4, "USD"); // Returns "$5"
* ```
*/
export function formatCurrency(
amount: number,
currency: string,
): string {
const numberFormat = new Intl.NumberFormat(
return new Intl.NumberFormat(
navigator.language,
{
style: "currency",
currency,
currencyDisplay: "symbol",
maximumFractionDigits: 0,
},
);
return numberFormat.format(amount);
).format(amount)
// Issue: https://stackoverflow.com/questions/44533919/space-after-symbol-with-js-intl
.replace(/^(\D+)/, "$1")
.replace(/\s+/, "");
}
13 changes: 11 additions & 2 deletions utils/display_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import { pluralize, timeAgo } from "./display.ts";
import { formatCurrency, pluralize, timeAgo } from "./display.ts";
import { DAY, HOUR, MINUTE, SECOND } from "std/datetime/constants.ts";
import { assertEquals } from "std/assert/mod.ts";
import { assertEquals, assertThrows } from "std/assert/mod.ts";

Deno.test("[display] pluralize()", () => {
assertEquals(pluralize(0, "item"), "0 items");
Expand All @@ -24,4 +24,13 @@ Deno.test("[display] timeAgo()", () => {
assertEquals(timeAgo(new Date(Date.now() - DAY)), "1 day ago");
assertEquals(timeAgo(new Date(Date.now() - DAY - HOUR * 12)), "1 day ago");
assertEquals(timeAgo(new Date(Date.now() - DAY * 5)), "5 days ago");
assertThrows(
() => timeAgo(new Date(Date.now() + 1)),
Error,
"Timestamp must be in the past",
);
});

Deno.test("[display] formatCurrency()", () => {
assertEquals(formatCurrency(5, "USD"), "$5");
});