Helper functions for precision handling, object conversion, and pace calculations.
Functions for handling floating-point precision issues and normalizing precision values.
/**
* Fixes binary rounding issues (e.g., (0.615).toFixed(2) === "0.61")
* @param value - Number to fix
* @param precision - Number of decimal places (default: 0)
* @returns Properly rounded fixed-point string
*/
function toFixed(value: number, precision?: number): string;
/**
* Ensures precision value is a positive integer
* @param value - Precision value to normalize
* @param base - Default value if invalid
* @returns Normalized precision value
*/
function normalizePrecision(value: number, base?: number): number;Usage Examples:
const Humanize = require('humanize-plus');
// Fix rounding issues
console.log((0.615).toFixed(2)); // "0.61" (JavaScript's broken behavior)
console.log(Humanize.toFixed(0.615, 2)); // "0.62" (correct rounding)
console.log(Humanize.toFixed(1.005, 2)); // "1.01"
console.log(Humanize.toFixed(123.456, 1)); // "123.5"
// Normalize precision
console.log(Humanize.normalizePrecision(-5)); // 5 (absolute value)
console.log(Humanize.normalizePrecision(3.7)); // 4 (rounded)
console.log(Humanize.normalizePrecision(NaN, 2)); // 2 (uses base for NaN)Converts objects to definition-like strings with customizable formatting.
/**
* Converts an object to a definition-like string
* @param object - Object to convert (must be a plain object, not array)
* @param joiner - Key-value separator (default: ' is ')
* @param separator - Entry separator (default: ', ')
* @returns String representation of object properties
*/
function dictionary(object: object, joiner?: string, separator?: string): string;Usage Examples:
const Humanize = require('humanize-plus');
const person = { name: 'John', age: 30, city: 'New York' };
// Default formatting
console.log(Humanize.dictionary(person));
// "name is John, age is 30, city is New York"
// Custom separators
console.log(Humanize.dictionary(person, ': ', '; '));
// "name: John; age: 30; city: New York"
console.log(Humanize.dictionary(person, ' = ', ' | '));
// "name = John | age = 30 | city = New York"
// Invalid inputs return empty string
console.log(Humanize.dictionary('not an object')); // ""
console.log(Humanize.dictionary([1, 2, 3])); // ""
console.log(Humanize.dictionary(null)); // ""Converts pace (value and time interval) to human-readable frequency descriptions.
/**
* Matches a pace (value and interval) with logical time frame
* @param value - Number of occurrences
* @param intervalMs - Time interval in milliseconds
* @param unit - Unit name (default: 'time')
* @returns Human-readable frequency string
*/
function pace(value: number, intervalMs: number, unit?: string): string;Usage Examples:
const Humanize = require('humanize-plus');
// Time constants
const second = 1000;
const minute = 60 * 1000;
const hour = 60 * minute;
const day = 24 * hour;
const week = 7 * day;
// Fast paces
console.log(Humanize.pace(1.5, second, 'heartbeat'));
// "Approximately 2 heartbeats per second"
console.log(Humanize.pace(30, minute));
// "Approximately 30 times per minute"
// Moderate paces
console.log(Humanize.pace(4, week));
// "Approximately 4 times per week"
console.log(Humanize.pace(1, day, 'meal'));
// "Approximately 1 meal per day"
// Slow paces
console.log(Humanize.pace(1, week * 52, 'vacation'));
// "Less than 1 vacation per week"
// Zero values
console.log(Humanize.pace(0, hour)); // "No times"
console.log(Humanize.pace(5, 0)); // "No times"Pace Time Units:
The function automatically selects the most appropriate time unit:
For very slow paces, it uses "Less than 1 [unit] per week" format.