refactor: add JSDoc for helper functions

This commit is contained in:
MAZE 2024-06-15 13:06:48 +04:30
parent af075b32e6
commit 4ae0504937
5 changed files with 63 additions and 0 deletions

View File

@ -1,3 +1,11 @@
/**
* Counts the number of characters and words in a given string.
*
* @param {string} _string - The input string to be analyzed.
* @returns {{characters: number, words: number}} An object containing the counts:
* - characters: The number of non-whitespace characters in the input string.
* - words: The number of words in the input string.
*/
export function count(_string: string) {
const string = _string.trim();

View File

@ -1,3 +1,9 @@
/**
* Triggers a download of a file with the specified filename and content.
*
* @param {string} filename - The name of the file to be downloaded.
* @param {string} content - The content to be included in the downloaded file.
*/
export function download(filename: string, content: string) {
const element = document.createElement('a');
element.setAttribute(

View File

@ -1,3 +1,10 @@
/**
* Pads a given number with leading zeros to ensure it reaches a specified length.
*
* @param {number} number - The number to be padded.
* @param {number} [maxLength=2] - The desired length of the resulting string. Defaults to 2 if not provided.
* @returns {string} The padded number as a string.
*/
export function padNumber(number: number, maxLength: number = 2): string {
return number.toString().padStart(maxLength, '0');
}

View File

@ -1,23 +1,59 @@
/**
* Generates a random number between the specified minimum and maximum values.
*
* @param {number} min - The minimum value (inclusive).
* @param {number} max - The maximum value (exclusive).
* @returns {number} A random number between min (inclusive) and max (exclusive).
*/
export function random(min: number, max: number): number {
return Math.random() * (max - min) + min;
}
/**
* Generates a random integer between the specified minimum and maximum values.
*
* @param {number} min - The minimum value (inclusive).
* @param {number} max - The maximum value (exclusive).
* @returns {number} A random integer between min (inclusive) and max (exclusive).
*/
export function randomInt(min: number, max: number): number {
return Math.floor(random(min, max));
}
/**
* Picks a random element from the given array.
*
* @template T
* @param {Array<T>} array - The array to pick an element from.
* @returns {T} A random element from the array.
*/
export function pick<T>(array: Array<T>): T {
const randomIndex = random(0, array.length);
return array[randomIndex];
}
/**
* Picks a specified number of random elements from the given array.
*
* @template T
* @param {Array<T>} array - The array to pick elements from.
* @param {number} count - The number of elements to pick.
* @returns {Array<T>} An array containing the picked elements.
*/
export function pickMany<T>(array: Array<T>, count: number): Array<T> {
const shuffled = shuffle(array);
return shuffled.slice(0, count);
}
/**
* Shuffles the elements of the given array in random order.
*
* @template T
* @param {Array<T>} array - The array to shuffle.
* @returns {Array<T>} The shuffled array.
*/
export function shuffle<T>(array: Array<T>): Array<T> {
return array
.map(value => ({ sort: Math.random(), value }))

View File

@ -1,5 +1,11 @@
type className = undefined | null | false | string;
/**
* Combines multiple class names into a single string, filtering out invalid values.
*
* @param {...(undefined|null|false|string)} classNames - The class names to be combined.
* @returns {string} A single string containing all valid class names separated by spaces.
*/
export function cn(...classNames: Array<className>): string {
const className = classNames.filter(className => !!className).join(' ');