/**
* Adds two numbers
* 两个数相加
* @param {number} augend The first number in an addition
* @param {number} addend The second number in an addition
* @returns {number} Returns the total
* @example
* add(6, 4)
* // => 10
* add(6, '4')
* // => '64'
*/
import createMathOperation from "./createMathOperation"
var add = createMathOperation(function(augend, addend) {
return augend + addend
}, 0)
export default add
/**
* Creates a function that performs a mathematical operation on two values
* @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
*/
import baseToString from "./baseToString"
import baseToNumber from "./baseToNumber"
function createMathOperation(operator, defaultValue) {
return function(value, other) {
var result
if (value === undefined && other === undefined) {
return defaultValue
}
if (value !== undefined) {
result = value
}
if (other !== undefined) {
if (result === undefined) {
return other
}
if (typeof value === "string" || typeof other === "string") {
value = baseToString(value)
other = baseToString(other)
} else {
value = baseToNumber(value)
other = baseToNumber(other)
}
result = operator(value, other)
}
return result
}
}
export default createMathOperation