_createMathOperation.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. * @param {number} [defaultValue] The value used for `undefined` arguments.
  9. * @returns {Function} Returns the new mathematical operation function.
  10. */
  11. function createMathOperation(operator, defaultValue) {
  12. return function(value, other) {
  13. var result;
  14. if (value === undefined && other === undefined) {
  15. return defaultValue;
  16. }
  17. if (value !== undefined) {
  18. result = value;
  19. }
  20. if (other !== undefined) {
  21. if (result === undefined) {
  22. return other;
  23. }
  24. if (typeof value == 'string' || typeof other == 'string') {
  25. value = baseToString(value);
  26. other = baseToString(other);
  27. } else {
  28. value = baseToNumber(value);
  29. other = baseToNumber(other);
  30. }
  31. result = operator(value, other);
  32. }
  33. return result;
  34. };
  35. }
  36. module.exports = createMathOperation;