From e2dab416f293423651533d9973c370afd4acef76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Mon, 31 Jul 2023 15:52:42 +0200 Subject: [PATCH] GeneralUtils/Profile: Shorten numbers to K M B format - Implemented for currencies on the profile --- commands/profile.js | 20 ++++++++++---------- util/general.js | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/commands/profile.js b/commands/profile.js index de05285..7254c51 100644 --- a/commands/profile.js +++ b/commands/profile.js @@ -1,7 +1,7 @@ require("dotenv").config(); const { SlashCommandBuilder } = require("discord.js"); const { Card } = require("../models"); -const { UserUtils, Rendering } = require("../util"); +const { UserUtils, Rendering, GeneralUtils } = require("../util"); const axios = require("axios"); const { CURRENCY_NAMES } = require("../config/constants"); @@ -103,7 +103,7 @@ module.exports = { }, { "type": "text", - "text": `CC: ${await Card.count({where: {userId: user.id}})}`, + "text": `CC: ${GeneralUtils.formatNumber(await Card.count({where: {userId: user.id}}))}`, "fontSize": 30, "x": 550, "y": 20, @@ -123,31 +123,31 @@ module.exports = { }, { "type": "text", - "text": `${await user.primaryCurrency} ${CURRENCY_NAMES[1]}`, + "text": `${GeneralUtils.formatNumber(await user.primaryCurrency)} ${CURRENCY_NAMES[1]}`, "fontSize": 30, "x": 850, "y": 20, - "width": 150, + "width": 170, "height": 30, "horizontalAlignment": "left" }, { "type": "text", - "text": `${await user.secondaryCurrency} ${CURRENCY_NAMES[2]}`, + "text": `${await GeneralUtils.formatNumber(user.secondaryCurrency)} ${CURRENCY_NAMES[2]}`, "fontSize": 30, - "x": 1000, + "x": 1020, "y": 20, - "width": 150, + "width": 170, "height": 30, "horizontalAlignment": "left" }, { "type": "text", "text": customStatus, - "fontSize": 30, + "fontSize": 25, "x": 550, - "y": 55, - "width": 600, + "y": 65, + "width": 625, "height": 300, "horizontalAlignment": "left" } diff --git a/util/general.js b/util/general.js index 739ec57..316d978 100644 --- a/util/general.js +++ b/util/general.js @@ -17,5 +17,23 @@ module.exports = { generateLogID: async function() { return crypto.randomBytes(4).toString("hex"); + }, + + formatNumber: function(num, precision = 1) { + const map = [ + { suffix: 'T', threshold: 1e12 }, + { suffix: 'B', threshold: 1e9 }, + { suffix: 'M', threshold: 1e6 }, + { suffix: 'K', threshold: 1e3 }, + { suffix: '', threshold: 1 }, + ]; + + const found = map.find((x) => Math.abs(num) >= x.threshold); + if (found) { + const formatted = (Math.floor((num / found.threshold)*10) / 10) + found.suffix; + return formatted; + } + + return num; } }