UNPKG

@tmlmobilidade/utils

Version:

A collection of utility functions and helpers for the TML Mobilidade Go monorepo, providing common functionality for batching operations, caching, HTTP requests, object manipulation, permissions, and more.

40 lines (39 loc) 1.44 kB
/* * */ /** * Utility function to convert a number to an integer, * handling null and undefined values gracefully. * @param value The value to convert to an integer. Can be a number, null, or undefined. * @returns The rounded integer value if the input is a number, or null if the input is null or undefined. * @example * roundToInt(3.7); // returns 4 * roundToInt(-2.3); // returns -2 * roundToInt(null); // returns null * roundToInt(undefined); // returns null */ export function roundToInt(value) { if (value === null || value === undefined) return null; const num = typeof value === 'string' ? parseFloat(value) : value; if (isNaN(num)) return null; return Math.round(num); } /** * Utility function to convert a number to an integer, * handling null and undefined values gracefully. * @param value The value to convert to an integer. Can be a number, null, or undefined. * @returns The truncated integer value if the input is a number, or null if the input is null or undefined. * @example * truncToInt(3.7); // returns 3 * truncToInt(-2.3); // returns -2 * truncToInt(null); // returns null * truncToInt(undefined); // returns null */ export function truncToInt(value) { if (value === null || value === undefined) return null; const num = typeof value === 'string' ? parseFloat(value) : value; if (isNaN(num)) return null; return Math.trunc(num); }