diff --git a/src/helpers/counter.ts b/src/helpers/counter.ts index 2289bbb..f0079f6 100644 --- a/src/helpers/counter.ts +++ b/src/helpers/counter.ts @@ -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(); diff --git a/src/helpers/download.ts b/src/helpers/download.ts index 7d205b9..d8ff211 100644 --- a/src/helpers/download.ts +++ b/src/helpers/download.ts @@ -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( diff --git a/src/helpers/number.ts b/src/helpers/number.ts index e904111..438747b 100644 --- a/src/helpers/number.ts +++ b/src/helpers/number.ts @@ -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'); } diff --git a/src/helpers/random.ts b/src/helpers/random.ts index c05fbeb..d150965 100644 --- a/src/helpers/random.ts +++ b/src/helpers/random.ts @@ -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} array - The array to pick an element from. + * @returns {T} A random element from the array. + */ export function pick(array: Array): 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} array - The array to pick elements from. + * @param {number} count - The number of elements to pick. + * @returns {Array} An array containing the picked elements. + */ export function pickMany(array: Array, count: number): Array { const shuffled = shuffle(array); return shuffled.slice(0, count); } +/** + * Shuffles the elements of the given array in random order. + * + * @template T + * @param {Array} array - The array to shuffle. + * @returns {Array} The shuffled array. + */ export function shuffle(array: Array): Array { return array .map(value => ({ sort: Math.random(), value })) diff --git a/src/helpers/styles.ts b/src/helpers/styles.ts index ba45cf7..f9bce59 100644 --- a/src/helpers/styles.ts +++ b/src/helpers/styles.ts @@ -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): string { const className = classNames.filter(className => !!className).join(' ');