From b88f51fe937bd5edb3579bcc15ce065ec3708dbc Mon Sep 17 00:00:00 2001 From: Tejas Lamba Date: Tue, 4 Jul 2023 15:12:59 +0530 Subject: [PATCH] added `prettyBytes` function --- index.js | 6 +- package.json | 5 +- readme.md | 14 +++++ src/functions/utilities/prettyBytes.js | 36 +++++++++++ test/index.js | 83 +++++++++++++------------- 5 files changed, 97 insertions(+), 47 deletions(-) create mode 100644 src/functions/utilities/prettyBytes.js diff --git a/index.js b/index.js index b0f892b..3a71f41 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,8 @@ const sleep = require('./src/functions/utilities/sleep'), cleanCode = require("./src/functions/utilities/cleanCode"), generateActivity = require("./src/functions/utilities/generateActivity"), disableButtons = require("./src/functions/utilities/disableButtons"), - { quickExport, exportChat, rawExport, Transcript } = require('./src/functions/tickets/main.js'); + { quickExport, exportChat, rawExport, Transcript } = require('./src/functions/tickets/main.js'), + prettyBytes = require("./src/functions/utilities/prettyBytes") checkUpdate() @@ -24,5 +25,6 @@ module.exports = { quickExport, exportChat, rawExport, - Transcript + Transcript, + prettyBytes }; diff --git a/package.json b/package.json index 5059965..4fea3c0 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,11 @@ { "name": "visa2discord", - "version": "1.1.0", + "version": "1.1.1", "description": "A package for your discord needs", "main": "index.js", "directories": { "test": "test" }, - "publishConfig": { - "registry": "https://npm.pkg.github.com/" - }, "dependencies": { "discord.js": "^14.11.0", "moment": "^2.29.4", diff --git a/readme.md b/readme.md index 06b47cd..17944b8 100644 --- a/readme.md +++ b/readme.md @@ -19,6 +19,7 @@ For your discord utilities - [Transcripts](#transcripts) - [Methods](#methods) - [Classes](#classes) + - [Pretty Bytes](#pretty-bytes) - [Contributing](#contributing) - [License](#license) - [Credits](#credits) @@ -309,6 +310,19 @@ For your discord utilities - `Transcript` - The `Transcript` class represents a chat transcript export and extends the `TranscriptDAO` class. +## Pretty Bytes +- Formats a number of bytes into a human-readable string representation. +- Usage + ```js + const { prettyBytes } = require('visa2discord'); + const bytes = 1000000000; + const formatted = prettyBytes(bytes); + console.log('Formatted bytes:', formatted); + ``` + Example output + ``` + Formatted bytes: 1 GB + ``` # Contributing - If you want to contribute to this project, you can fork this repository and make a pull request. diff --git a/src/functions/utilities/prettyBytes.js b/src/functions/utilities/prettyBytes.js new file mode 100644 index 0000000..9936d9f --- /dev/null +++ b/src/functions/utilities/prettyBytes.js @@ -0,0 +1,36 @@ +/** + * Formats a number of bytes into a human-readable string representation. + * + * @param {number} bytes - The number of bytes to format. + * @param {Object} [options={}] - Formatting options. + * @param {boolean} [options.bits=false] - If true, format the bytes as bits. + * @param {boolean} [options.signed=false] - If true, include a plus sign for positive values when using the signed format. + * @param {string} [options.locale='en'] - The locale to use for formatting. Can be 'en' (English) or 'de' (German). + * @returns {string} The formatted byte size string. + */ + +module.exports = (bytes, options = {}) => { + const { bits = false, signed = false, locale = "en" } = options; + + const units = bits + ? ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"] + : ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + + const kilo = bits ? 1024 : 1000; + const isNegative = signed && bytes < 0; + const num = isNegative ? -bytes : bytes; + + if (num < 1) { + return (isNegative ? "-" : "") + num + " " + units[0]; + } + + const exponent = Math.min( + Math.floor(Math.log10(num) / Math.log10(kilo)), + units.length - 1 + ); + const value = (num / Math.pow(kilo, exponent)).toFixed(2); + + const formattedValue = locale === "en" ? value : value.replace(".", ","); + + return (isNegative ? "-" : "") + formattedValue + " " + units[exponent]; +}; diff --git a/test/index.js b/test/index.js index fa5bdf3..0246d42 100644 --- a/test/index.js +++ b/test/index.js @@ -1,44 +1,45 @@ -//make a simple discord.js client -const Discord = require('discord.js'); -const client = new Discord.Client({ - intents: [ - Discord.GatewayIntentBits.Guilds, - Discord.GatewayIntentBits.GuildMembers, - Discord.GatewayIntentBits.GuildModeration, - Discord.GatewayIntentBits.GuildEmojisAndStickers, - Discord.GatewayIntentBits.GuildWebhooks, - Discord.GatewayIntentBits.GuildInvites, - Discord.GatewayIntentBits.GuildVoiceStates, - Discord.GatewayIntentBits.GuildMessages, - Discord.GatewayIntentBits.DirectMessages, - Discord.GatewayIntentBits.MessageContent, - Discord.GatewayIntentBits.AutoModerationConfiguration, - Discord.GatewayIntentBits.AutoModerationExecution, - ] -}); -client.on('ready', async () => { - console.log(`Logged in as ${client.user.tag}!`); - const test = require('../src/functions/tickets/main.js'); - const channel = await client.channels.fetch('1123861563141533754'); - const allMessages = new Discord.Collection(); - let messages = await channel.messages.fetch({ limit: 100, cache: false, - force: true, }); +// //make a simple discord.js client +// const Discord = require('discord.js'); +// const client = new Discord.Client({ +// intents: [ +// Discord.GatewayIntentBits.Guilds, +// Discord.GatewayIntentBits.GuildMembers, +// Discord.GatewayIntentBits.GuildModeration, +// Discord.GatewayIntentBits.GuildEmojisAndStickers, +// Discord.GatewayIntentBits.GuildWebhooks, +// Discord.GatewayIntentBits.GuildInvites, +// Discord.GatewayIntentBits.GuildVoiceStates, +// Discord.GatewayIntentBits.GuildMessages, +// Discord.GatewayIntentBits.DirectMessages, +// Discord.GatewayIntentBits.MessageContent, +// Discord.GatewayIntentBits.AutoModerationConfiguration, +// Discord.GatewayIntentBits.AutoModerationExecution, +// ] +// }); +// client.on('ready', async () => { +// console.log(`Logged in as ${client.user.tag}!`); +// const test = require('../src/functions/tickets/main.js'); +// const channel = await client.channels.fetch('1123861563141533754'); +// const allMessages = new Discord.Collection(); +// let messages = await channel.messages.fetch({ limit: 100, cache: false, +// force: true, }); - allMessages.concat(messages); - //concat doesnt do anytthing do something else - // console.log(allMessages) - while (messages.size === 100) { - const lastMessageId = messages.lastKey(); - messages = await channel.messages.fetch({ limit: 100, before: lastMessageId, cache: false, - force: true, }); - allMessages.concat(messages); - } - -const trsa = await test.quickExport(channel).catch(console.error); -channel.send({ files: [new Discord.AttachmentBuilder(trsa, { name: `xd.html` })] }) -}); -//login to discord with your app's token -client.login('MTEwOTA2NTQ0MjMwNTI1MzM4Ng.GSKaL5.K6nUopgM92RcZ-_2X6lSUwWcTP_Aa6qtIu324o'); -//when the bot receives a message +// allMessages.concat(messages); +// //concat doesnt do anytthing do something else +// // console.log(allMessages) +// while (messages.size === 100) { +// const lastMessageId = messages.lastKey(); +// messages = await channel.messages.fetch({ limit: 100, before: lastMessageId, cache: false, +// force: true, }); +// allMessages.concat(messages); +// } +// const trsa = await test.quickExport(channel).catch(console.error); +// channel.send({ files: [new Discord.AttachmentBuilder(trsa, { name: `xd.html` })] }) +// }); +// //login to discord with your app's token +// client.login('MTEwOTA2NTQ0MjMwNTI1MzM4Ng.GSKaL5.K6nUopgM92RcZ-_2X6lSUwWcTP_Aa6qtIu324o'); +// //when the bot receives a message +const {prettyBytes} = require('../index.js') +console.log(prettyBytes(100000, {locale: 'de'}, {bits: true}))