rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
47 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Multiply = void 0;
const ensure_number_1 = require("./ensure-number");
const is_nan_util_1 = require("./is-nan.util");
const normalize_number_util_1 = require("./normalize-number.util");
const to_decimal_number_util_1 = require("./to-decimal-number.util");
const to_long_number_util_1 = require("./to-long-number.util");
const to_precision_util_1 = require("./to-precision.util");
/**
* @description Multiplies two numbers (`valueA` and `valueB`) with validation, normalization,
* and optional precision adjustment. Handles edge cases where the inputs are not valid numbers.
*
* @param valueA The first value to multiply. Can be a number or a value convertible to a number.
* @param valueB The second value to multiply. Can be a number or a value convertible to a number.
* @param precision The number of decimal places to apply to the result. Defaults to 5.
*
* @returns The result of the multiplication, normalized and adjusted to the specified precision.
* Returns `0` if either value is not a valid number.
*
* @example
* ```typescript
* Multiply(10, 2); // Returns 20
* Multiply(10, 2.5, 2); // Returns 25.00
* Multiply("10", "3"); // Returns 30 (handles string inputs)
* Multiply(NaN, 2); // Returns 0
* ```
*/
const Multiply = (valueA, valueB, precision = 5) => {
const isValueOneNumber = typeof valueA === 'number';
const isValueTwoNumber = typeof valueB === 'number';
const isBothNumber = isValueOneNumber && isValueTwoNumber;
if (!isBothNumber) {
const isNaNValueA = (0, is_nan_util_1.default)(valueA);
const isNaNValueB = (0, is_nan_util_1.default)(valueB);
const isBothNaN = isNaNValueA && isNaNValueB;
if (isBothNaN || isNaNValueA || isNaNValueB)
return 0;
const result = (0, to_precision_util_1.default)((0, to_decimal_number_util_1.default)((0, normalize_number_util_1.default)((0, to_decimal_number_util_1.default)((0, to_long_number_util_1.default)((0, normalize_number_util_1.default)(valueA)) * (0, to_long_number_util_1.default)((0, normalize_number_util_1.default)(valueB))))), precision);
return (0, ensure_number_1.default)(result);
}
const result = (0, to_precision_util_1.default)((0, to_decimal_number_util_1.default)((0, normalize_number_util_1.default)((0, to_decimal_number_util_1.default)((0, to_long_number_util_1.default)(valueA) * (0, to_long_number_util_1.default)(valueB)))), precision);
return (0, ensure_number_1.default)(result);
};
exports.Multiply = Multiply;
exports.default = exports.Multiply;
//# sourceMappingURL=multiply-number.util.js.map