@rashedmakkouk/dev-utils
Version:
Utility library.
25 lines (24 loc) • 961 B
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/** Utilities */
const isNumber_1 = __importDefault(require("lodash/isNumber"));
const utils_1 = require("../utils");
/**
* Converts value to and validates as 'number'.
*
* @returns Formatted number.
*/
function toNumeric(value, { decimal = true, math = decimal === false ? 'trunc' : undefined, precision, } = {}) {
const nextValue = !(0, isNumber_1.default)(value) ? Number(value) ?? 0 : value;
if (nextValue === 0 || !math || !utils_1.toNumericMathTypes.includes(math)) {
return nextValue;
}
const numericValue = Math[math](nextValue);
return precision == null || !utils_1.toFixedRange.includes(precision)
? numericValue
: Number(numericValue.toFixed(precision));
}
exports.default = toNumeric;