| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var baseToNumber = require('./_baseToNumber'), |
| 2 | baseToString = require('./_baseToString'); |
| 3 | |
| 4 | /** |
| 5 | * Creates a function that performs a mathematical operation on two values. |
| 6 | * |
| 7 | * @private |
| 8 | * @param {Function} operator The function to perform the operation. |
| 9 | * @param {number} [defaultValue] The value used for `undefined` arguments. |
| 10 | * @returns {Function} Returns the new mathematical operation function. |
| 11 | */ |
| 12 | function createMathOperation(operator, defaultValue) { |
| 13 | return function(value, other) { |
| 14 | var result; |
| 15 | if (value === undefined && other === undefined) { |
| 16 | return defaultValue; |
| 17 | } |
| 18 | if (value !== undefined) { |
| 19 | result = value; |
| 20 | } |
| 21 | if (other !== undefined) { |
| 22 | if (result === undefined) { |
| 23 | return other; |
| 24 | } |
| 25 | if (typeof value == 'string' || typeof other == 'string') { |
| 26 | value = baseToString(value); |
| 27 | other = baseToString(other); |
| 28 | } else { |
| 29 | value = baseToNumber(value); |
| 30 | other = baseToNumber(other); |
| 31 | } |
| 32 | result = operator(value, other); |
| 33 | } |
| 34 | return result; |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | module.exports = createMathOperation; |