_createMathOperation.js 1003 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var baseToNumber = require('./_baseToNumber'),
  2. baseToString = require('./_baseToString');
  3. /**
  4. * Creates a function that performs a mathematical operation on two values.
  5. *
  6. * @private
  7. * @param {Function} operator The function to perform the operation.
  8. * @returns {Function} Returns the new mathematical operation function.
  9. */
  10. function createMathOperation(operator) {
  11. return function(value, other) {
  12. var result;
  13. if (value === undefined && other === undefined) {
  14. return 0;
  15. }
  16. if (value !== undefined) {
  17. result = value;
  18. }
  19. if (other !== undefined) {
  20. if (result === undefined) {
  21. return other;
  22. }
  23. if (typeof value == 'string' || typeof other == 'string') {
  24. value = baseToString(value);
  25. other = baseToString(other);
  26. } else {
  27. value = baseToNumber(value);
  28. other = baseToNumber(other);
  29. }
  30. result = operator(value, other);
  31. }
  32. return result;
  33. };
  34. }
  35. module.exports = createMathOperation;