Add number formatting (#148)

This commit is contained in:
dangered wolf 2022-12-22 21:00:27 -05:00
parent d08d56a718
commit d37812bd4d
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
3 changed files with 14 additions and 6 deletions

View file

@ -1,16 +1,18 @@
import { formatNumber } from "./utils";
/* The embed "author" text we populate with replies, retweets, and likes unless it's a video */ /* The embed "author" text we populate with replies, retweets, and likes unless it's a video */
export const getAuthorText = (tweet: APITweet): string | null => { export const getAuthorText = (tweet: APITweet): string | null => {
/* Build out reply, retweet, like counts */ /* Build out reply, retweet, like counts */
if (tweet.likes > 0 || tweet.retweets > 0 || tweet.replies > 0) { if (tweet.likes > 0 || tweet.retweets > 0 || tweet.replies > 0) {
let authorText = ''; let authorText = '';
if (tweet.replies > 0) { if (tweet.replies > 0) {
authorText += `${tweet.replies} 💬 `; authorText += `${formatNumber(tweet.replies)} 💬 `;
} }
if (tweet.retweets > 0) { if (tweet.retweets > 0) {
authorText += `${tweet.retweets} 🔁 `; authorText += `${formatNumber(tweet.retweets)} 🔁 `;
} }
if (tweet.likes > 0) { if (tweet.likes > 0) {
authorText += `${tweet.likes} ❤️ `; authorText += `${formatNumber(tweet.likes)} ❤️ `;
} }
authorText = authorText.trim(); authorText = authorText.trim();

View file

@ -14,3 +14,7 @@ export const unescapeText = (text: string) => {
.replace(/>/g, '>') .replace(/>/g, '>')
.replace(/&/g, '&'); .replace(/&/g, '&');
}; };
const numberFormat = new Intl.NumberFormat('en-US');
export const formatNumber = (num: number) => numberFormat.format(num)

View file

@ -1,6 +1,6 @@
import { Constants } from './constants'; import { Constants } from './constants';
import { handleQuote } from './helpers/quote'; import { handleQuote } from './helpers/quote';
import { sanitizeText } from './helpers/utils'; import { formatNumber, sanitizeText } from './helpers/utils';
import { Strings } from './strings'; import { Strings } from './strings';
import { getAuthorText } from './helpers/author'; import { getAuthorText } from './helpers/author';
import { statusAPI } from './api'; 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 */ /* 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') { if (poll.time_left_en !== 'Final results') {
cacheControl = Constants.POLL_TWEET_CACHE; cacheControl = Constants.POLL_TWEET_CACHE;
} }