aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorNuckyz <61953774+Nuckyz@users.noreply.github.com>2023-02-01 08:11:05 -0300
committerGitHub <noreply@github.com>2023-02-01 12:11:05 +0100
commit369d179bbf67d34fc4d5f8312d19a106f3552373 (patch)
tree54849a5a410a00201f5a5ccd2803f31c6fb9475a /src/utils
parent8f4e8d0a9bd29b59cd9ea4e3228fd1b3e73fbfd9 (diff)
downloadVencord-369d179bbf67d34fc4d5f8312d19a106f3552373.tar.gz
Vencord-369d179bbf67d34fc4d5f8312d19a106f3552373.tar.bz2
Vencord-369d179bbf67d34fc4d5f8312d19a106f3552373.zip
ShowHiddenChannels: New screen for showing hidden channels (#460)
Co-authored-by: Ven <vendicated@riseup.net>
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/index.ts1
-rw-r--r--src/utils/text.ts25
2 files changed, 26 insertions, 0 deletions
diff --git a/src/utils/index.ts b/src/utils/index.ts
index 41e1597..b80bde3 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -27,4 +27,5 @@ export * as Modals from "./modal";
export * from "./onceDefined";
export * from "./proxyLazy";
export * from "./Queue";
+export * from "./text";
diff --git a/src/utils/text.ts b/src/utils/text.ts
index 17826e8..fae3343 100644
--- a/src/utils/text.ts
+++ b/src/utils/text.ts
@@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+import { moment } from "@webpack/common";
+
// Utils for readable text transformations eg: `toTitle(fromKebab())`
// Case style to words
@@ -34,3 +36,26 @@ export const wordsToPascal = (words: string[]) =>
words.map(w => w[0].toUpperCase() + w.slice(1)).join("");
export const wordsToTitle = (words: string[]) =>
words.map(w => w[0].toUpperCase() + w.slice(1)).join(" ");
+
+/**
+ * Forms milliseconds into a human readable string link "1 day, 2 hours, 3 minutes and 4 seconds"
+ * @param ms Milliseconds
+ * @param short Whether to use short units like "d" instead of "days"
+ */
+export function formatDuration(ms: number, short: boolean = false) {
+ const dur = moment.duration(ms);
+ return (["years", "months", "weeks", "days", "hours", "minutes", "seconds"] as const).reduce((res, unit) => {
+ const x = dur[unit]();
+ if (x > 0 || res.length) {
+ if (res.length)
+ res += unit === "seconds" ? " and " : ", ";
+
+ const unitStr = short
+ ? unit[0]
+ : x === 1 ? unit.slice(0, -1) : unit;
+
+ res += `${x} ${unitStr}`;
+ }
+ return res;
+ }, "").replace(/((,|and) \b0 \w+)+$/, "") || "now";
+}