mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 08:07:50 +00:00
36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
import baseToNumber from './baseToNumber.js'
|
|
import baseToString from './baseToString.js'
|
|
|
|
/**
|
|
* 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 = baseToString(value)
|
|
other = baseToString(other)
|
|
}
|
|
else {
|
|
value = baseToNumber(value)
|
|
other = baseToNumber(other)
|
|
}
|
|
return operator(value, other)
|
|
}
|
|
}
|
|
|
|
export default createMathOperation
|