-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: polish display utilities (#455)
- Loading branch information
Showing
2 changed files
with
61 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,56 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { difference } from "std/datetime/difference.ts"; | ||
|
||
export function pluralize(unit: number, label: string) { | ||
return unit === 1 ? `${unit} ${label}` : `${unit} ${label}s`; | ||
/** | ||
* Returns a pluralized string for the given amount and unit. | ||
* | ||
* @example | ||
* ```ts | ||
* import { pluralize } from "@/utils/display.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) { | ||
return amount === 1 ? `${amount} ${unit}` : `${amount} ${unit}s`; | ||
} | ||
|
||
export function timeAgo(time: number | Date) { | ||
const minutes = difference(new Date(), new Date(time))?.minutes; | ||
if (!minutes) return pluralize(0, "minute"); | ||
if (minutes < 60) return pluralize(~~minutes, "minute"); | ||
else if (minutes < 24 * 60) return pluralize(~~(minutes / 60), "hour"); | ||
else return pluralize(~~(minutes / (24 * 60)), "day"); | ||
/** | ||
* Returns how long ago a given date is from now. | ||
* | ||
* @example | ||
* ```ts | ||
* import { timeAgo } from "@/utils/display.ts"; | ||
* import { SECOND, MINUTE, HOUR } from ""std/datetime/constants.ts"" | ||
* | ||
* timeAgo(new Date(Date.now() - SECOND)); // Returns "1 second" | ||
* timeAgo(new Date(Date.now() - 2 * MINUTE)); // Returns "2 minutes" | ||
* timeAgo(new Date(Date.now() - 3 * HOUR)); // Returns "3 hours" | ||
* ``` | ||
*/ | ||
export function timeAgo(date: Date) { | ||
const now = new Date(); | ||
if (date > now) throw new Error("Timestamp must be in the past"); | ||
const match = Object.entries( | ||
difference(now, date, { | ||
// These units make sense for a web UI | ||
units: [ | ||
"seconds", | ||
"minutes", | ||
"hours", | ||
"days", | ||
"weeks", | ||
"months", | ||
"years", | ||
], | ||
}), | ||
) | ||
.toReversed() | ||
.find(([_, amount]) => amount > 0); | ||
if (match === undefined) return "Now"; | ||
const [unit, amount] = match; | ||
// Remove the last character which is an "s" | ||
return pluralize(amount, unit.slice(0, -1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters