tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
87 lines • 3.66 kB
text/typescript
/**
* Executes a Rule of Three calculation.
*
* @param {number} val1 - The first reference value (numerator in direct proportion, denominator in inverse).
* @param {number} val2 - The second reference value (denominator in direct proportion, numerator in inverse).
* @param {number} val3 - The third value (numerator in direct proportion, denominator in inverse).
* @param {boolean} [inverse] - Whether the calculation should use inverse proportion (true for inverse, false for direct).
* @returns {number} The result of the Rule of Three operation.
*
* Rule of Three Formula (Direct Proportion):
* val1 / val2 = val3 / result
*
* For Inverse Proportion:
* val1 / val3 = val2 / result
*
* Visual Representation:
*
* For Direct Proportion:
* val1 val2
* ----- = ------
* val3 result
*
* For Inverse Proportion:
* val1 val2
* ----- = ------
* val3 result
*
* @example
* // Direct proportion:
* ruleOfThree.execute(2, 6, 3, false); // → 9
*
* @example
* // Inverse proportion:
* ruleOfThree.execute(2, 6, 3, true); // → 4
*/
export function ruleOfThree(val1: number, val2: number, val3: number, inverse?: boolean): number;
/**
* Calculates a percentage of a given base value.
* @param {number} price - The base value.
* @param {number} percentage - The percentage to apply.
* @returns {number} The result of the percentage calculation.
*
* @example
* getSimplePerc(200, 15); // 30
*/
export function getSimplePerc(price: number, percentage: number): number;
/**
* Calculates the age based on the given date.
*
* @param {number|string|Date} timeData - The birth date (can be a timestamp, ISO string, or Date object).
* @param {Date|null} [now=null] - The Date object representing the current date. Defaults to the current date and time if not provided.
* @returns {number|null} The age in years, or null if `timeData` is not provided or invalid.
*/
export function getAge(timeData?: number | string | Date, now?: Date | null): number | null;
/**
* @typedef {Object} FormattedByteResult
* @property {string|null} unit - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
* @property {number|null} value - The numerical value in the chosen unit, or null if input is invalid.
*/
/**
* Converts a byte value into a human-readable format with unit and value separated.
*
* @param {number} bytes - The number of bytes to format. Must be a non-negative number.
* @param {number|null} [decimals=null] - The number of decimal places to include in the result. Defaults to null. If negative, it will be treated as 0. If null, no rounding is applied.
* @param {string|null} [maxUnit=null] - Optional unit limit. If provided, restricts conversion to this unit at most (e.g., 'MB' prevents conversion to 'GB' or higher). Must be one of: 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'.
* @returns {FormattedByteResult} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
*
* @example
* formatBytes(123456789);
* // → { unit: 'MB', value: 117.74 }
*
* @example
* formatBytes(1073741824, 2, 'MB');
* // → { unit: 'MB', value: 1024 }
*/
export function formatBytes(bytes: number, decimals?: number | null, maxUnit?: string | null): FormattedByteResult;
export type FormattedByteResult = {
/**
* - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
*/
unit: string | null;
/**
* - The numerical value in the chosen unit, or null if input is invalid.
*/
value: number | null;
};
//# sourceMappingURL=simpleMath.d.mts.map