UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

41 lines (40 loc) 1.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMathOperation = void 0; const baseToNumber_1 = __importDefault(require("./baseToNumber")); const baseToString_1 = __importDefault(require("./baseToString")); /** * Creates a function that performs a mathematical operation on two values. * * @private * @param {Function} operator The function to perform the operation. * @param {number} [defaultValue] The value used for `undefined` arguments. * @returns {Function} Returns the new mathematical operation function. */ function createMathOperation(operator, defaultValue) { return (value, other) => { if (value === undefined && other === undefined) { return defaultValue; } if (value !== undefined && other === undefined) { return value; } if (other !== undefined && value === undefined) { return other; } if (typeof value === 'string' || typeof other === 'string') { value = (0, baseToString_1.default)(value); other = (0, baseToString_1.default)(other); } else { value = (0, baseToNumber_1.default)(value); other = (0, baseToNumber_1.default)(other); } return operator(value, other); }; } exports.createMathOperation = createMathOperation; exports.default = createMathOperation;