JaBa/helpers/functions.js

153 lines
5.4 KiB
JavaScript
Raw Normal View History

2024-06-15 17:46:43 +05:00
const moment = require("moment"),
2024-06-15 21:39:11 +05:00
Canvas = require("@napi-rs/canvas"),
{ resolve } = require("path");
2024-06-15 17:46:43 +05:00
Canvas.registerFont(resolve("./assets/fonts/RubikMonoOne-Regular.ttf"), { family: "RubikMonoOne" });
Canvas.registerFont(resolve("./assets/fonts/KeepCalm-Medium.ttf"), { family: "KeepCalm" });
2022-01-04 02:18:28 +05:00
module.exports = {
/**
2024-04-07 15:29:16 +05:00
* Asynchronously iterates over a collection and executes a callback function for each item.
*
* @param {any[]} collection - The collection to iterate over.
* @param {(item: any) => Promise<void>} callback - The async callback function to execute for each item in the collection.
* @returns {Promise<void>} A promise that resolves when all items in the collection have been processed.
*/
async asyncForEach(collection, callback) {
const allPromises = collection.map(async key => {
await callback(key);
});
return await Promise.all(allPromises);
},
2022-12-24 18:25:41 +05:00
/**
2024-04-07 15:29:16 +05:00
* Sorts an array by the specified key in ascending order.
*
* @param {any[]} array - The array to sort.
* @param {string} key - The key to sort the array by.
* @returns {any[]} The sorted array.
2022-12-24 18:25:41 +05:00
*/
2022-01-04 02:18:28 +05:00
sortByKey(array, key) {
return array.sort(function (a, b) {
const x = a[key];
const y = b[key];
2023-07-05 00:58:06 +05:00
return x < y ? 1 : x > y ? -1 : 0;
2022-01-04 02:18:28 +05:00
});
},
2022-12-24 18:25:41 +05:00
/**
2024-04-07 15:29:16 +05:00
* Shuffles the elements of the provided array in-place.
*
* @param {any[]} pArray - The array to shuffle.
* @returns {any[]} The shuffled array.
2022-12-24 18:25:41 +05:00
*/
2022-01-04 02:18:28 +05:00
shuffle(pArray) {
const array = [];
2023-11-05 16:03:23 +05:00
2022-01-04 02:18:28 +05:00
pArray.forEach(element => array.push(element));
2023-11-05 16:03:23 +05:00
2022-01-04 02:18:28 +05:00
let currentIndex = array.length,
2023-07-05 00:58:06 +05:00
temporaryValue,
randomIndex;
2022-01-04 02:18:28 +05:00
while (currentIndex !== 0) {
2022-01-04 02:18:28 +05:00
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
2022-12-24 18:25:41 +05:00
2022-01-04 02:18:28 +05:00
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
},
2022-12-24 18:25:41 +05:00
/**
2024-04-07 15:29:16 +05:00
* Generates a random integer between the specified minimum and maximum values (inclusive).
*
2024-04-29 15:57:02 +05:00
* @param {number} [min=0] - The minimum value (inclusive).
* @param {number} [max=100] - The maximum value (inclusive).
2024-04-07 15:29:16 +05:00
* @returns {number} A random integer between min and max.
2022-12-24 18:25:41 +05:00
*/
2024-04-29 15:57:02 +05:00
randomNum(min = 0, max = 100) {
2022-12-24 18:25:41 +05:00
min = Math.ceil(min);
max = Math.floor(max);
2023-10-10 20:44:42 +05:00
2022-12-24 18:25:41 +05:00
return Math.floor(Math.random() * (max - min + 1) + min);
},
/**
2024-04-07 15:29:16 +05:00
* Formats a date for the specified client and locale.
*
* @param {Object} client - The client object containing language data.
* @param {string} date - The date to format.
* @param {string} [format=null] - The date format to use. If not provided, the default format for the client's language will be used.
* @param {string} [locale=client.defaultLanguage.name] - The locale to use for formatting the date.
* @returns {string} The formatted date.
*/
2024-02-09 23:26:57 +05:00
printDate(client, date, format = null, locale = client.defaultLanguage.name) {
2023-05-30 15:28:41 +05:00
const languageData = client.languages.find(language => language.name === locale);
if (format === "" || format === null) format = languageData.defaultMomentFormat;
2023-10-10 20:44:42 +05:00
2023-07-05 00:58:06 +05:00
return moment(new Date(date)).locale(languageData.moment).format(format);
},
/**
2024-04-07 15:29:16 +05:00
* Formats a time value relative to the current time.
*
* @param {Object} client - The client object containing language data.
* @param {string|number|Date} time - The time value to format.
* @param {boolean} [type=false] - If true, formats the time as "X time ago", otherwise formats it as "in X time".
* @param {boolean} [prefix=true] - If true, includes a prefix like "in" or "ago" in the formatted time.
* @param {string} [locale=client.defaultLanguage.name] - The locale to use for formatting the time.
* @returns {string} The formatted time value.
*/
2024-02-09 23:26:57 +05:00
convertTime(client, time, type = false, prefix = true, locale = client.defaultLanguage.name) {
2023-05-30 15:28:41 +05:00
const languageData = client.languages.find(language => language.name === locale);
const m = moment(time).locale(languageData.moment);
2023-10-10 20:44:42 +05:00
2023-11-05 16:03:23 +05:00
return type ? m.toNow(!prefix) : m.fromNow(!prefix);
},
/**
2024-04-07 15:29:16 +05:00
* Generates the appropriate noun form based on the given number and noun forms.
*
* @param {number} number - The number to use for determining the noun form.
* @param {string} one - The noun form for the singular case.
* @param {string} two - The noun form for the dual case.
* @param {string} five - The noun form for the plural case.
* @returns {string} The appropriate noun form based on the given number.
*/
getNoun(number, one, two, five) {
let n = Math.abs(number);
n %= 100;
if (n >= 5 && n <= 20) return five;
n %= 10;
if (n === 1) return one;
if (n >= 2 && n <= 4) return two;
return five;
},
2024-06-15 17:46:43 +05:00
/**
* Function to apply text on a canvas with dynamic font size based on the width constraint.
*
* @param {import("@napi-rs/canvas").Canvas} canvas - The canvas object where the text will be applied.
* @param {string} text - The string of text that needs to be applied on the canvas.
* @param {number} defaultFontSize - The initial font size for the text. It is expected to decrease with each iteration.
* @param {number} width - The maximum width that the text can occupy before it has to shrink down.
* @param {string} font - The name of the font used for drawing the text on the canvas.
*
* @return {string} - The final calculated font size in a format '<size>px <family>'.
*/
applyText(canvas, text, defaultFontSize, width, font) {
const ctx = canvas.getContext("2d");
do ctx.font = `${(defaultFontSize -= 1)}px ${font}`;
while (ctx.measureText(text).width > width);
return ctx.font;
},
2023-07-05 00:58:06 +05:00
};