toFinite
lang
Converts value to a finite number.
Installation
Import
import { toFinite } from '@tulx/utils';Source Code
Implementation
/**
* Converts value to a finite number.
*
* @param value - The value to convert.
* @returns Returns the converted number.
*
* @example
* ```ts
* toFinite(3.2); // 3.2
* toFinite(Number.MIN_VALUE); // 5e-324
* toFinite(Infinity); // 1.7976931348623157e+308
* toFinite('3.2'); // 3.2
* ```
*/
export function toFinite(value: unknown): number {
const num = Number(value);
if (!Number.isFinite(num)) {
return num > 0 ? Number.MAX_VALUE : -Number.MAX_VALUE;
}
return num;
}
Example
import { toFinite } from '@tulx/utils';
toFinite(3.2); // 3.2
toFinite(Number.MIN_VALUE); // 5e-324
toFinite(Infinity); // 1.7976931348623157e+308
toFinite('3.2'); // 3.2Related Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.