diff --git a/src/helpers/author.ts b/src/helpers/author.ts index 9da98f8..453326f 100644 --- a/src/helpers/author.ts +++ b/src/helpers/author.ts @@ -1,16 +1,18 @@ +import { formatNumber } from "./utils"; + /* The embed "author" text we populate with replies, retweets, and likes unless it's a video */ export const getAuthorText = (tweet: APITweet): string | null => { /* Build out reply, retweet, like counts */ if (tweet.likes > 0 || tweet.retweets > 0 || tweet.replies > 0) { let authorText = ''; if (tweet.replies > 0) { - authorText += `${tweet.replies} 💬 `; + authorText += `${formatNumber(tweet.replies)} 💬 `; } if (tweet.retweets > 0) { - authorText += `${tweet.retweets} 🔁 `; + authorText += `${formatNumber(tweet.retweets)} 🔁 `; } if (tweet.likes > 0) { - authorText += `${tweet.likes} ❤️ `; + authorText += `${formatNumber(tweet.likes)} ❤️ `; } authorText = authorText.trim(); diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 3fae465..2829722 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -14,3 +14,7 @@ export const unescapeText = (text: string) => { .replace(/>/g, '>') .replace(/&/g, '&'); }; + +const numberFormat = new Intl.NumberFormat('en-US'); + +export const formatNumber = (num: number) => numberFormat.format(num) \ No newline at end of file diff --git a/src/status.ts b/src/status.ts index 554456c..6881d82 100644 --- a/src/status.ts +++ b/src/status.ts @@ -1,6 +1,6 @@ import { Constants } from './constants'; import { handleQuote } from './helpers/quote'; -import { sanitizeText } from './helpers/utils'; +import { formatNumber, sanitizeText } from './helpers/utils'; import { Strings } from './strings'; import { getAuthorText } from './helpers/author'; import { statusAPI } from './api'; @@ -272,9 +272,11 @@ export const handleStatus = async ( }); /* Finally, add the footer of the poll with # of votes and time left */ - str += `\n${poll.total_votes} votes · ${poll.time_left_en}`; + str += `\n${formatNumber(poll.total_votes)} votes · ${poll.time_left_en}`; - /* Check if the poll is ongoing and apply low TTL cache control */ + /* Check if the poll is ongoing and apply low TTL cache control. + Yes, checking if this is a string is a hacky way to do this, but + it can do it in way less code than actually comparing dates */ if (poll.time_left_en !== 'Final results') { cacheControl = Constants.POLL_TWEET_CACHE; }